If I use the Repository Pattern in an ASP.NET MVC Application I need DI to let the program know, to interface the classes must be mapped. If I implement Unity I need to add the DAL project to my MVC project, and then register the types in the global.asax.
如果我在ASP.NET MVC应用程序中使用存储库模式,我需要DI让程序知道,必须映射类的接口。如果我实现Unity,我需要将DAL项目添加到我的MVC项目,然后在global.asax中注册类型。
In my mind, I think it's bad to add the namespace of the DAL Layer to the MVC project, there is a business layer also in between. I think, it would be beautiful to inject the DAL classes in the business layer and only the business layer mappings in the MVC app.
在我看来,我认为将DAL Layer的命名空间添加到MVC项目是不好的,中间还有一个业务层。我认为,在业务层中注入DAL类并且只在MVC应用程序中注入业务层映射会很美妙。
What's the way to go here? Do you have suggestions?
怎么去这里?你有什么建议吗?
UPDATE: To make it clear to me. In the service layer, there are only DTO's and the DI for the business and data access layer. In the service layer I map the DTOs to the domain model. What I don't understand is, how can I call the business layer methods then?
更新:向我说清楚。在服务层中,只有DTO和业务和数据访问层的DI。在服务层中,我将DTO映射到域模型。我不明白的是,我怎样才能调用业务层方法呢?
3 个解决方案
#1
3
Even if you don't use a distinct service layer, you can accomplish what you want, which is to decouple the MVC application from the DAL project using DI.
即使您不使用不同的服务层,您也可以完成所需的操作,即使用DI将MVC应用程序与DAL项目分离。
The way to do this is to add a couple of projects/assemblies in between that wires up your IoC container with specific instances of the interfaces you have defined.
这样做的方法是在两者之间添加一些项目/程序集,将IoC容器与您定义的接口的特定实例连接起来。
I typically use this naming convention:
我通常使用这个命名约定:
MyCompany.MyProject.Infrastructure
MyCompany.MyProject.Abstract
Your main MVC project would then have a reference to your Abstract and Infrastructure projects. Your Infrastructure project would have a reference to the Abstract and instance specific projects like the Business and DAL projects. Within Infrastructure project you wire up the dependencies.
然后,您的主要MVC项目将引用您的抽象和基础结构项目。您的基础结构项目将引用抽象和特定于实例的项目,如Business和DAL项目。在基础结构项目中,您可以连接依赖项。
You'll have to setup a mechanism for your MVC project to bootstrap your IoC in the Infrastructure assembly. You can do that in your global.asax or as an App_Start method and call a Registration class within your Infrastructure assembly.
您必须为MVC项目设置一种机制,以便在基础架构程序集中引导IoC。您可以在global.asax中或作为App_Start方法执行此操作,并在基础结构程序集中调用Registration类。
We use StructureMap, but the concept is the same. Here's some sample code.
我们使用StructureMap,但概念是一样的。这是一些示例代码。
In your MVC App, create a App_Start method to setup the DI.
在您的MVC应用程序中,创建一个App_Start方法来设置DI。
public static class StructuremapMvc
{
public static void Start()
{
// Create new Structuremap Controller factory so Structure map can resolve the parameter dependencies.
ControllerBuilder.Current.SetControllerFactory(new StructuremapControllerFactory());
IContainer container = IoC.Initialize();
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
}
}
In your Infrastructure assembly, wire up the dependencies.
在基础结构程序集中,连接依赖项。
public static class IoC
{
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IRepositoryNum1>().Use<Num1Repository>();
x.For<IRepositoryNum2>().Use<Num2Repository>();
x.For<IRepositoryNum3>().Use<Num3Repository>();
});
return ObjectFactory.Container;
}
}
#2
4
If you want to be pragmatic, a true 3-tier architecture requires a service layer. Between the service and MVC are Data Transfer Objects (DTOs). The service layer hides both the DAL and the business layer.
如果您想要务实,真正的3层架构需要服务层。服务和MVC之间是数据传输对象(DTO)。服务层隐藏了DAL和业务层。
If you set it up like this, the MVC itself knows nothing about DAL, only DTOs and Service (contracts).
如果你这样设置,MVC本身对DAL一无所知,只知道DTO和服务(合同)。
#3
0
You should use DI to inject the Domain/DAL interfaces into your constructors. This has a lot of upside including allowing you to moq your interfaces when you write your unit tests. You can use Autofac to handle the injection.
您应该使用DI将Domain / DAL接口注入构造函数。这有很多好处,包括允许您在编写单元测试时使用moq接口。您可以使用Autofac来处理注射。
#1
3
Even if you don't use a distinct service layer, you can accomplish what you want, which is to decouple the MVC application from the DAL project using DI.
即使您不使用不同的服务层,您也可以完成所需的操作,即使用DI将MVC应用程序与DAL项目分离。
The way to do this is to add a couple of projects/assemblies in between that wires up your IoC container with specific instances of the interfaces you have defined.
这样做的方法是在两者之间添加一些项目/程序集,将IoC容器与您定义的接口的特定实例连接起来。
I typically use this naming convention:
我通常使用这个命名约定:
MyCompany.MyProject.Infrastructure
MyCompany.MyProject.Abstract
Your main MVC project would then have a reference to your Abstract and Infrastructure projects. Your Infrastructure project would have a reference to the Abstract and instance specific projects like the Business and DAL projects. Within Infrastructure project you wire up the dependencies.
然后,您的主要MVC项目将引用您的抽象和基础结构项目。您的基础结构项目将引用抽象和特定于实例的项目,如Business和DAL项目。在基础结构项目中,您可以连接依赖项。
You'll have to setup a mechanism for your MVC project to bootstrap your IoC in the Infrastructure assembly. You can do that in your global.asax or as an App_Start method and call a Registration class within your Infrastructure assembly.
您必须为MVC项目设置一种机制,以便在基础架构程序集中引导IoC。您可以在global.asax中或作为App_Start方法执行此操作,并在基础结构程序集中调用Registration类。
We use StructureMap, but the concept is the same. Here's some sample code.
我们使用StructureMap,但概念是一样的。这是一些示例代码。
In your MVC App, create a App_Start method to setup the DI.
在您的MVC应用程序中,创建一个App_Start方法来设置DI。
public static class StructuremapMvc
{
public static void Start()
{
// Create new Structuremap Controller factory so Structure map can resolve the parameter dependencies.
ControllerBuilder.Current.SetControllerFactory(new StructuremapControllerFactory());
IContainer container = IoC.Initialize();
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
}
}
In your Infrastructure assembly, wire up the dependencies.
在基础结构程序集中,连接依赖项。
public static class IoC
{
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.For<IRepositoryNum1>().Use<Num1Repository>();
x.For<IRepositoryNum2>().Use<Num2Repository>();
x.For<IRepositoryNum3>().Use<Num3Repository>();
});
return ObjectFactory.Container;
}
}
#2
4
If you want to be pragmatic, a true 3-tier architecture requires a service layer. Between the service and MVC are Data Transfer Objects (DTOs). The service layer hides both the DAL and the business layer.
如果您想要务实,真正的3层架构需要服务层。服务和MVC之间是数据传输对象(DTO)。服务层隐藏了DAL和业务层。
If you set it up like this, the MVC itself knows nothing about DAL, only DTOs and Service (contracts).
如果你这样设置,MVC本身对DAL一无所知,只知道DTO和服务(合同)。
#3
0
You should use DI to inject the Domain/DAL interfaces into your constructors. This has a lot of upside including allowing you to moq your interfaces when you write your unit tests. You can use Autofac to handle the injection.
您应该使用DI将Domain / DAL接口注入构造函数。这有很多好处,包括允许您在编写单元测试时使用moq接口。您可以使用Autofac来处理注射。