I have been learning the Unity IoC container recently as we will be making use of it in a project I am working on. Like all IoC containers, it makes it nice and easy to automatically construct an object, fulfilling all its dependencies.
One issue that comes up frequently when using IoC containers, is how to implement lazy loading. For example, suppose my class has a dependency on IEmailSender, but only uses it in certain circumstances. I might not wish for the concrete implementation to be created until I actually know I need it.
public class MyClass(IEmailSender emailSender)
One quick way round this is to take a dependency on the container instead. With Unity, the container comes already registered, so you can simply change the constructor prototype. Now you can call container.Resolve
public class MyClass(IUnityContainer container)
The disadvantage of this solution is that we have now obscured the real dependencies of MyClass. It could ask for anything it likes from the container, and we have to examine the code to find out what it actually uses. Fortunately, there is a way we can solve this using Unity’s ability to allow you to register open generic types.
Suppose we create a generic class called Lazy, that implements ILazy as follows:
public interface ILazy
{
T Resolve();
T Resolve(string namedInstance);
}
public class Lazy
{
IUnityContainer container;
public Lazy(IUnityContainer container)
{
this.container = container;
}
public T Resolve()
{
return container.Resolve
}
public T Resolve(string namedInstance)
{
return container.Resolve
}
}
Now we need to tell our container to use Lazy when someone asks for ILazy:
container.RegisterType(typeof(ILazy<>),typeof(Lazy<>));
And now that allows us to change our original class to have the following prototype:
public class MyClass(ILazy
Now we have advertised that our class depends on an IEmailService, but have not created it immediately on construction of MyClass, nor have we allowed MyClass to get at the IUnityContainer itself.
Sunday, September 6, 2009
Lazy Loading of Dependencies in Unity
Subscribe to:
Posts (Atom)
Archives
-
►
2008
(527)
-
►
October
(20)
- Extend BoundField Of GridView
- How To Add Tooltip in GridView
- Building a Simple CSV Parser in C#
- C# Tutorial - Extension Methods
- C# Tutorial - The Readonly Keyword
- How To - Prevent Script Attacks
- How To: Prevent Cross-Site Scripting in ASP.NET
- How To Add Title Attribute To GridView Row
- Hidden Field Inside GridView
- How To Display data in marquee from database by us...
- FileUpload and Ajax UpdatePanel
- Binding Data to a Web Forms DataList
- How To Open div popup with a LightBox effect
- Asynchronous GridView in 5 simple steps
- size of the viewstate in ASP.NET
- How To Create a Value Type That Can Be Initialized...
- How To Add Programming Languages in the App_Code F...
- How To Create A Filebrowser in ASP
- How to add an RSS feed to your website
- Export Html to Pdf using iTextSharp(GridView)
-
►
September
(257)
- How to display a serial number from 1 to n in a gr...
- Load Controls Dynamically in ASP.NET
- Three Tier Architecture with ASP.NET
- ADO.NET Tutorial
- Building a DAL using Strongly Typed TableAdapters ...
- Return new identity from Strongly Typed Dataset Da...
- Select a row in an asp:GridView without using a Se...
- How To Restrict the number of rows in a dataview w...
- How To Convert HTML to Text, Easily
- How To Post Data From ASP.net To Another URL
- The .NET Replacement for App.Path
- Binding a Listcontrol(Dropdownlist) to Enumeration...
- Tips
- using arrayList as DataSource for Gridview
- Enum With String Values In C#
- Making a small Popup picture on mouse over event
- Databinding Tips: Nesting Eval Statements
- How To Add Checkboxlist Dynamically
- Working with the DOM
- Performing a Server Request Asynchronously
- Autosugget Using xmlHTTP in Asp.net
- Asynchronous Request to Server (JavaScript)
- How To Create Ajax Based Search Page using xmlHTTP...
- How To Create Ajax Based Search Page using xmlHTTP...
-
►
October
(20)



