我正在ASP.NET Core中构建解决方案体系结构。
我引用了Web项目中的存储库,用于在ConfigureServices()
中进行声明依赖注入,您可以吗?
我认为理想情况是仅引用服务项目,因为控制器仅应使用服务而不使用存储库。
我有以下项目:
Web应用程序(ASP.NET Core)-引用所有项目。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddTransient<IEventsService, EventsService>();
services.AddTransient<IEventsRepository, EventsSqlRepository>();
}
public class EventsController : Controller
{
private readonly IEventsService _eventsService;
public EventsController(IEventsService eventsService)
{
_eventsService = eventsService;
}
}
业务(类库-.NET标准)
Folder IServices
IEventsService
Folder Services
public class EventsService : IEventsService
{
private readonly IEventsRepository _eventsRepository;
public EventsService(IEventsRepository eventsRepository)
{
_eventsRepository = eventsRepository;
}
}
IRepository(类库-.NET标准)
存储库(类库-.NET标准)
使用E.F访问BD。
public class EventsSqlRepository : BaseRepository, IEventsRepository
{
}
实用程序(类库-.NET标准)
实体(类库-.NET标准)
非常感谢!
答案 0 :(得分:0)
您的解决方案看起来不错。注册应用程序依赖项的部分称为组合根,它是应用程序中应注册所有依赖项的唯一位置。即使将其物理放置在应用程序的Web项目中,但从逻辑上讲,它还是应用程序的独立部分。您可以在这里了解更多信息:http://blog.ploeh.dk/2011/07/28/CompositionRoot/