我有一个名为 IRepository 的接口,有两个实现:
-
SqlRepository
-
SqlDualWriterRepository
第一个实现是 T 类型对象的常规 SQL 持久性实现。它依赖于SqlConnectionStringProvider 的实例(顾名思义,它提供连接字符串,并将连接字符串名称作为构造函数参数)。
第二个是另一个在内部使用两个 SQLRepository 依赖项的实现:
public class SqlDualWriterRepository<T> : IRepository<T>
{
private readonly IRepository<T> _primaryRepository;
private readonly IRepository<T> _secondaryRepository;
public SqlDualWriterRepository(
IRepository<T> primaryRepository,
IRepository<T> secondaryRepository)
{
_primaryRepository = primaryRepository;
_secondaryRepository = secondaryRepository;
}
我
想要实现的是配置 StructureMap,以便在请求 IRepository 实例时,它将:
- 将 IRepository 解析为 SqlDualWriterRepository 的实例
- 将两个内部存储库解析
- 为 SqlRepository 类型在步骤 2 中实例化的两个 SqlRepository 对象我需要以不同的方式解析 SqlConnectionStringProvider (在构造函数中提供不同的字符串参数)
我不知道如何实现这一目标。
有没有办法通过属性或其他类型的配置来做到这一点?
我正在使用 StructureMap 2.6.2.0。
I have an interface, called IRepository, with two implementations:
-
SqlRepository
-
SqlDualWriterRepository
The first implementation is a regular SQL persistence implementation for objects of type T. It has a dependency on an instance of SqlConnectionStringProvider (which provides a connection string, as the name suggests and takes as constructor parameter a connection string name ).
The second is another implementation that uses internally two SQLRepository dependencies:
public class SqlDualWriterRepository<T> : IRepository<T>
{
private readonly IRepository<T> _primaryRepository;
private readonly IRepository<T> _secondaryRepository;
public SqlDualWriterRepository(
IRepository<T> primaryRepository,
IRepository<T> secondaryRepository)
{
_primaryRepository = primaryRepository;
_secondaryRepository = secondaryRepository;
}
}
What I want to achieve is configure StructureMap so that when asking for an IRepository instance, it will:
- resolve IRepository to an instance of SqlDualWriterRepository
- resolve the two inner repositories to type SqlRepository
- for the two SqlRepository objects instantiated at step 2 I need to resolve the SqlConnectionStringProvider in a different way (providing different string parameters in the constructor)
I have no idea how to achieve this.
Is there a way to do it with Attributes or other types of configuration?
I'm using StructureMap 2.6.2.0.
发布评论
评论(1)
我找到了两个解决方案:
使用 lambda 告诉 StructureMap 如何构建 SqlDualWriterRepository 实例来解决 IRepository 依赖关系:
使用 .Ctor<>() 显式指定为每个构造函数依赖关系实例化什么:
在上面的两个示例中,NewPrimaryRepositoryInstance() 和 NewSecondaryRepositoryInstance() 方法使用适当的值创建主 SqlRepositories 和辅助 SqlRepositories SqlConnectionStringProvider 配置。
可能有更好的方法来实现这一目标,但这些解决方案目前只能做到这一点。
I found two solutions:
Using a lambda to tell StructureMap how to build the SqlDualWriterRepository instance that will solve IRepository dependencies:
Using .Ctor<>() to explicitly specify what to instantiate for each constructor dependency:
In both examples above, the NewPrimaryRepositoryInstance() and NewSecondaryRepositoryInstance() methods create the primary and secondary SqlRepositories with the appropriate SqlConnectionStringProvider configurations.
There are probably better ways to achieve this, but these solutions do it for now.