class 个库中的 DryIoC
DryIoC in class libraries
我有一个相当简单的解决方案设置:
Web 项目 --> 服务层 --> 存储库
我的 IoCbootstrapper class 位于我的网络项目中。
var c = new Container();
c.Register<IUserService, UserService>();
c.Register<ILoggingService, LoggingService>();
很好,但我的服务取决于存储库。
有没有办法在服务层 class 库中有一个 "parital bootsrapper",然后以某种方式将其作为配置传递给 Web 项目,因为这样就无法创建服务实例web 项目不知道存储库?
让服务层公开一个获取容器并注册所需内容的操作。
引用存储库的服务层
namespace Project.ServiceLayer {
public static class DryIocExtesnions {
public static void AddServiceLayer(this IContainer c) {
c.Register<IUserRepository, UserRepository>();
c.Register<IOtherRepository, OtherRepository>();
}
}
}
IoCbootstrapper 位于 web 项目中。
var c = new Container();
c.Register<IUserService, UserService>();
c.Register<ILoggingService, LoggingService>();
//Register service layer
c.AddServiceLayer();
退一步说,这取决于你的偏好,现在服务层必须意识到来自组合根的关注。您可以将 抽象到您自己的容器中,但这可能有点矫枉过正。
也许你可以使用 container.WithAutoFallbackResolution
with repositories assemblies, or some variant of container.RegisterMany
我有一个相当简单的解决方案设置:
Web 项目 --> 服务层 --> 存储库
我的 IoCbootstrapper class 位于我的网络项目中。
var c = new Container();
c.Register<IUserService, UserService>();
c.Register<ILoggingService, LoggingService>();
很好,但我的服务取决于存储库。
有没有办法在服务层 class 库中有一个 "parital bootsrapper",然后以某种方式将其作为配置传递给 Web 项目,因为这样就无法创建服务实例web 项目不知道存储库?
让服务层公开一个获取容器并注册所需内容的操作。
引用存储库的服务层
namespace Project.ServiceLayer {
public static class DryIocExtesnions {
public static void AddServiceLayer(this IContainer c) {
c.Register<IUserRepository, UserRepository>();
c.Register<IOtherRepository, OtherRepository>();
}
}
}
IoCbootstrapper 位于 web 项目中。
var c = new Container();
c.Register<IUserService, UserService>();
c.Register<ILoggingService, LoggingService>();
//Register service layer
c.AddServiceLayer();
退一步说,这取决于你的偏好,现在服务层必须意识到来自组合根的关注。您可以将 抽象到您自己的容器中,但这可能有点矫枉过正。
也许你可以使用 container.WithAutoFallbackResolution
with repositories assemblies, or some variant of container.RegisterMany