定义
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());