WinForm - Using Umbraco ApplicationContext.Current
Hello all, I am currently updating the Visual Studio Package for the File --> New Project extension for Umbraco to use a Wizard and this case it is a simple WinForm that loads which asks the user which mode to use (MVC or WebForms) and the DB choice SQL CE or custom DB connection string..
I have it mostly working but I am currently stuck on updating the DB by using the Umbraco.Application.Current methods in the Database Service like so:
if (Umbraco.Core.ApplicationContext.Current != null)
{ Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConfigureEmbeddedDatabaseConnection(); }
Or alternatively this:
//Use Umbraco DB API - to update web.config if (Umbraco.Core.ApplicationContext.Current != null) { //Setup DB config Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConfigureDatabaseConnection(dbConnectionString); //Use API to create DB schema... (Skips Web Installer) Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database.CreateDatabaseSchema(); }
However my current issue is that the Current ApplicationContext is null so I am unable to use these awesome DB calls. Does anyone have any pointers or guidance for me.
So it's not possible to do then? I have seen Morten C create a command line application that uses the DB context before but not sure how to apply it to WinForms.
Morten's command line app was using internal stuff as far as I know. Umbraco.Web.Standalone was inspired by his app. But none of this is open at the moment because it's not finalized... If you want to look at it and see if it's polished enough we might open it, nothing against it I guess.
Be aware that the code may well break as Umbraco gets changed.
But I'm sure the core guys are well aware of the troubles we are having with coupling and internals, and will hopefully address it when the U7 mayhem is done. :)
I've been fiddling a bit with your project and I've got some good, and some bad news. :)
The bad: The DatabaseContext is too tightly coupled to different parts of Umbraco, which all has to be reflected. The good: However, there's not that much of the logic you really need to use which is actually coupled.
It's gonna take a little while to test and get right (and I'm tired), so I'd like to jump off again for now, but here's what I'd do if I were you:
- Get a clone of Umbraco - Create your own "DatabaseContext" class in your project, and copy the following methods from Umbraco.Core.DatabaseContext: - SaveConnectionString - Remove lines 242-247, and make sure you'll pass in a providerName - Remove lines 252 to 260, replace vDir with a parameter where you pass your own path to the root - ConfigureEmbeddedDatabaseConnection - Add path parameter to pass to SaveConnectionString - Replace GlobalSettings.FullpathToRoot in line 119 with your path - Remove call to initialize - ConfigureDatabaseConnection - Add path parameter to pass to SaveConnectionString
I might have missed something from browsing the code, but it should be the bulk of it. You've basically just replicated "simple" BCL usage from Umbraco, instead of getting "stuck" trying to re-use something not intended as an API.
When that class has been used to create the CE DB and/or set the connectionstring to the DB, you can just instantiate Umbraco.Core.Persistence.Database with a regular connection to the given DB. It, and it's CreateDatabaseSchema() method is public and free to use. (bless it's author)
WinForm - Using Umbraco ApplicationContext.Current
Hello all,
I am currently updating the Visual Studio Package for the File --> New Project extension for Umbraco to use a Wizard and this case it is a simple WinForm that loads which asks the user which mode to use (MVC or WebForms) and the DB choice SQL CE or custom DB connection string..
I have it mostly working but I am currently stuck on updating the DB by using the Umbraco.Application.Current methods in the Database Service like so:
Or alternatively this:
However my current issue is that the Current ApplicationContext is null so I am unable to use these awesome DB calls. Does anyone have any pointers or guidance for me.
Thanks,
Warren :)
You want to look at Umbraco.Web.Standalone which creates a context for such standalone apps. It's all internal at the moment, though.
Hey Stephen.
So it's not possible to do then? I have seen Morten C create a command line application that uses the DB context before but not sure how to apply it to WinForms.
https://github.com/sitereactor/umbraco-console-example
(I am not a WinForms developer at all :S )
Morten's command line app was using internal stuff as far as I know. Umbraco.Web.Standalone was inspired by his app. But none of this is open at the moment because it's not finalized... If you want to look at it and see if it's polished enough we might open it, nothing against it I guess.
But not tonight ;-(
Here is the source code of the WinForm app that I have written which is the Wizard
https://github.com/warrenbuckley/Umbraco-VS-New-Project/blob/master/Umbraco.VS.NewProject/Umbraco.VS.NewProject.Wizard/UserInputForm.cs
And this is the Wizard class that instantiates the WinForm to show
https://github.com/warrenbuckley/Umbraco-VS-New-Project/blob/master/Umbraco.VS.NewProject/Umbraco.VS.NewProject.Wizard/Wizard.cs
I've been fiddling a bit with your project and I've got some good, and some bad news. :)
The bad: The DatabaseContext is too tightly coupled to different parts of Umbraco, which all has to be reflected.
The good: However, there's not that much of the logic you really need to use which is actually coupled.
It's gonna take a little while to test and get right (and I'm tired), so I'd like to jump off again for now, but here's what I'd do if I were you:
- Get a clone of Umbraco
- Create your own "DatabaseContext" class in your project, and copy the following methods from Umbraco.Core.DatabaseContext:
- SaveConnectionString
- Remove lines 242-247, and make sure you'll pass in a providerName
- Remove lines 252 to 260, replace vDir with a parameter where you pass your own path to the root
- ConfigureEmbeddedDatabaseConnection
- Add path parameter to pass to SaveConnectionString
- Replace GlobalSettings.FullpathToRoot in line 119 with your path
- Remove call to initialize
- ConfigureDatabaseConnection
- Add path parameter to pass to SaveConnectionString
I might have missed something from browsing the code, but it should be the bulk of it. You've basically just replicated "simple" BCL usage from Umbraco, instead of getting "stuck" trying to re-use something not intended as an API.
When that class has been used to create the CE DB and/or set the connectionstring to the DB, you can just instantiate Umbraco.Core.Persistence.Database with a regular connection to the given DB. It, and it's CreateDatabaseSchema() method is public and free to use. (bless it's author)
Hope this helps you guys. :)
L-E
Thanks for the notes & ideas Lars.
Looks like I will be doing alot of copying & pasting code from the core today :-P
Cheers,
Warren
is working on a reply...