Unity-依赖注入之构造注入

  菜鸟有话说:对依赖注入理解不是很深,只是把学到的东西记录下来,以便日后查看。依赖注入分为构造注入、属性注入和方法注入,今天我主要介绍下构造注入。

  构造注入主要是解耦各数据层之间的调用关系,使其之间没有直接的依赖关系。

  1、首先新建IOCContainner类,只是IOC容器,ConfigureContainer()方法主要实现接口和实现类之间的匹配。

 1 static void ConfigureContainer()
 2 {
 3     /*
 4      * Add here the code configuration or the call to configure the container 
 5      * using the App configuration file
 6      */
 7     currentContainer = new UnityContainer().AddNewExtension<Interception>();
 8     //-> Adapters
 9    currentContainer.RegisterType<ITypeAdapterFactory, AutomapperTypeAdapterFactory>(new ContainerControlledLifetimeManager());
10    currentContainer.RegisterType<IUserBaseServiceImpl, UserBaseServiceImpl>();
11    currentContainer.RegisterType<IUserBaseRepository, UserBaseRepository>();
12 }

     2、在实现类里面的构造方法:

1 readonly IUserBaseRepository UserBaseRepository;
2 public UserBaseServiceImpl(IUserBaseRepository userBaseRepository)
3 {
4   this.UserBaseRepository = userBaseRepository;
5 }

    3、前台调用

1 [Dependency]
2 public IUserBaseServiceImpl UserBaseServiceImplInstance
3 {
4         get;
5         set;
6 }

 

 

转载于:https://www.cnblogs.com/xuguoming/articles/3166418.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值