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());
  • 相关阅读:
    SpringCache使用
    SpringDataRedis使用
    Spring data jpa使用
    webpack的安装
    Vue基本使用
    Swagger使用
    gulp常用插件之gulp-notify使用
    gulp常用插件之gulp-beautify使用
    gulp常用插件之gulp-uglify使用
    gulp常用插件之gulp-size使用
  • 原文地址:https://www.cnblogs.com/wangyinlon/p/13100368.html
Copyright © 2011-2022 走看看