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());
作者:银龙
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!