/Autofac.Engine

easy to use Autofac

Primary LanguageC#OtherNOASSERTION

有关Autofac的使用帮助可以查看:Autofac中文文档

注册组件

可以在任意地方通过现实 IDependencyRegistrar 接口进行注册组件。

Order属性代表注册优化级。

Register方法 提供了ContainerBuilder 用于组件注册的,还提供了 ITypeFinder 可用于动态加载程序并查找程序集中的类型,可用于动态加载插件。

IDependencyRegistrar 的实现 可以有多个,可放在项目的任意地方。

// Surf.Data.dll
public class DependencyRegistrar : IDependencyRegistrar
{
    public int Order => 1;

    public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
    {
        //泛型类型的注册
        builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
    }
}

// Surf.Service.dll
public class DependencyRegistrar2 : IDependencyRegistrar
{
    public int Order => 2; // or 1

    public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
    {
        builder.RegisterType<Service>().As<IService>().InstancePerLifetimeScope();
    }
}

组件注册更多参考:Autofac中文文档-组件注册

组件注册更多参考:Autofac英文文档-Registration Concepts

关于 生命周期

// Todo

组件注册时的 生命周期可参考:生命周期 or 实例作用域

应用启动

在应用程序启动时调用 EngineContext.Initialie();初始化注册,就可以再任意地方解析服务。

Main 的启动方式(使用于Console、Service)

static void Main(string[] args)
{
    EngineContext.Initialize();
}

.Net Framework 的启动方式

在 Global.cs 的 Application_Start() 中调用 EngineContext.Initialize()

Web和Api程序需要分别设置解析器,这里需要引用Autofac.Mvc5、Autofac.WebApi2

protected void Application_Start()
{
    // 初始化
    EngineContext.Initialize(ScopeTag.Http);           

    //用于 Web Mvc, 需引入 PM>  Install-Package Autofac.Mvc5
    System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(EngineContext.Scope));  
	     
    //用于Web Api,需引入 PM>  Install-Package Autofac.WebApi2
    System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(EngineContext.Scope);                
}

.Net Core 的启动方式

需要在 Startup.cs 的ConfigureServices方法中调用 EngineContext.Initialize(services);

并把 ConfigureServices 的返回类型 从 Void 改为 IServiceProvider

public void ConfigureServices(IServiceCollection services)

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

	// 必须要 return 用于 Only Api
	return EngineContext.Initialize(services, ScopeTag.Http);       
    //or  return EngineContext.Initialize(services);    //用于 Web / Api
}

应用启动参考:Application Startup

应用启动-英文参考:Application Startup

解析服务(获取对象)

可以在任意地方 使用 EngineContext.Resolve 方法来解析服务

EngineContext.Resolve<IService>();

如果要使用BeginLifetimeScope(),可以这样

using(var scope = EngineContext.BeginLifetimeScope())
{
   var service = scope.Resolve<IService>();
   var service2 = scope.Resolve<IService2>();
}

解析服务可以参考:解析服务

更多方式的应用

ITypeFinder 应用例子

使用ITypeFinder加载安全的IPlugin

写好的Plugin可以在需要时放到Bin目录下,通过ITypeFinder加载、执行。

public JsonResult LoadAllPlugins()
{
    // 动态加载插件
    var typeFinder = EngineContext.Resolve<ITypeFinder>();
    var type = typeFinder.FindClassesOfType(typeof(IPlugin)).FirstOrDefault();

    // 创建插件对象
    IPlugin plugin = null;
    if (!EngineContext.TryResolve(type, out object instance))
        instance = EngineContext.BeginLifetimeScope().ResolveUnregistered(type);

    plugin = instance as IPlugin;
    if (plugin == null)
    return Json(false);

    // 执行插件方法
    var executedResult = plugin.Execute();
    return Json(executedResult);
}

服务注册 方式

一般服务注册

// PerDependency
builder.RegisterType<UserService>().As<IUserService>().InstancePerDependency();

// PerLifetime
builder.RegisterType<Logger>().As<ILogger>().InstancePerLifetimeScope();

匹配后缀注册 typeEndName

// 注册 Services.dll 下所有 以Service结尾的服务。
// 如:AppRoleService、WorksheetService、
builder.RegisterAssemblyTypes(typeFinder, "Services.dll", typeEndName: "Service").AsImplementedInterfaces().InstancePerLifetimeScope();

泛型注册 RegisterGeneric

builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope(); // or SingleInstance()

单例服务的注册 SingleInstance

builder.RegisterType<CacheService>().As<ICacheService>().SingleInstance();

例子代码 可查看:Samples

NuGet:Autofac.Engine