English 中文(简体)
结构 地图组合
原标题:StructureMap configuration help

I have an interface, called IRepository, with two implementations:

  • SqlRepository

  • SqlDualWriter

第一项执行工作是定期实施T类物体。 它依赖SqlConnectionStringProvider(提供联系人,如名称所示,并作为建筑参数,使用一个显示姓名的链接)。

第二项是另一项在内部使用两种储存地:

public class SqlDualWriter<T> : IRepository<T>
{
    private readonly IRepository<T> _primaryRepository;
    private readonly IRepository<T> _secondaryRepository;

    public SqlDualWriter(
        IRepository<T> primaryRepository,
        IRepository<T> secondaryRepository)
    {
        _primaryRepository = primaryRepository;
        _secondaryRepository = secondaryRepository;
    iii

iii

我想要实现的是组合结构,以便在要求进行保存时,它将:

  1. resolve IRepository to an instance of SqlDualWriter
  2. resolve the two inner repositories to type SqlRepository
  3. 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 采用结构Map 2.6.2.0。

最佳回答

我找到了两种解决办法:

  1. • 利用斜线标出结构,说明如何建立SqlDualWriter 保存法院,解决托存的依赖:

    x.For<IRepository<Type>>().Use(
            ()=> new SqlDualWriterRepository<Type>(
                NewPrimaryRepositoryInstance<Type>(),
                NewSecondaryRepositoryInstance<Type>()));
    
  2. 采用“Ctor<”和“gt”明确指明每一建筑依赖者应如何即时:

    x.For<IRepository<Type>>().Use<SqlDualWriterRepository<Type>>()
            .Ctor<IRepository<Type>>("primaryRepository").Is(NewPrimaryRepositoryInstance<Type>())
            .Ctor<IRepository<Type>>("secondaryRepository").Is(NewSecondaryRepositoryInstance<Type>());
    

在上述两个例子中,新的遗产保存和新的第二代保存方法都建立了具有适当SqlConnectionStringProvider配置的初级和二级储存库。

实现这一点也许有更好的办法,但这些解决办法现在确实如此。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签