Welcome to MSDN Blogs Sign in | Join | Help

Alex Yakhnin's Blog

Development and architecture for Windows Mobile devices. And a few useful tips on the way...

News

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.

 

Posted: Tuesday, August 07, 2007 2:44 AM by priozersk
Filed under:

Attachment(s): MVC_Mobile.zip

Comments

Neil Cowburn said:

Shouldn't the instance constructor for the ApplicationManager class be non-public?

# August 7, 2007 8:43 AM

Neil Cowburn said:

Also, I'm not seeing the Model anywhere in your MVC implementation.

# August 7, 2007 8:53 AM

priozersk said:

Hi Neil,

You are right, it is mostly viewer+controller. You should be able to plug in any model into it.

-Alex

# August 7, 2007 9:34 AM

Alex Yakhnin - Mobility Consultant, Former MVP said:

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

# August 7, 2007 5:23 PM

Alex Yakhnin - Mobility Consultant, Former MVP said:

I've just got back from a long and big project with one of our customers for whom we have created an

# October 9, 2008 4:50 PM

OVZh said:

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

# October 30, 2008 7:19 AM

Only Arindam said:

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?

# August 23, 2009 2:44 PM

priozersk said:

The MCSF is slow. Take a look at the Mobile MVC:

http://blogs.msdn.com/priozersk/archive/tags/MVC/default.aspx

# August 30, 2009 9:35 AM
Anonymous comments are disabled
Page view tracker