标签:
引用 dll
Microsoft.Practices.Unity.dll
Autofac.dll
IContainer 接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ctrip.HotelReservation.Ioc { public interface IContainer { TService Resolve<TService>(); TService Resolve<TService>(string serviceLine); object Resolve(Type type); void RegisterType(Type type); } }
IoCFactory 工厂
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ctrip.HotelReservation.Ioc { /// <summary> /// IoCFactory implementation /// </summary> public sealed class IoCFactory { #region Singleton static readonly IoCFactory instance = new IoCFactory(); /// <summary> /// Get singleton instance of IoCFactory /// </summary> public static IoCFactory Instance { get { return instance; } } #endregion #region Members IContainer _CurrentContainer; /// <summary> /// Get current configured IContainer /// <remarks> /// At this moment only IoCUnityContainer existss /// </remarks> /// </summary> public IContainer CurrentContainer { get { return _CurrentContainer; } } #endregion #region Constructor /// <summary> /// 这里可以指定具体的容器,是用Unity,还是Autofac /// </summary> static IoCFactory() { } IoCFactory() { _CurrentContainer = new IoCUnityContainer(); } #endregion } }
IoCUnityContainer 容器
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.Unity; using Ctrip.HotelReservation.Interface.DAL; using Ctrip.HotelReservation.DAL; using Ctrip.HotelReservation.Interface.DB; using Ctrip.HotelReservation.DB; using Ctrip.HotelReservation.SOA1; using Ctrip.HotelReservation.Interface.SOA1; using System.Web.Mvc; using Ctrip.HotelReservation.Biz.General.Interface; using Ctrip.HotelReservation.Biz; using Ctrip.HotelReservation.IBiz; using Ctrip.HotelReservation.Biz.GroupOrder; namespace Ctrip.HotelReservation.Ioc { public class IoCUnityContainer : IContainer { IUnityContainer Container { get; set; } public IoCUnityContainer() { Container = new UnityContainer(); ConfigureRootContainer(Container); } void ConfigureRootContainer(IUnityContainer container) {
满足开闭耦合,各个数据源的切换方便。大的改动可以方便的切换回去 #region 注册业务逻辑类 #region Dal层 container.RegisterType<IUserDAL, UserDAL>(); #endregion #region DB层 container.RegisterType<ICrmDB, CrmDB>(); container.RegisterType<ICrmbiDB, CrmbiDB>(); container.RegisterType<IObookingDB, ObookingDB>(); container.RegisterType<IOrderDB, OrderDB>(); container.RegisterType<IProductDB, ProductDB>(); container.RegisterType<IUserDB, UserDB>(); #endregion #region SOA1.0 层 container.RegisterType<IUserInvoices, UserInvoices>(); container.RegisterType<IUserAddressInfo, UserAddressInfo>(); container.RegisterType<IContactInfo, ContactInfo>(); container.RegisterType<IClientInfo, ClientInfo>(); container.RegisterType<ISmallCorpSMCorporationID, SmallCorpSMCorporationID>(); container.RegisterType<ITravelMoneyUser, TravelMoneyUser>(); container.RegisterType<IRead, Read>(); #endregion #endregion #region 注册数据组装类 #endregion #region 业务处理 container.RegisterType<IBaseDataBiz, BaseDataBiz>(); container.RegisterType<IOrderBiz, CreateOrderBiz>("GroupOrder"); container.RegisterType<IOrderBiz, CreateOrderBookingBiz>("BookingGroupOrder"); #endregion } public TService Resolve<TService>() { return Container.Resolve<TService>(); } public TService Resolve<TService>(string ServiceLine) { return Container.Resolve<TService>(ServiceLine); } public object Resolve(Type type) { return Container.Resolve(type, null); } public void RegisterType(Type type) { Container.RegisterType(type, new TransientLifetimeManager()); } } }
IoCAutofacContainer 容器
using Autofac; using Ctrip.HotelReservation.Biz.GroupOrder; using Ctrip.HotelReservation.DAL; using Ctrip.HotelReservation.DB; using Ctrip.HotelReservation.IBiz; using Ctrip.HotelReservation.Interface.DAL; using Ctrip.HotelReservation.Interface.DB; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ctrip.HotelReservation.Ioc { public sealed class IoCAutofacContainer : IContainer { ContainerBuilder Container { get; set; } Autofac.IContainer builder { get; set; } public IoCAutofacContainer() { Container = new ContainerBuilder(); ConfigureRootContainer(Container); } void ConfigureRootContainer(ContainerBuilder container) { #region 注册业务逻辑类 #region Dal层 container.RegisterType<UserDAL>().As<IUserDAL>(); #endregion #region DB层 container.RegisterType<CrmDB>().As<ICrmDB>(); container.RegisterType<CrmbiDB>().As<ICrmbiDB>(); //container.RegisterType<IObookingDB, ObookingDB>(); //container.RegisterType<IOrderDB, OrderDB>(); //container.RegisterType<IProductDB, ProductDB>(); //container.RegisterType<IUserDB, UserDB>(); #endregion #region SOA1.0 层 //container.RegisterType<IUserInvoices, UserInvoices>(); //container.RegisterType<IUserAddressInfo, UserAddressInfo>(); //container.RegisterType<IContactInfo, ContactInfo>(); //container.RegisterType<IClientInfo, ClientInfo>(); //container.RegisterType<ISmallCorpSMCorporationID, SmallCorpSMCorporationID>(); //container.RegisterType<ITravelMoneyUser, TravelMoneyUser>(); //container.RegisterType<IRead, Read>(); #endregion #endregion #region 注册数据组装类 #endregion #region 业务处理 //container.RegisterType<IBaseDataBiz, BaseDataBiz>(); container.RegisterType<CreateOrderBiz>().Named<IOrderBiz>("GroupOrder"); //按照业务线划分,实现多态 container.RegisterType<CreateOrderBookingBiz>().Named<IOrderBiz>("BookingGroupOrder"); #endregion builder = Container.Build(); } public TService Resolve<TService>() { return builder.Resolve<TService>(); } public TService Resolve<TService>(string ServiceLine) { if (builder.IsRegisteredWithName(ServiceLine,typeof(TService))) { return builder.ResolveNamed<TService>(ServiceLine); } return default(TService); } public object Resolve(Type type) { return builder.Resolve(type); } public void RegisterType(Type type) { Container.RegisterType(type); } } }
具体用法如下,解耦如下
IOrderBiz GOBiz = Ioc.IoCFactory.Instance.CurrentContainer.Resolve<IOrderBiz>("GroupOrder"); O_CreateOrder obj = GOBiz.BulidNewOrder<O_CreateOrder>(resinfo);
标签:
原文地址:http://www.cnblogs.com/kfsmqoo/p/4241734.html