Example : Using Microsoft Unity Block from Configuration File

/// Client code ///


using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using Microsoft.Practices.Unity;

namespace unityman
{
class Program
{
static void Main(string[] args)
{
IUnityContainer mycontainer = new UnityContainer();
UnityConfigurationSection config = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
config.Containers.Default.Configure(mycontainer);
MyInterface proxy = mycontainer.Resolve();
proxy.SayHello();

}
}
}


////// App.config Configuration File ////



Optionally you can change MyClass from FriendClass to see it's effects.


//// MyInterface, MyClass and FriendClass codes


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Unity;

namespace myunityclass
{
public class MyClass : MyInterface
{
public MyClass(MyParam myParam)
{
myParam.UserAddress = "1111";
myParam.userName = "Jeremy";
}


///// Forces Unity to use this constructor /////

[InjectionConstructor]
public MyClass(MyParam myParam, MyParam myParam2)
{
myParam.UserAddress = "1111";
myParam.userName = "Jeremy";

}


public void SayHello()
{

}


}

public class FriendClass : MyInterface
{

public FriendClass()
{
string my = "something something";
}

public void SayHello()
{

}


}


public class MyParam
{

private string _userName;

public string userName
{
get { return _userName; }
set { _userName = value; }
}


private string _userAddress;

public string UserAddress
{
get { return _userAddress; }
set { _userAddress = value; }
}


}

public interface MyInterface
{
void SayHello();
}
}

Comments

Popular posts from this blog

OpenCover code coverage for .Net Core

ionic2 cordova build android - Unable resolve gradle 2.2.3

Using Custom DLL with IronPython / Scripts