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