Structuremap 按对象类型使用不同的具体类型

发布于 2024-08-24 11:21:53 字数 524 浏览 3 评论 0原文

我在注册表中定义了一个默认的具体类型:

    ForRequestedType<IXRepository>()
        .TheDefaultIsConcreteType<CacheXRepository>();

ChaceXRepository 有以下构造函数:

public class CacheXRepository: IXRepository{

    public CacheXRepository(IXRepository xRepository,ICache cacheService){

在构造函数中,它接收一个与其自身具有相同接口类型的对象,但我想传递一个不同的具体类型。

如何在注册表中定义,如果类型是 CacheXRepository,那么我希望参数 IXRepository 的具体类型为 XRepository,在所有其他情况下 IXRepository 应解析为 CacheXRepository。

I have a default concrete type defined in a registry:

    ForRequestedType<IXRepository>()
        .TheDefaultIsConcreteType<CacheXRepository>();

The ChaceXRepository has the following constructor:

public class CacheXRepository: IXRepository{

    public CacheXRepository(IXRepository xRepository,ICache cacheService){

In the constructor it receives an object that has the same interface type as itself, but I want to pass in a different concrete type.

How do I define in the registry that if the type is a CacheXRepository then I want the concrete type for parameter IXRepository to be XRepository, in all other cases IXRepository should resolve to CacheXRepository.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风起 2024-08-31 11:21:53

您应该看一下丰富方法。然后代码将如下所示:

      ForRequestedType<IXRepository>().TheDefault.Is
            .OfConcreteType<XRepository>()
            .EnrichWith(
            (context, repository) =>
            new CacheXRepository(repository));

查看此页面以获取更多信息:http://codebetter.com/blogs/jeremy.miller/archive/2008/01/27/interception-techniques-in-structuralmap-2-5.aspx< /a>

You should take a look at the enrichwith method. Then the code will look something like:

      ForRequestedType<IXRepository>().TheDefault.Is
            .OfConcreteType<XRepository>()
            .EnrichWith(
            (context, repository) =>
            new CacheXRepository(repository));

Look at this page for more info: http://codebetter.com/blogs/jeremy.miller/archive/2008/01/27/interception-techniques-in-structuremap-2-5.aspx

安稳善良 2024-08-31 11:21:53

怎么样(不是测试):

ForRequestedType<IXRepository>()
  .TheDefaultIsConcreteType<CacheXRepository>()
  .WithCtorArg("xREpository")
  .EqualTo(new XRepository());

我不太熟悉 Structuremap 方言:)

在我使用的版本中它将是:

For<IXRepository>()
  .Use<CacheXRepository>()
  .CtorDependency<IXRepository>("xRepository")
  .IsConcreteType<XRepository>();

How about something like (not testet):

ForRequestedType<IXRepository>()
  .TheDefaultIsConcreteType<CacheXRepository>()
  .WithCtorArg("xREpository")
  .EqualTo(new XRepository());

Im not so familiar with that Structuremap dialect :)

In the version I am using it would be:

For<IXRepository>()
  .Use<CacheXRepository>()
  .CtorDependency<IXRepository>("xRepository")
  .IsConcreteType<XRepository>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文