最近公司用到了Unity,自己就研究了一下。
/// <summary>Registers the type mappings with the Unity container.</summary> /// <param name="container">The unity container to configure.</param> /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> public static void RegisterTypes(IUnityContainer container) { // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. container.LoadConfiguration(); // TODO: Register your types here // container.RegisterType<IProductRepository, ProductRepository>(); }
2.在configSections节点下添加以下内容
<configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/> </configSections>
3.配置unity节点信息
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <assembly name="IBLL"/> <assembly name="BLL"/> <containers> <container> <register type="IBLL.ITest,IBLL" mapTo="BLL.Test,BLL" /> </container> </containers> </unity>
配置完成运行就ok了。
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <containers> <container> <register type="IBLL.ITest" mapTo="BLL.Test" /> </container> </containers> </unity>
这种情况下会报这种错误
/// <summary>Registers the type mappings with the Unity container.</summary> /// <param name="container">The unity container to configure.</param> /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> public static void RegisterTypes(IUnityContainer container) { // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. //container.LoadConfiguration(); // TODO: Register your types here // container.RegisterType<IProductRepository, ProductRepository>(); container.RegisterType<IBLL.ITest,BLL.Test>(); }
以上是个人使用经验分享给大家,上面的内容比较浅显,如果有错误请大家指正
原文:http://www.cnblogs.com/chengxuzhimei/p/4977754.html