c# - 手动注册所有类(class)还是有自动方式?

标签 c# asp.net-mvc dependency-injection autofac asp.net-mvc-5.1

我正在使用 ASP.NET MVC 5。我是 AutoFac 的新手。我有很多课,每节课我都要做,

builder.RegisterType<AuthenticationService>().As<IAuthenticationService>();
builder.RegisterType<IAuthenticationRepositry>().As<AuthenticationRepositry>();
...............................................................................
...............................................................................

注册每种类型非常耗时,而且很容易忘记。有没有什么自动方法可以在 AutoFac 中注册组件?

最佳答案

如果你想自动注册所有类型作为它们的接口(interface),你可以使用RegisterAssemblyTypes:

builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).AsImplementedInterfaces();

或者AsSelf,如果你想解析具体的实现。

关于c# - 手动注册所有类(class)还是有自动方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22603973/

相关文章:

asp.net-mvc - ApiController 和 Controller 继承自同一个基类

不使用反射的 C# 字符串到对象

c# - 在 ASP.NET MVC3 模型绑定(bind)器中使用依赖注入(inject)

c# - Entity Framework : many to many relationship , 插入和更新

c# - 取消 NamedPipeClientStream.Read 调用

c# - 尝试 NHibernate 事务时捕获 SqlException

asp.net-mvc - MVC的实际应用||何时使用或不使用 MVC

c# - 如何从 C# 调用带有结构指针参数的 C++ 函数?

c# - 如何定义类内封装属性的导航?

c# - 如何确保注入(inject)的属性更新其在 IoC 中的引用,以使所有依赖的 ViewModel 具有相同的属性实例是最新的