【发布时间】:2019-04-27 12:19:45
【问题描述】:
我的应用程序包括表示层项目,它是 Web 服务器的项目。
在 PL 的 Startup 类中,我添加了一个 IServiceFacade 类型的单例服务。控制器将被注入它并使用它来与较低的服务层项目功能对话(PL 项目包含对 ServiceLayer 项目的引用)。
ServiceFacade 拥有一个 IBusinessLogicFacade 类型的对象,并使用它与较低的业务层项目对话(服务层项目持有对业务逻辑项目的引用)。
我还使用 .net Core 内置的日志 API 将 ILogger 注入到控制器、ServiceFacade 和 BusinessLogicFacade,这只需要引用 Microsoft.Extensions.Logging。
就像我将 ServiceFacade 添加为服务一样,从而启用了注入, 我想将 BusinessLogicFacade 添加为服务,但这需要 PL 项目对业务逻辑项目的引用,从而打破层分离。
我可以在 ServiceFacade 中“手动”创建 BusinessLogicFacade,但是我还需要提供 ILogger,因为在手动创建 BusinessLogicFacade 时我无法使用它的注入。
services.AddSingleton<IServiceFacade,ServiceFacade> (OK, PL holds a reference to ServiceLayer Project)
services.AddSingleton<IBusinessLogicFacade,BusinessLogicFacade> (Not OK, Requires a reference from PL to BL).
有没有办法在“手动”创建对象时通过注入接收一些参数(如 ILogger)? 我应该如何解决这个问题?
【问题讨论】:
标签: asp.net-core dependency-injection architecture n-tier-architecture