English 中文(简体)
选择内部助手班,同时解决公共阶层的问题
原标题:Injecting an internal helper class while resolving a public class

我有以下结构,在另一个大会中设有一个公务员班,称为内部助手班:

ApplicationAssembly {
  public class Widget {
    public Widget(ReferencedAssembly.Service service) { ... }
  }
}

ReferencedAssembly {
  public class Service {
    public Service(Helper helper) { ... }
  }
  class Helper { ... }
}

(我认识到,我可以不把一个内部阶级放在一个公共阶层的构造者——Im的论点中,只是试图说明IoC的模式,Im在之后。)

The problem is that ApplicationAssembly can t see ReferencedAssembly.Helper, so it can t be registered in my IoC container (Autofac in this case). As a result, Helper cannot be resolved when I try to resolve Service. What is my best option here?

  • 备选案文1:删除<代码>Helper>,从 > 处到构造,并在构造中明确加以更新。 我不喜欢这一选择,因为这种做法打破了国际奥委会的模式。

  • 备选案文2:制作<代码>Helper,实施公共<代码>IHelper接口,然后在内添加公共模块。 登记<代码>Helper的参考大会。 我不喜欢这一选择,因为它要求<代码>ApplicationAssembly了解太多的执行细节-service,而且如果用户忘记在启动时登记该模块,则每次都会中断。

  • Option 3: Create a public static constructor on Service that builds a 2nd IoC container specifically for ReferencedAssembly and registers Helper in it. Remove Helper from Service s constructor and resolve it within the constructor using the 2nd IoC container. This seems like my best option but requires more "plumbing" code than the others. I m also not a big fan of public static constructors.

  • 备选办法4. 将我的架构完全改为其他内容。

最佳回答

登记内部类型的方法是,在装配中包含一个内部类型,并在容器上登记单元。 由于该模块是同一组装的,因此可以将<代码>ContainerBuilder与内部类型混为一谈。

我认为,最佳选择是使<代码>服务<>/代码>实施公共接口,然后使<代码>服务<>/代码>类别成为你目前内部的类别。 Autofac将停在内部,提供公共接口。

另一种选择(允许<条码>服务<>/条码>对内部类型进行构造或依赖”是指在内部构造,然后在<条码<>服务/代码>上使用建筑商:

builder.RegisterType<Service>()
  .FindConstructorsWith(new BindingFlagsConstructorFinder(BindingFlags.NonPublic));
问题回答

暂无回答




相关问题
SQL Server - How many users do I *really* need?

I m setting up an application, and I m looking into purchasing a license for SQL Server. My question is pretty simple (though may have a complicated answer...) How many users accounts do I really ...

Object Graph / Persistence Question

I m currently working with an online application that requires multiple screens/steps in order to complete. The application domain is modeled using an object graph. Persistence to and from a database ...

Why does stack get truncated in Exception.StackTrace?

Why does the high part of the stack (in Exception.StackTrace) gets truncated? Let s see a simple example: public void ExternalMethod() { InternalMethod(); } public void InternalMethod() { try ...

ASP.NET MVC and Service Oriented Architecture

I would like to know how do i incorporate a feature like wcf within and MVC application. My current idea of the architecture is as follows: EntityFramework -> ASP.NET MVC (Views) ...

热门标签