ASP.NET MVVM and Depedency Injection

Model-View-ViewModel pattern is essentially the Presentation Model pattern. It is a design pattern to separate content from presentation and data-processing (model) from content. The term Presentation Model is mainly used here.
pm.png
The objects' relationships in the Presentation Model pattern are,
  • View references to Presentation Model.
  • View subscribes to Presentation Model’s events.
  • Presentation Model references to Model.
  • Presentation Model subscribes to Model’s events.
If it is in a traditional control flow, the process could be that the View creates an instance of Presentation Model and subscribes to its events. But in the real world implementation for ASP.NET,
  • View references to Presentation Model through interfaces for decoupling and testability.
  • Presentation Model can implement many interfaces required by many Views.
  • Presentation Model can be shared by many Views. E.g. A Presentation Model instance created on web form (ASPX) is shared by all user controls (ASCX) on the web form.
Therefore the View knows only an interface, not the type of the Presentation Mode. The View does not know how to create the Presentation Model incidence. Also the Presentation Model instance is shared between many Views. None of the View owns the Presentation Model instance.

In order to create the Presentation Model instances, a helper is needed. The Inversion of Control principal comes into the picture here. It brings two patterns, the Service Locator pattern or the Dependency Injection pattern.

Using the Service Locator pattern, there must be a Service Locator class that knows how to create and maintain references to Presentation Model instances. The View calls the Service Locator class to get a reference of Presentation Model instances.

    public ICustomerList controller {get; set;}
    protected void Page_Load(object sender, EventArgs e)
    {
        controller = ControllerLocator.GetController(this) as ICustomerList;
        customerGrid.DataSource = controller.Customers;
    }
Using the Dependency Injection pattern, it works just like the Hollywood principle, “Don’t call us, we will call you”. The View does not even know where the helper is. The View only marks where the Presentation Model instances are needed. Right after the View is created, the Presentation Model instances are there ready for use. This is done through a custom HttpModule in the ASP.NET MVVM framework.

    [Inject]
    public ICustomerList controller {get; set;}
    protected void Page_Load(object sender, EventArgs e)
    {
        customerGrid.DataSource = controller.Customers;
    }

There are many framework that provide the dependency injection function, for example Unity. The main difference between the ASP.NET MVVM framework and other frameworks is that the ASP.NET MVVM framework does not create special containers. It uses ASP.NET instinct object storage as the containers. They are Application, Session and the Request. The Presentation Model instances are stored in Application, Session and the Request.

    [Inject(Scope=InjectScope.Application)]
    public ILogger logger {get; set;}

    [Create(Scope=CreationScope.Session)]
    public CustomerController controller {get; set;}

    protected void Page_Load(object sender, EventArgs e)
    {
    }

The Presentation Model instances can be stored in the control tree, searching for the Presentation Model instances, the ASP.NET MVVM framework looks into the control tree to find them. It searches user control’s parent, then the parent of the parent until no parent. Nested user control hierarchy is supported. This flexibility enables us keep using the user controls as the application building blocks.

Last edited Mar 17, 2009 at 2:12 AM by yysun, version 2

Comments

No comments yet.