如何在存储库模式中使用Unity添加依赖注入 [英] How to add dependency Injection using Unity in Repository Pattern

查看:146
本文介绍了如何在存储库模式中使用Unity添加依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的ASP.NET MVC Project Strucure。 (所有都是单独的项目)

1. FT.Web

2. FT.Entities

3. FT.BLL.Contracts
4. FT.BLL.Logic

5. FT.Data.Contracts

6. FT.Data.Sql



这里我想在Web项目中创建服务层(契约和逻辑)依赖项。我们可以添加合同项目参考而不是逻辑项目,因为Web项目只需要合同而不是实现。



我可以通过将Bll.Logic引用添加到Web Project来实现,但我不喜欢它,因为它与Web Project紧密结合。有没有其他方法可以在不添加引用的情况下实现依赖?顺便说一句,我使用Unity.MVC3进行依赖注入。

Here is my ASP.NET MVC Project Strucure. (all are seperate projects)
1. FT.Web
2. FT.Entities
3. FT.BLL.Contracts
4. FT.BLL.Logic
5. FT.Data.Contracts
6. FT.Data.Sql

Here I'd like to create the Service Layer (contract and Logic) dependency in Web project. We can add contract project reference but not Logic Project since web project only required contracts not implementation.

I am able to do it by adding the Bll.Logic reference to Web Project but I dont like it since its tightly coupled with Web Project. Is there any other way that we can achieve the dependency without adding the reference? btw, I am using Unity.MVC3 for dependency Injection.

推荐答案

这是我的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.


这篇关于如何在存储库模式中使用Unity添加依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆