本文介绍了定义默认构造函数与StructureMap而不提供arguements或使用DefaultConstructor属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用StructureMap了一段时间,但现在我根本不是一个专家。我的问题很简单,我想通过code(注册)来配置SM创建一个存储库对象的实例时,使用特定的构造函数。

I've been using StructureMap for sometime now but I'm far from an expert. My problem is simple, I'm trying to configure SM via code (Registry) to use a particular constructor when creating an instance of a repository object.

下面是我的2构造(注既不是贪婪)。

Here are my 2 constructors (note neither is the greediest).

public BusinessUnitRepository( IDatabase database )
    : base( database ) {

}

public BusinessUnitRepository( IDatabaseFactory factory )
    : base( factory ) {

}

请注意:第一个构造函数的了IDatabase 接口的实例,并调用基类的构造函数(IDatabaseFactory)实施

Note: The first constructor takes an instance of the IDatabase interface and is called by the base class's ctor(IDatabaseFactory) implementation.

我想要做的就是配置SM使用第二个构造函数,并从SM容器提供的 DatabaseFactory 的实例。我无法使用 [DefaultConstructor] 属性在 BusinessUnitRepository 定义的组件所以这个选项是假表。

What I'm trying to do is configure SM to use the second constructor and provide an instance of DatabaseFactory from the SM container. I can't use the [DefaultConstructor] attribute in the assembly where BusinessUnitRepository is defined so this option is off the table.

我的注册表code

ForRequestedType<IDatabaseFactory>()
    .CacheBy( InstanceScope.PerRequest )
    .TheDefaultIsConcreteType<DatabaseFactory>();


ForRequestedType<Repository.IBusinessUnitRepository>()
    .CacheBy( InstanceScope.PerRequest )
    .TheDefault.Is.OfConcreteType<BusinessUnitRepository>().CtorDependency<IDatabaseFactory>().Is<DatabaseFactory>();

当我运行程序SM抛出试图创建一个实例 BusinessUnitRepository 当一个302错误。

When I run the program SM throws a 302 error when trying to create an instance of BusinessUnitRepository.

StructureMap.StructureMapException: StructureMap Exception Code:  302
There is no argument of type Repository.LinqToSql.IDatabaseFactory for concrete type Repository.LinqToSql.BusinessUnitRepository

供参考:

  • 如果我做的Repository.LinqToSql.BusinessUnitRepository集引用StructureMap并使用[DefaultConstructor]属性我IDatabaseFactory构造的一切完美的作品。
  • 此外,我已经证实,StructureMap确实包含一个配置Repository.LinqToSql.DatabaseFactory

推荐答案

我找到了一个解决方案,但魔力有臭味:

I found a solution but the 'magic' has a stink:

ForRequestedType<Repository.IBusinessUnitRepository>()
            .CacheBy( InstanceScope.PerRequest )
            .TheDefault.Is.OfConcreteType<BusinessUnitRepository>()
            .CtorDependency<IDatabaseFactory>("factory").IsTheDefault();

SelectConstructor<BusinessUnitRepository>( () => new BusinessUnitRepository((IDatabaseFactory)null ) );

我仍然认为这是一个更好的答案。因为我有相当多的资源库接口和具体实施这似乎是一个大量的工作来定义 SelectConstructor 每种类型。我打开的建议。

I still think there is a better answer. Since I have quite a few Repository interfaces and concrete implementation it seems like a lot of work to define the SelectConstructor for each type. I'm open to suggestions.

这篇关于定义默认构造函数与StructureMap而不提供arguements或使用DefaultConstructor属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:17