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

该博客对比了Autofac、Castle.Windsor和Spring.NET三个IoC容器在单例和多例模式下的性能表现。测试结果显示,Autofac在初始化和非初始化时的速度均较快,尤其在多例模式下,性能最佳且稳定。Spring.NET在多例模式下性能下降明显,而Windsor则介于两者之间。综上,建议优先考虑Autofac作为IoC容器。
摘要由CSDN通过智能技术生成

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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值