UnityContainer在web.config中配置

时间:2012-04-11 17:59:28

标签: c# asp.net dependency-injection inversion-of-control unity-container

我有以下代码

var container = new UnityContainer();                       //LINE 1
container.RegisterType<ILogUtility,LogUtil>();              //LINE 2
var logger = container.Resolve<Logger>();                   //LINE 3
logger.Log(LogType.Warn, "logging from container");         //LINE 4

如何在web.config中实现第2行,这样我只需要在我的代码中编写第1,3和4行代码?我已经在每个地方搜索代码示例,但它们并不清楚。

由于

1 个答案:

答案 0 :(得分:12)

看看我的教程

http://netpl.blogspot.com/2011/11/unity-application-block-is-lightweight.html

有一个示例XML配置:

<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">       
<container>        
<register type="ConsoleApplication30.Logic.ICustomService, ConsoleApplication30"                  
          mapTo="ConsoleApplication30.Logic.CustomServiceImpl, ConsoleApplication30" />    
</container></unity>

然后用

加载它
IUnityContainer container = new UnityContainer();                
container.LoadConfiguration();
免费获取贴纸的机会↓↓↓
豫ICP备18024241号-1