Share via


Implementing MVC pattern in .NET CF applications (Part 2).

Let's continue on the way how-to implement the model-viewer-controller pattern in .NET Compact Framework applications that I started in my previous post. We stopped at the point where we needed to figure out on how to connect the LoginController with the concrete instance of the LoginForm. Of course we can create an instance of the LoginController class when loading the LoginForm, but in this case we would break the decoupling by doing this. The better solution would be to create another class ApplicationManager that will be responsible for instantiation of the LoginForm (or any other forms that you will have in your application), keeping a memory cache of the loaded forms and displaying/hiding them. So here we go:

5. Create ApplicationManager class  

public class ApplicationManager

{

        #region fields

        private static ApplicationManager appManager;

        private Dictionary<string, IController> controllersCache;

        #endregion

        #region constructors

        static ApplicationManager()

        {

            // Create an instance of itself for a singleton

            appManager = new ApplicationManager();

        }

        public ApplicationManager()

        {

            controllersCache = new Dictionary<string, IController>();

        }

        #endregion

        public static ApplicationManager Instance

        {

            get

            {

                return appManager;

            }

        }

        public LoginForm GetLoginForm()

        {

            // Check if we already have the form in the cache 

            if (!controllersCache.ContainsKey("Login"))

            {

                controllersCache.Add("Login",

                            new LoginController(new LoginForm()));

            }

           

            return controllersCache["Login"].View

                                           as LoginForm;

        }

 

               

        public void ShowLoginForm()

        {

      if (!controllersCache.ContainsKey("Login"))

             {

                    controllersCache.Add("Login",

                           new LoginController(new LoginForm()));

             }

             controllersCache["Login"].View.Show();

        }

 }

I've implemented the ApplicationManager as a singleton. It creates an instance of its own in the static constructor and also implements the static property Instance to expose the singleton for consuming parts. It also includes the controllersCache dictionary that stores instances of the controllers with associated viewer in it.

OK, we are almost done here. Just one thing is left - to make a call to the ApplicationManager to get an instance of the LoginForm. For example, we can do it in the entry point of our application:

[MTAThread]

static void Main()

{

     LoginForm form = ApplicationManager.Instance.GetLoginForm();

      Application.Run(form);

}

So that is all to it. You should be able to take it from here: add more forms to the project and implement the view and controller for them. You can download the project from here, so you should be able to step through the code and see how it works.

 

MVC_Mobile.zip

Comments

  • Anonymous
    August 07, 2007
    Shouldn't the instance constructor for the ApplicationManager class be non-public?

  • Anonymous
    August 07, 2007
    Also, I'm not seeing the Model anywhere in your MVC implementation.

  • Anonymous
    August 07, 2007
    Hi Neil, You are right, it is mostly viewer+controller. You should be able to plug in any model into it. -Alex

  • Anonymous
    August 07, 2007
    As Neil has astutely observed, the code that I described in my previous posts does not have an implementation

  • Anonymous
    October 09, 2008
    I've just got back from a long and big project with one of our customers for whom we have created an

  • Anonymous
    October 29, 2008
    Hi Alex, Nice work! You might also want to take a look at <a href="http://www.mvcsharp.org" title="Model View Presenter for .NET">MVC#</a> - a Model-View-Presenter framework with .NET Compact Framework support. Kind regards, -- Oleg Zhukov

  • Anonymous
    August 23, 2009
    It's perfect... I am just curious to verify if you are having a singleton instance then why the second constructor is public. I have one point - what's major reason you did not prefer the MCSF?Is it only slowness?

  • Anonymous
    August 30, 2009
    The MCSF is slow. Take a look at the Mobile MVC: http://blogs.msdn.com/priozersk/archive/tags/MVC/default.aspx