Microsoft has recently launched the Unity Application Block(Unity).Unity is a lightweight,extensible dependency injection container.
Its built upon the next version of ObjectBuilder.In fact ObjectBuilder which was part of Composite Application Block has been removed and added to Unity after some changes. These are few Advantages of using Unity:
- Loosly coupled design
- Simplified object creation
- Specify dependency in configuration file to resolve dependency at run time
- Allows client to store and cache container using service locator
- As it is extensible so it can be modified to suit a particular requirement
Here is a sample application using MVP pattern
Add the components entry to the config file
type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" /> type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" />
The config entries can be read and the container instance is created in Global.asax file.Here I have created a class so that the container can be accessed from a shared instance(IoC in this example)
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) Dim container As IUnityContainer = New UnityContainer() Dim section As UnityConfigurationSection = CType(ConfigurationManager.GetSection("unity"), UnityConfigurationSection) section.Containers.Default.Configure(container) IoC.Initialize(container) End Sub The presenter is resolved at run time using a key in the config file Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load _presenter = IoC.Container.Resolve(Of TaskPresenter)("presenter") _presenter.SetView = Me End Sub
The Model instances are resolved in the constructor of the presenter.So no need to create the instance explicitly rather Unity does it for you at run time.You can specify the life time of the components.In this example Logger is a singleton instance is created once which is shared across the application.
The construtor of the presenter looks like below
Public Sub New(ByVal log As ILogger, ByVal task As ITask) _log = log _task = task End Sub
Attachments
- Getting Started with Unity Application Block (20074-5910-UnitySample.zip)
- Unity Sample (20074-51037-UnitySample.zip)
For more details, visit http://msdn.microsoft.com/en-us/library/cc511654.aspx
|
| Author: Anjum Rizwi 25 Oct 2008 | Member Level: Silver Points : 1 |
brief but good Inro.
Can you please explain the sample application. I did not see any input or output? Any comparision chart why we go for Unity App Block.
|