In Autofac, you can register your dependencies with RegisterAssemblyTypes so you can do something like this, is there a way to do something like this in building in DIfor.net Core
builder.RegisterAssemblyTypes(Assembly.Load("SomeProject.Data"))
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
This is what I'm trying to register
LeadService.cs
public class LeadService : ILeadService
{
private readonly ILeadTransDetailRepository _leadTransDetailRepository;
public LeadService(ILeadTransDetailRepository leadTransDetailRepository)
{
_leadTransDetailRepository = leadTransDetailRepository;
}
}
LeadTransDetailRepository.cs
public class LeadTransDetailRepository : RepositoryBase<LeadTransDetail>,
ILeadTransDetailRepository
{
public LeadTransDetailRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory) { }
}
public interface ILeadTransDetailRepository : IRepository<LeadTransDetail> { }
This is how I try to register with Regisyer, but I cannot figure out how to register Startup.cs repositories
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddTransient<ILeadService, LeadService>();
services.Add(new ServiceDescriptor(typeof(ILeadTransDetailRepository),
typeof(IRepository<>), ServiceLifetime.Transient));
services.Add(new ServiceDescriptor(typeof(IDatabaseFactory),
typeof(DatabaseFactory), ServiceLifetime.Transient));
services.AddTransient<DbContext>(_ => new DataContext(
this.Configuration["Data:DefaultConnection:ConnectionString"]));
}
source
share