zoukankan      html  css  js  c++  java
  • Unity的Resolving Objects by Using Overrides

    在一些情况下虽然我们抽象了接口或者基类型,但是配置不同的实现可能需要提供不同的初始化值,比如两个实现类型的构造函数参数相同,但一个类型的某个参数会产生变化。在Unity中可以通过ParameterOverride完成(Unity还提供了DependencyOverride、PropertyOverride等,复数后缀s)。当然关于constructor的构造完全可以通过配置完成。ParameterOverride的OnType可以指定条件类型。看一个示例:

     1 public interface IService
     2 {
     3 
     4 }
     5 
     6 public sealed class ServiceImpl : IService
     7 {
     8   public ServiceImpl(Int32 value)
     9   {
    10 
    11   }
    12 }
    13 
    14 public sealed class ServiceImpl2 : IService
    15 {
    16   public ServiceImpl2(Int32 value)
    17   {
    18 
    19   }
    20 }
    21 
    22 IUnityContainer unityContainer = new UnityContainer();
    23 
    24 unityContainer.LoadConfiguration();
    25 //unityContainer.RegisterType<IService, ServiceImpl>(new InjectionConstructor(1));
    26 unityContainer.RegisterType<IService, ServiceImpl2>(new InjectionConstructor(1));
    27 
    28 IService service = unityContainer.Resolve<IService>(new ParameterOverride(“value”, 1 * 2).OnType<ServiceImpl2>());
  • 相关阅读:
    【CCPC2020网络赛11】Convolution
    【CCPC2020网络赛02】Graph Theory Class
    全国中学生网安竞赛出题总结
    XDU2020ACM校赛总结
    CTF错误集合
    【洛谷2916】图的遍历
    20200420(ABC)题解 by 辛晓东
    20200402(ABC)题解 by 孙晨曦
    20200406(ABC)题解 by 徐光旭
    20200407(DE)题解 by 孙晨曦
  • 原文地址:https://www.cnblogs.com/junchu25/p/2631532.html
Copyright © 2011-2022 走看看