如何解决 50+使用 Autofac DI 动态地具有相同接口的类

发布于 2024-12-19 06:36:33 字数 650 浏览 3 评论 0原文

我刚刚开始在大型生产项目中第一次使用 Autofac DI。我目前让它在 Asp.Net MVC3 中注册并解决“正常”情况。 我的意思是控制器、业务层、存储库等。

我的问题是我有 50 多个类,它们都具有相同的接口。我可以为每个类提供一个唯一的接口类型,但这种方法的问题是:

  1. 它非常乏味,因为所有类中的方法都完全相同
  2. 这似乎违反了所有 DRY 编码原则。

其用法是作为对要在视图模型中的 MVC3 自定义属性上实例化的类的引用。用法是:

[CustomAttribute(typeof(foo))]
Public string FooProperty {get;set;};

然后在自定义 MVC3 HTML Helper 方法中使用该属性,并从 [CustomAttribute(typeof(foo))] 属性。 Html.Helper 方法根据要实例化的类的名称来请求实例化该类。在将 DI 添加到应用程序之前,我们通过使用 System.Activator.CreateInstance 来完成此工作。

采取这种方法是因为它解决了应用程序中重用属性信息的其他部分的问题。

当我研究了多种方法来做到这一点时,我对采取正确的方法感到非常困惑。一些指导将不胜感激。

I've just starting using Autofac DI for the first time on a large production project. I currently have it do registration and resolving for "normal" situations in Asp.Net MVC3. By this, I mean controllers, business layer, repositories, etc.

My problem is that I have 50+ classes that all have the same interface. I could give each class a unique interface type, but the problems with that approach are:

  1. It is extremely tedious as the methods are all exactly the same across all classes
  2. This seems to violate all the DRY principles of coding.

The usage is as a reference to the class to be instantiated on a MVC3 Custom attribute in our view models. Usage is:

[CustomAttribute(typeof(foo))]
Public string FooProperty {get;set;};

This attribute is then used in a custom MVC3 HTML Helper method and gets the fully qualified type name (or can be a System.Type) to use from the [CustomAttribute(typeof(foo))] attribute. From the name of the class to instantiate, the Html.Helper method requests the class to be instantiated. Before we added DI to the application, we had this working by using System.Activator.CreateInstance.

This approach was taken because it solves problems in other parts of the application where the attribute information is reused.

When I've looked at a number of ways to do this and am seriously confused as to the correct approach to take. Some guidance would be appreciated.

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

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

发布评论

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

评论(1

南烟 2024-12-26 06:36:33

您不需要解析 Autofac 容器中的接口,您还可以解析具体的类。这取决于类型注册。您可以通过查找实现特殊接口的类型来批量注册 50 多个类,然后通过“self”注册这些类:

var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(assemblyWithClasses)
    .Where(type => type.IsAssignableTo<IYourSpecialInterface>())
    .AsSelf();
var container = builder.Build();

AsSelf 确保您能够通过类名进行解析,如下所示:

var type = Type.GetType("OneOfThe50PlusClasses");
var instance = container.Resolve(type);

You are not required to resolve interfaces from an Autofac container, you can also resolve concrete classes. It depends on the type registration. You could bulk register your 50+ classes by looking for types implementing your special interface, and then register these classes by "self":

var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(assemblyWithClasses)
    .Where(type => type.IsAssignableTo<IYourSpecialInterface>())
    .AsSelf();
var container = builder.Build();

AsSelf makes sure that you are able to resolve by the class name, like this:

var type = Type.GetType("OneOfThe50PlusClasses");
var instance = container.Resolve(type);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文