1、基本方式
为了接下来的说明,我们先编写几个后面需要的接口和类:
准备工作:ILogger 接口
public interface ILogger { void Log(string msg); }
继承上述接口的子类
public class DBLogger : ILogger
{
public void Log(string msg)
{
Console.WriteLine("数据库记录消息为:" + msg);
}
}
及
public class FileLogger : ILogger
{
public void Log(string msg)
{
Console.WriteLine("日志文件记录消息为:" + msg);
}
}
Step1. 创建对象容器
在Unity中创建容器实例最简单的方法是直接使用构造函数创建,如下代码所示:
IUnityContainer container = new UnityContainer();
Step2. 注册接口映射
在Unity中提供了一组Register方法供我们在容器中注册接口映射,如下代码所示:
container.RegisterType<ILogger, DBLogger>();
Step3. 获取对象实例
在Unity中提供了一组Resolve方法用以获取对象实例,如下代码所示:
var logger = container.Resolve<ILogger>();
OK,就这么简单!
测试如下:
class Program
{
static void Main(string[] args)
{
//
IUnityContainer container = new UnityContainer();
container.RegisterType<ILogger, DBLogger>();
var logger = container.Resolve<ILogger>();
logger.Log("孙行者:看我72变");
//
container.RegisterType<ILogger, FileLogger>();
logger = container.Resolve<ILogger>();
logger.Log("二郎神:看我73变");
Console.ReadKey();
}
}
2、配置文件的方式
public class UnityIocHelper : IServiceProvider
{
private readonly IUnityContainer _container;
private static readonly UnityIocHelper dbinstance = new UnityIocHelper("DBcontainer");
private UnityIocHelper(string containerName)
{
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
_container = new UnityContainer();
section.Configure(_container, containerName);
}
public static UnityIocHelper DBInstance
{
get { return dbinstance; }
}
public object GetService(Type serviceType)
{
return _container.Resolve(serviceType);
}
public T GetService<T>()
{
return _container.Resolve<T>();
}
public T GetService<T>(params ParameterOverride[] obj)
{
return _container.Resolve<T>(obj);
}
public T GetService<T>(string name, params ParameterOverride[] obj)
{
return _container.Resolve<T>(name, obj);
}
}
具体调用方法
public static IDatabase Base(string connString)
{
return UnityIocHelper.DBInstance.GetService<IDatabase>(“MySql”, new ParameterOverride(
"connString", connString));
}
配置信息
<unity>
<typeAliases>
<typeAlias alias="IDatabase" type="LRMC.Data.IDatabase,LRMC.Data" />
<typeAlias alias="SqlServer" type="LRMC.Data.SqlDapper.Database,LRMC.Data.SqlDapper" />
<typeAlias alias="MySql" type="LRMC.Data.MySqlDapper.Database,LRMC.Data.MySqlDapper" />
</typeAliases>
<containers>
<container name="DBcontainer">
<!-- 默认数据库软件类型:SqlServer,MySql,Oracle,Access,SQLite-->
<type type="IDatabase" mapTo="SqlServer" name="SqlServer"></type >
<type type="IDatabase" mapTo="MySql" name="MySql"></type >
</container>
</containers>
</unity>
typeAlias 指定一个类的别名,<type type="IDatabase" mapTo="MySql" name="MySql"></type >就是IDatabase这个接口通过MySql(LRMC.Data.MySqlDapper.Database)这个类去实现。
另外在unity2.0配置中alias节点下的生命管理周期配置并不需要了,比如我们创建一个单例注册类型,只需要配置下面就可以了:
<containers>
<container name="defaultContainer">
<register type="UnityContainerDemo.IPeople, UnityContainerDemo" mapTo="UnityContainerDemo.VillagePeople01, UnityContainerDemo">
<lifetime type="singleton" />
</register>
<register type="UnityContainerDemo.IWaterTool, UnityContainerDemo" mapTo="UnityContainerDemo.PressWater, UnityContainerDemo"/>
</container>
</containers>
</unity>