I've got to maintain a legacy application (actually a number of applications) and currently most of the startup-settings (like upgrading App.config when the version has changed, check Database against Datasets, etc.) happens in the MainForm or in a Singleton object called "ApplicationController" which is more or less instantiated (ApplicationController.Instance -> private constructor) some time during the MainForm construction. (we've got a common library, let's call it OurCore
where a MainForm resides which is then inherited by the concrete frmMain in the application itself which is then run by Application.Run()
in the program.cs of the respecting project.
I want to move the bootstrap code (checking DB/DataSet integrity, check for some values in the DB, etc.) into some kind of "application wrapper" to get rid of that ugly Singleton thing that gives us a hard time because of the magic going on inside into some easier maintainable structure.
So far I've come up with the following Idea and I'd like to know if this is bad design and if there are better ways to do this:
public class OurCoreApplication
{
private Form _mainForm;
private Type _mainFormType;
protected OurCoreApplication(Type mainFormType)
{
_mainFormType = mainFormType;
}
public virtual void Run(params object[] args)
{
try
{
_mainForm = (Form)Activator.CreateInstance(_mainFormType, args);
var applicationContext = new OurApplicationContext(_mainForm);
UpgradeSettings();
// connect to DB and verify DataSet against Database structure, etc. ..
Application.Run(applicationContext);
}
catch (Exception ex)
{
Logger.Fatal("Unexpected Exception - Application crashed", ex);
}
}
Concrete Application
public class ConcreteApplication : OurCoreApplication
{
public ConcreteApplication () : base(typeof (frmMain))
{
}
public override void Run(params object[] args)
{
var configPath = Settings.Default.ConfigPath;
base.Run(configPath);
}
}
I'm aware that I have to run Application.Run()
to get the message loop started. Are there any other drawbacks? Does anyone have a better solution than what I'm thinking of? I'm really not sure if this is the best way to do this, but I want to seperate the concerns of the MainForm (just beeing a form without having to check application specific data or doing application specific tasks.
OurApplicationContext
does with the form it's given. I suggest you post the form's constructor along withOurApplicationContext
so we have a clearer picture. Again, sorry CR hasn't been a very exciting experience for you so far. Feel free to edit your question with more details; I'm voting to put it on hold until there's more meat for review. – Mat's Mug♦ Dec 1 '13 at 21:02