说明:需要引用using Microsoft.Practices.Unity;using Microsoft.Practices.Unity.Configuration; 两个企业库,using System.IO;using System.Configuration;以及接口和类的dll (IDAL.dll,DAL.dll)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.IO;
using System.Configuration;
namespace ObjectFactory
{
public sealed class ObjectFactory
{
private static readonly object containerLock = new object();
private static Dictionary Containers = new Dictionary();
public static IObject CreateObject()
{
IUnityContainer container = null;
lock (containerLock)
{
Type type = typeof(IObject);
string name = type.Assembly.GetName().Name;
string configName = (Path.GetDirectoryName(type.Assembly.CodeBase) + @"\" + name + ".config").Substring(6);
if (Containers.ContainsKey(name))
{
container = Containers[name];
}
else
{
container = LoadContainer(configName);
if (container != null)
{
Containers.Add(name, container);
}
}
}
if (container != null)
{
return Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(container, new ResolverOverride[0]);
}
return default(IObject);
}
public static IObject CreateObject(string strFilePath)
{
IUnityContainer container = null;
IObject entity=default(IObject);
lock (containerLock)
{
Type type = typeof(IObject);
string name = type.Assembly.GetName().Name;
Path.GetDirectoryName(type.Assembly.CodeBase);
string configName = strFilePath;
if (Containers.ContainsKey(name))
{
container = Containers[name];
}
else
{
container = LoadContainer(configName);
if (container != null)
{
Containers.Add(name, container);
}
}
}
if (container != null)
{
entity= Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(container, new ResolverOverride[0]);
return entity;
}
return entity
}
public static object CreateObject(Type interfaceType)
{
IUnityContainer container = null;
lock (containerLock)
{
string name = interfaceType.Assembly.GetName().Name;
string configName = (Path.GetDirectoryName(interfaceType.Assembly.CodeBase) + @"\" + name + ".config").Substring(6);
if (Containers.ContainsKey(name))
{
container = Containers[name];
}
else
{
container = LoadContainer(configName);
if (container != null)
{
Containers.Add(name, container);
}
}
}
if (container != null)
{
return Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(container, interfaceType, new ResolverOverride[0]);
}
return null;
}
private static IUnityContainer LoadContainer(string configName)
{
IUnityContainer container = new UnityContainer();
ConfigurationFileMap map2 = new ConfigurationFileMap();
map2.MachineConfigFilename = configName;
ConfigurationFileMap fileMap = map2;
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.OpenMappedMachineConfiguration(fileMap).GetSection("unity");
if (section != null)
{
container= Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(container, section);
return container;
}
return null;
}
}
}
以下是配制文件:
type="Microsoft.Practices.Unity.TransientLifetimeManager,Microsoft.Practices.Unity"/>
type="Microsoft.Practices.Unity.PerThreadLifetimeManager,Microsoft.Practices.Unity"/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.IO;
using System.Configuration;
namespace IOC
{
///
/// IOC容器类(需要引用接口和类的dll)
///
public sealed class Factory
{
//单例模式
public static readonly object singletonObj = new object(); //用于锁(在多线程时锁住一个线程,排斥其它线程以保证实例唯一)
public static Dictionary Containers = new Dictionary();
public static IObject CreateObject()
{
IUnityContainer container = null;
//default 用于泛型参数初始值,引用类型默认是null,值类型是0.
IObject entity = default(IObject);
lock (singletonObj)
{
Type type = typeof(IObject);
//接口名称
string interfaceName = type.Assembly.GetName().Name;
//获取接口的完整路径
string dllFullPath = type.Assembly.CodeBase;
//获取配制文件的位置
string configNamePth = (Path.GetDirectoryName(dllFullPath) + @"\" + interfaceName + ".config").Substring(6);
//创建区域容器实例
container = LoadUnityContainer(configNamePth);
if (!Containers.ContainsKey(interfaceName))
{
Containers.Add(interfaceName, container);
}
else
{
container = Containers[interfaceName];
}
}
if (container != null)
{
entity = Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(container, new ResolverOverride[0]);
}
return entity;
}
///
/// 创建区域容器实例
///
/// 配制文件绝对路径
///
public static IUnityContainer LoadUnityContainer(string configNamePath)
{
IUnityContainer container = new UnityContainer();
//指定映射配制文件
ConfigurationFileMap map = new ConfigurationFileMap();
map.MachineConfigFilename = configNamePath;
//打开指定的映射配制文件
UnityConfigurationSection section = ConfigurationManager.OpenMappedMachineConfiguration(map).GetSection("unity") as UnityConfigurationSection;
if (section != null)
{
container = Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(container, section);
return container;
}
return null;
}
}
}