zoukankan      html  css  js  c++  java
  • Unity实现IOC

    定义

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;
    
    namespace xgbfw.Infrastructure.Aop
    {
        public class Ioc
        {
            private static readonly IUnityContainer container;
            private static Dictionary<Type, Type> types;
            private static object lock_obj = new Object();
            static Ioc()
            {
                types = new Dictionary<Type, Type>();
                container = new UnityContainer();
                container.AddNewExtension<Interception>();
            }
    
    
            public static TFrom get<TFrom, TTo>() where TTo : TFrom
            {
                lock (lock_obj)
                {
                    if (!types.ContainsKey(typeof(TFrom)))
                    {
                        types.Add(typeof(TFrom), typeof(TTo));
    
                        //InterfaceInterceptor
                        container.RegisterType<TFrom, TTo>(new Interceptor<InterfaceInterceptor>(),
                            new InterceptionBehavior<PolicyInjectionBehavior>());
                    }
    
                    return container.Resolve<TFrom>();    
                }
            }
    
    
        }
    }

    应用

    var result = Ioc.get<IAlertInfoService, AlertInfoService>().Get(getId());
    
    var result = Ioc.get<ISysconfigService, SysconfigService>().Delete(getIds());
  • 相关阅读:
    Classic Source Code Collected
    Chapter 2 Build Caffe
    蓝屏代码大全 & 蓝屏全攻略
    AMD C1E SUPPORT
    DCU IP Prefether
    DCU Streamer Prefetcher
    adjacent cache line prefetch
    Hardware Prefetcher
    Execute Disable Bit
    Limit CPUID MAX
  • 原文地址:https://www.cnblogs.com/wangyinlon/p/13100368.html
Copyright © 2011-2022 走看看