public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); return services.AddLuna<AutofacModule>(); } public static IServiceProvider AddLuna<TModule>([NotNull]this IServiceCollection services) where TModule : IModule, new() { var builder = new ContainerBuilder(); builder.Populate(services); builder.RegisterModule<TModule>(); return new AutofacServiceProvider(builder.Build()); } public class AutofacModule : Module { protected override void Load(ContainerBuilder builder) { builder.RegisterType<TestContext>(); builder.RegisterGeneric(typeof(TestRepository<,>)).As(typeof(IRepository<,>)) .InstancePerLifetimeScope(); } }
这里的Module和IModule是autofac的,功能已经实现了,但是作为框架来说直接暴露了autofac的东西显然是不合适的,下一步要实现一个框架自身的模块加载方式.