且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在存储库模式中使用Unity添加依赖注入

更新时间:2023-01-16 08:19:08

这是我的Unity配置代码



Here is my code for Unity configuration

<?xml version="1.0"?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <typeAliases>

    <!-- Services -->
    <typeAlias alias="IUserAccountService" type="FT.BLL.Contracts.IUserAccountService, Antony.BLL.Contracts" />
    <typeAlias alias="UserAccountService" type="FT.BLL.Logic.UserAccountService, Antony.BLL.Logic" />


  </typeAliases>
  <container>
    <register type="IUserAccountService" mapTo="UserAccountService" />
  </container>
</unity>







和Bootstrapper.cs代码






And Bootstrapper.cs code

private static IUnityContainer BuildUnityContainer()
       {
          // var container = new UnityContainer();

           var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
           IUnityContainer container = new UnityContainer().LoadConfiguration(section);

           // register all your components with the container here
           // it is NOT necessary to register your controllers

           // e.g. container.RegisterType<ITestService, TestService>();

           return container;
       }





此配置设置需要在Web项目中添加两个服务项目。但我只想添加接口参考。不是实现层引用。

谢谢。任何帮助。



This configuration setting expects both service projects need to be added in Web project. But I just want to add Interface reference only. not implementation layer reference.
Thanks. Any Help.