1、新建EXtension类库,添加包引用
2、反射注入Repository和Service
namespace DearlPlatform.Extensions{public static class ServiceCollectionExtensions{public static IServiceCollection RepositoryRegister(this IServiceCollection services){var asmCore = Assembly.Load("DealerPlatform.Core");var implementationType = asmCore.GetTypes().FirstOrDefault(m =>m.Name == "Repository`1");// var interfaceType = implementationType.GetInterfaces().FirstOrDefault();var interfaceType = implementationType.GetInterface("IRepository`1").GetGenericTypeDefinition();//视频里面是var interfaceType = implementationType.GetInterface("IRepository`1")//services.AddTransient(typeof(IRepository<>), implementationType);services.AddTransient(interfaceType, implementationType);return services;}public static IServiceCollection ServicesRegister(this IServiceCollection services){var asmCore = Assembly.Load("DearlPlatform.Service");var implementationTypes = asmCore.GetTypes().Where(m =>m.IsAssignableTo(typeof(IocTag))&&!m.IsAbstract&&m.IsInterface);foreach (var implementationType in implementationTypes){var interfaceType = implementationType.GetInterfaces().Where(m=>m!=typeof(IocTag));services.AddTransient(typeof(IRepository<>), implementationType);}return services;}}}
3、在startup里面注入
////注入仓储层//services.AddTransient(typeof(IRepository<>), typeof(Repository<>));services.RepositoryRegister();//注入服务处services.AddTransient<ICustomerService, CustomerService>();
