C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Getting started with Unity Application Block


Posted Date: 05 Aug 2008    Resource Type: Articles    Category: .NET Framework
Author: pradiptaMember Level: Gold    
Rating: Points: 5



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




Responses

Author: Anjum Rizwi    25 Oct 2008Member 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.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Unity:Dependency Injection Container from Microsoft P&P  .  Unity Sample  .  Unity Application Block  .  ObjectBuilder  .  Application Dependencies with Unity Application Block  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Accessing the Active Directory from Microsoft .NET
Previous Resource: Creating Setup project for windows application
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use