This project is read-only.

CreateShared attribute

Please read the last section before you use it.

The CreateShared attribute functions the same way as CreateNew except that it will only create one instance / request for the requested type. This can for instance be used in presenters to create a shared instance of the controller.

By doing this you can add events to the controller which reflects changes in the model. The different presenters can listen to these events and respond to changes made by another presenter. This is very useful for adding communication between user controls on a web form.


You can use it to decorate either parameters or public properties.
public OrderListPresenter([CreateShared] IOrders_ModuleController controller)
    _controller = controller;

public IOrders_ModuleController Controller { get; set; }

The attribute takes a named parameter Key which can be used to create different instances of the same type.
public OrderListPresenter([CreateShared(Key="OrderList")] IOrders_ModuleController controller)
    _controller = controller;


You can download a complete example from the source tree. The sample consists of a web form with two user controls. One control shows a list of orders, the other one shows the order rows of the currently selected order.

The communication is done through the controller, which exposes an event.
event EventHandler<DataEventArgs<string>> OrderSelected;

The order details presenter listens to this event and updates the list of order rows once an order is selected.
public OrderDetailsPresenter([CreateShared] IOrders_ModuleController controller)
    _controller = controller;
    controller.OrderSelected += new EventHandler<DataEventArgs<string>>(_controller_OrderSelected);

void _controller_OrderSelected(object sender, DataEventArgs<string> e)
    View.OrderRows = _controller.RetrieveOrderRows(e.Data);

The order list presenter contains code for selecting an order, and does this via the controller.
public void OnOrderSelected(string selectedOrderNo)

Important - before you can use

The attribute needs to be registered with the object builded before it can be used. There are two ways to do this via global.asax in your web project. More specifically it's the attributes parameter resolver that needs to be registered.

Inherit CreateSharedWebApplication
<%@ Application Language="C#" Inherits="WCSFContrib.Extensions.CompositeWeb.CreateSharedWebApplication" %>

Register the parameter resolver manually
Simply add the following snippet to your global.asax. (Make sure you don't inherit the application above too since this will register it twice!)
<script RunAt="server" Language="C#">
protected override void Start()
        new WCSFContrib.Extensions.CompositeWeb.CreateSharedParameterResolver());

Last edited Mar 24, 2008 at 9:18 PM by MatsL, version 3


mhedman Oct 1, 2008 at 5:45 PM 
If you upgrade an existing application to the CreateSharedWeb, you'll have to use the second method, as Global.asax only allows one application directive, unless you replace the <%@ Application Language="C#" Inherits="Microsoft.Practices.CompositeWeb.WebClientApplication" %> with it.