TornLifestyle 简易注射器
TornLifestyle simple injector
我定义了这样一个通用接口:
public interface IPlanManagmentRepository<TEntity> where TEntity:class
当我这样定义具体实现时:
public class ProviderPlanManagementRepo : IPlanManagmentRepository<Provider>
一切都通过像这样注册接口来工作:
container.Register(typeof(IPlanManagmentRepository<>),
new [] { typeof(IPlanManagmentRepository<>).Assembly},
Lifestyle.Scoped);
但是,如果这个 class 也处理更多的东西并且我添加一个额外的接口:
public interface IProviderPlanManagementRepo
{
void doSomethingSpecificToProviderHere();
}
public class ProviderPlanManagementRepo : IProviderPlanManagementRepo,
IPlanManagmentRepository<Provider>
{
}
然后我得到这个错误:
-[Torn Lifestyle] The registration for IPlanManagmentRepository maps to the same implementation and lifestyle as the registration for IProviderPlanManagementRepo does. They both map to ProviderPlanManagementRepo
我也试过在IProviderPlanManagementRepo中继承IPlanManagmentRepository,也出现同样的错误
这个 class 是否应该只处理通用接口的实现?
或者可以用简单的注射器来完成这个?
更新:
随着 Simple Injector 4 的引入,在大多数情况下,容器将阻止为同一具体类型创建多个注册实例。因此 Torn Lifestyles 警告类型应该极为罕见。只有当自定义生活方式绕过 Lifestyle.CreateRegistration 重载的缓存行为时,才会发生撕裂的生活方式。
您的问题与此 work item and this discussion. Typically, torn lifestyles can be fixed as follows 有关,但是当使用批注册注册具有多个不相关接口的类型时,Simple Injector 3.1 使得修复违规变得异常困难。这是我们将在即将发布的次要版本之一中解决的问题。
我现在可以推荐的最简单的解决方法是使您的 IPlanManagmentRepository<T>
注册成为临时注册。你应该能够让它们成为瞬态的,因为你的组件应该是不可变的。所以通常只有 DbContext
应该是 Scoped
,但你甚至可能不想将 DbContext
注入你的存储库,因为 DbContext
是运行时数据,运行时数据应该not be injected into the components of your object graph.
我定义了这样一个通用接口:
public interface IPlanManagmentRepository<TEntity> where TEntity:class
当我这样定义具体实现时:
public class ProviderPlanManagementRepo : IPlanManagmentRepository<Provider>
一切都通过像这样注册接口来工作:
container.Register(typeof(IPlanManagmentRepository<>),
new [] { typeof(IPlanManagmentRepository<>).Assembly},
Lifestyle.Scoped);
但是,如果这个 class 也处理更多的东西并且我添加一个额外的接口:
public interface IProviderPlanManagementRepo
{
void doSomethingSpecificToProviderHere();
}
public class ProviderPlanManagementRepo : IProviderPlanManagementRepo,
IPlanManagmentRepository<Provider>
{
}
然后我得到这个错误:
-[Torn Lifestyle] The registration for IPlanManagmentRepository maps to the same implementation and lifestyle as the registration for IProviderPlanManagementRepo does. They both map to ProviderPlanManagementRepo
我也试过在IProviderPlanManagementRepo中继承IPlanManagmentRepository,也出现同样的错误
这个 class 是否应该只处理通用接口的实现? 或者可以用简单的注射器来完成这个?
更新:
随着 Simple Injector 4 的引入,在大多数情况下,容器将阻止为同一具体类型创建多个注册实例。因此 Torn Lifestyles 警告类型应该极为罕见。只有当自定义生活方式绕过 Lifestyle.CreateRegistration 重载的缓存行为时,才会发生撕裂的生活方式。
您的问题与此 work item and this discussion. Typically, torn lifestyles can be fixed as follows 有关,但是当使用批注册注册具有多个不相关接口的类型时,Simple Injector 3.1 使得修复违规变得异常困难。这是我们将在即将发布的次要版本之一中解决的问题。
我现在可以推荐的最简单的解决方法是使您的 IPlanManagmentRepository<T>
注册成为临时注册。你应该能够让它们成为瞬态的,因为你的组件应该是不可变的。所以通常只有 DbContext
应该是 Scoped
,但你甚至可能不想将 DbContext
注入你的存储库,因为 DbContext
是运行时数据,运行时数据应该not be injected into the components of your object graph.