为什么网络上所有 asp.net 个 mvc 示例都引用 UI 中的 DAL

Why all asp.net mvc samples on the net reference the DAL in UI

可能是我弄错了,但在我工作过的所有公司中,我们从未在 UI 层(winforms-Web)中添加对 Dal 的引用 我是网络新手,但是经验丰富的 winform-wcf 开发人员,但是我正在努力解决在 UI.

中引用 dal 的概念

在一个没有适当监督的大团队中是灾难的根源。

为什么不能有 2 个合成根

 1)WebUI just services classes (not wcf) when using wcf composition root is in there

 2)within the services that we inject there the repository.

我错过了显而易见的东西吗?

谢谢

已更新

在我看到的所有示例中,它们都使用 DI(Unity-Autofac-Nject 等),并且它们通过接口连接存储库,这也需要具体的 class,因此引用了DAL.Can这个在service层做?

有一种方法可以在不从 UI 引用数据访问层的情况下注入依赖项。您必须添加一个横向 Utils 或 Helpers 项目来解决项目中的所有依赖关系。

在此说明中,我将使用 Unity 进行 DI。我将通过几个步骤向您介绍它:

首先,创建一个Utils 项目。该项目将横向于所有层。使用 IUnityContainer 的扩展方法添加此静态 class:

// This class will resolve all dependencies across the project
public static class DependencyResolver
{
    // This method will resolve all dependencies
    public static void ResolveDependencies(this IUnityContainer container)
    {
        container.ResolveBusinessLayerDependencies();
        container.ResolveDataAccessLayerDependencies();
    }
}

这将强制您创建两种方法:容器中的 ResolveBusinessLayerDependencies 和 ResolveDataAccessLayerDependencies。

其次,你应该去你的业务逻辑项目(在这里,你应该能够访问这一层的接口和实现)。像这样添加一个新的静态 class:

public static class DependencyResolver
{
    public static void ResolveBusinessLayerDependencies(this IUnityContainer container)
    {
        container.RegisterType<IBusinessLayerClass, BusinessLayerClass>();
    }
}

对您的数据访问项目执行相同的操作:

public static class DependencyResolver
{
    public static void ResolveDataAccessLayerDependencies(this IUnityContainer container)
    {
        container.RegisterType<IDataAccessLayerClass, DataAccessLayerClass>();
    }
}

现在在 UnityConfig.cs 中,RegisterTypes 方法类似于:

public static void RegisterTypes(IUnityContainer container)
{
      container.ResolveDependencies();
}

这是我发现在不引用主项目的所有层的情况下使用 DI 的一种有用方法