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