Using Unity – Part 6
- by nmarun
This is the last of the ‘Unity’ series and I’ll be talking about generics here. If you’ve been following the previous articles, you must have noticed that I’m just adding more and more ‘Product’ classes to the project. I’ll change that trend in this blog where I’ll be adding an ICaller interface and a Caller class. 1: public interface ICaller<T> where T : IProduct
2: {
3: string CallMethod<T>(string typeName);
4: }
5:
6: public class Caller<T> : ICaller<T> where T:IProduct
7: {
8: public string CallMethod<T>(string typeName)
9: {
10: //...
11: }
12: }
We’ll fill-in the implementation of the CallMethod in a few, but first, here’s what we’re going to do:
create an instance of the Caller class
pass it the IProduct as a generic parameter
in the CallMethod method, we’ll use Unity to dynamically create an instance of IProduct implemented object
I need to add the config information for ICaller and Caller types.
1: <typeAlias alias="ICaller`1" type="ProductModel.ICaller`1, ProductModel" />
2: <typeAlias alias="Caller`1" type="ProductModel.Caller`1, ProductModel" />
The .NET Framework’s convention to express generic types is ICaller`1, where the digit following the "`" matches the number of types contained in the generic type. So a generic type that contains 4 types contained in the generic type would be declared as:
1: <typeAlias alias="Caller`4" type="ProductModel.Caller`4, ProductModel" />
On my .aspx page, I have the following UI design:
1: <asp:RadioButton ID="LegacyProduct" Text="Product" runat="server" GroupName="ProductWeb"
2: AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" />
3: <br />
4: <asp:RadioButton ID="NewProduct" Text="Product 2" runat="server" GroupName="ProductWeb"
5: AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" />
6: <br />
7: <asp:RadioButton ID="ComplexProduct" Text="Product 3" runat="server" GroupName="ProductWeb"
8: AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" />
9: <br />
10: <asp:RadioButton ID="ArrayConstructor" Text="Product 4" runat="server" GroupName="ProductWeb"
11: AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" />
Things to note here are that all these radio buttons belong to the same GroupName => only one of these four can be clicked. Next, all four controls postback to the same ‘OnCheckedChanged’ event and lastly the ID’s point to named types of IProduct (already added to the web.config file).
1: <type type="IProduct" mapTo="Product" name="LegacyProduct" />
2:
3: <type type="IProduct" mapTo="Product2" name="NewProduct" />
4:
5: <type type="IProduct" mapTo="Product3" name="ComplexProduct">
6: ...
7: </type>
8:
9: <type type="IProduct" mapTo="Product4" name="ArrayConstructor">
10: ...
11: </type>
In my calling code, I see which radio button was clicked, pass that as an argument to the CallMethod method.
1: protected void RadioButton_CheckedChanged(object sender, EventArgs e)
2: {
3: string typeName = ((RadioButton)sender).ID;
4: ICaller<IProduct> caller = unityContainer.Resolve<ICaller<IProduct>>();
5: productDetailsLabel.Text = caller.CallMethod<IProduct>(typeName);
6: }
What’s basically happening here is that the ID of the control gets passed on to the typeName which will be one of “LegacyProduct”, “NewProduct”, “ComplexProduct” or “ArrayConstructor”. I then create an instance of an ICaller and pass the typeName to it.
Now, we’ll fill in the blank for the CallMethod method (sorry for the naming guys).
1: public string CallMethod<T>(string typeName)
2: {
3: IUnityContainer unityContainer = HttpContext.Current.Application["UnityContainer"] as IUnityContainer;
4: T productInstance = unityContainer.Resolve<T>(typeName);
5: return ((IProduct)productInstance).WriteProductDetails();
6: }
This is where I’ll resolve the IProduct by passing the type name and calling the WriteProductDetails() method. With all things in place, when I run the application and choose different radio buttons, the output should look something like below:
Basically this is how generics come to play in Unity. Please see the code I’ve used for this here.
This marks the end of the ‘Unity’ series. I’ll definitely post any updates that I find, but for now I don’t have anything planned.