Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question

closed as unclear what you're asking by Mat's Mug, amon, Malachi, rolfl, Simon Forsberg Dec 1 '13 at 23:48

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Hi, welcome to CR! Sorry your first question earned you a [badge:tumbleweed] for having low views, no comments and no answers for a while. I have upvoted this question, but as it stands it's very difficult to answer, without seeing what OurApplicationContext does with the form it's given. I suggest you post the form's constructor along with OurApplicationContext 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

Browse other questions tagged or ask your own question.