一.EngineContext初始化
[MethodImpl(MethodImplOptions.Synchronized)]//可以确保在不同线程中运行的该方式以同步的方式运行
public static IEngine Initialize(bool forceRecreate)
{
if (Singleton<IEngine>.Instance == null || forceRecreate)
{
Singleton<IEngine>.Instance = new NopEngine(); var config = ConfigurationManager.GetSection("NopConfig") as NopConfig;
Singleton<IEngine>.Instance.Initialize(config);
}
return Singleton<IEngine>.Instance;
}
EngineContext.Initialize(false)
二.IEngine接口初始化
public void Initialize(NopConfig config)
{
//register dependencies 注册依赖项 nop 使用 Autofac进行依赖注入
RegisterDependencies(config); //register mapper configurations 注册对象映射配置 nop 使用 AutoMapper进行对象映射
RegisterMapperConfiguration(config); //startup tasks 启动任务
if (!config.IgnoreStartupTasks)
{
RunStartupTasks();
} }
1.调用RegisterDependencies(config)方法进行依赖注入(IOC)初始化工作
我们先看看代码
/// <summary>
/// 使用AutoFac注册依赖项
/// 遍历所有的实现IDependencyRegistrar接口实例进行注册
/// Register dependencies
/// </summary>
/// <param name="config">Config</param>
protected virtual void RegisterDependencies(NopConfig config)
{
var builder = new ContainerBuilder(); //dependencies
var typeFinder = new WebAppTypeFinder();
builder.RegisterInstance(config).As<NopConfig>().SingleInstance();
builder.RegisterInstance(this).As<IEngine>().SingleInstance();
builder.RegisterInstance(typeFinder).As<ITypeFinder>().SingleInstance(); //register dependencies provided by other assemblies
var drTypes = typeFinder.FindClassesOfType<IDependencyRegistrar>();
var drInstances = new List<IDependencyRegistrar>();
foreach (var drType in drTypes)
drInstances.Add((IDependencyRegistrar) Activator.CreateInstance(drType));
//sort
drInstances = drInstances.AsQueryable().OrderBy(t => t.Order).ToList();
foreach (var dependencyRegistrar in drInstances)
dependencyRegistrar.Register(builder, typeFinder, config); var container = builder.Build();
this._containerManager = new ContainerManager(container); //set dependency resolver
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
2.调用RegisterMapperConfiguration(config)方法进行对象映射
还是先看代码
/// <summary>
/// AutoMapper为对象映射
/// 遍历所有的实现IMapperConfiguration接口实例进行注册
/// Register mapping
/// </summary>
/// <param name="config">Config</param>
protected virtual void RegisterMapperConfiguration(NopConfig config)
{
//dependencies
var typeFinder = new WebAppTypeFinder(); //register mapper configurations provided by other assemblies
var mcTypes = typeFinder.FindClassesOfType<IMapperConfiguration>();
var mcInstances = new List<IMapperConfiguration>();
foreach (var mcType in mcTypes)
mcInstances.Add((IMapperConfiguration)Activator.CreateInstance(mcType));
//sort
mcInstances = mcInstances.AsQueryable().OrderBy(t => t.Order).ToList();
//get configurations
var configurationActions = new List<Action<IMapperConfigurationExpression>>();
foreach (var mc in mcInstances)
configurationActions.Add(mc.GetConfiguration());
//register
AutoMapperConfiguration.Init(configurationActions);
}
3.运行启动任务
/// <summary>
/// 启动时运行的任务任务
/// 遍历所有实现IStartupTask接口的实例
/// Run startup tasks
/// </summary>
protected virtual void RunStartupTasks()
{
var typeFinder = _containerManager.Resolve<ITypeFinder>();
var startUpTaskTypes = typeFinder.FindClassesOfType<IStartupTask>();
var startUpTasks = new List<IStartupTask>();
foreach (var startUpTaskType in startUpTaskTypes)
startUpTasks.Add((IStartupTask)Activator.CreateInstance(startUpTaskType));
//sort
startUpTasks = startUpTasks.AsQueryable().OrderBy(st => st.Order).ToList();
foreach (var startUpTask in startUpTasks)
startUpTask.Execute();
}
三.总结:
本文地址:http://www.cnblogs.com/yaoshangjin/p/7221586.html
本文为大波浪原创、转载请注明出处。