多个IoC容器适配器设计及性能测试和容器选择

1. 采用的IoC容器和版本

Autofac.2.6.3.862

Castle.Windsor.3.1.0

Spring.Core.2.0.0

 

2. 基础类库:服务类库和组件类库及相关的辅助类库

辅助类库:Demo.Common.dll

服务接口类库:Demo.Lib.dll

Oracle组件类库:Demo.Lib.Oracle.dll

Sql组件类库:Demo.Lib.Sql.dll

 

3. Autofac容器适配器

using Autofac;

using System;

namespace IoC.Core.Adapter
{
    public sealed class AutofacContainerAdapter : IContainerAdapter
    {
        public AutofacContainerAdapter()
        {
            Prepare();
        }
        private IContainer container;


        public void Prepare()
        {
            var autofacContainerBuilder = new ContainerBuilder();
            
            //autofacContainerBuilder.RegisterType<Singleton>()
            //        .As<ISingleton>()
            //        .SingleInstance();

            //autofacContainerBuilder.RegisterType<Transient>()
            //        .As<ITransient>();

            //autofacContainerBuilder.RegisterType<Combined>()
            //        .As<ICombined>();

            this.container = autofacContainerBuilder.Build();
        }

        public T Resolve<T>()
        {
            return this.container.Resolve<T>();
        }


        public T Resolve<T>(string ServiceName)
        {
            return this.container.ResolveNamed<T>(ServiceName);
        }


        public void Dispose()
        {
            // Allow the container and everything it references to be disposed.
            this.container = null;
        }

        public void Register(Type TService, Type TImplementation, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient)
        {
            if (!string.IsNullOrEmpty(ServiceName) && !container.IsRegisteredWithName(ServiceName, TService))
            {
                var autofacContainerBuilder = new ContainerBuilder();

                var item = autofacContainerBuilder.RegisterType(TImplementation);

                if (!string.IsNullOrEmpty(ServiceName))
                {
                    item = item.Named(ServiceName, TService);
                }
                else
                {
                    item = item.As(TService);
                }



                switch (LifeCycle)
                {
                    case LifeCycleType.Singleton:
                        item = item.SingleInstance();
                        break;
                    case LifeCycleType.Transient:
                        item = item.InstancePerDependency();
                        break;
                    case LifeCycleType.Request:
                        item = item.InstancePerLifetimeScope();
                        break;
                }


                

                //if (!string.IsNullOrEmpty(ServiceName))
                //{
                //    autofacContainerBuil
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值