简体   繁体   English

正确的n层应用程序引用方法

[英]Correct approach of referencing in n-layer application

I am a bit new to n-layer architecture and learning it by implementing a simple console app. 我对n层架构有点新意,并通过实现一个简单的控制台应用程序来学习它。

I have 3 projects : 我有3个项目:

DAL with Domain entities and DbContext class. 带有Domain实体和DbContext类的DAL。 BLL with Repository class. 带有Repository类的BLL。
Console Application just to run it. 控制台应用程序只是为了运行它

As all my Entities defined in DAL, BLL layer has reference to DAL and looks like: 由于我在DAL中定义的所有实体,BLL层都引用了DAL,如下所示:

public class DefaultRepository
{
    private DefaultDbContext _repository;

    private void SaveChanges()
    {
        try
        {
            _repository.SaveChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception were caught");
            Console.WriteLine(e.Message);
        }
    }
    public void AddPatient(Patient patient)
    {
        _repository.Patients.Add(patient);
        SaveChanges();
    }

    public Patient GetPatientById(int id)
        => _repository.Patients.Find(id) ?? null;

    public void AddVisit(int patientId, Visit visit)
    {
        GetPatientById(patientId)?.Visits.Add(visit);
        SaveChanges();
    }
    public DefaultRepository()
    {
        _repository = new DefaultDbContext();
    }
}

The obvious problem is that I cannot use repository in my console application project because console application has no reference to DAL level. 显而易见的问题是我无法在我的控制台应用程序项目中使用存储库,因为控制台应用程序没有引用DAL级别。 The following code occurs compile-time exception. 以下代码出现编译时异常。

DefaultRepository repository = new DefaultRepository();
repository.AddPatient(new Patient());

Of course, I can solve it just by adding reference to DAL in ConsoleApplication project. 当然,我可以通过在ConsoleApplication项目中添加对DAL的引用来解决它。 However, I understand, that absolutely destroy the n-layer conception. 但是,据我所知,绝对破坏了n层概念。 So, how should I manage this problem? 那么,我该如何处理这个问题呢? I googled something about using auto-mappers... 我搜索了一些关于使用自动映射器的信息......

Here I'm not directly giving solution to your use case.But I would like to give right path to implement sophisticated NLayer Architecture application. 在这里,我不是直接为您的用例提供解决方案。但我想提供实现复杂的NLayer架构应用程序的正确途径。

What is NLayer Architecture 什么是NLayer架构

Layering of an application's codebase is a widely accepted technique to help reduce complexity and improve code reusability. 分层应用程序的代码库是一种广泛接受的技术,有助于降低复杂性并提高代码的可重用性。 To achieve layered architecture, we can follows the principles of Domain Driven Design . 为了实现分层架构,我们可以遵循域驱动设计的原则。 In Domain Driven Design there are four fundamental layers: 在域驱动设计中,有四个基本层:

Presentation Layer: Provides an interface to the user. 表示层:为用户提供接口。 Uses the Application Layer to achieve user interactions. 使用Application Layer实现用户交互。

Application Layer: Mediates between the Presentation and Domain Layers. 应用层:在演示文稿和域层之间进行调解。 Orchestrates business objects to perform specific application tasks. 编排业务对象以执行特定的应用程序任务。

Domain Layer: Includes business objects and their rules. 域层:包括业务对象及其规则。 This is heart of the application. 这是应用程序的核心。

Infrastructure Layer: Provides generic technical capabilities that support higher layers. 基础架构层:提供支持更高层的通用技术功能。 An example of the Infrastructure Layer can be a Repository implementation used to interact with a database through an ORM framework, or an implementation for an email provider to send emails. 基础架构层的示例可以是用于通过ORM框架与数据库交互的存储库实现,或者是用于发送电子邮件的电子邮件提供商的实现。

There may be additional layers added as necessary. 必要时可能会添加其他图层。 An example being: 一个例子是:

Distributed Services Layer: Used to expose application features to remote clients. 分布式服务层:用于向远程客户端公开应用程序功能。 There are tools like ASP.NET Web API and WCF that can provide this layer. ASP.NET Web API和WCF等工具可以提供此层。 These are all common layers of a domain-centric architecture. 这些都是以域为中心的架构的常见层。 There may be minor differences based on implementation. 根据实施情况可能存在细微差别。

Overview of layers and structures are shown below: 层和结构概述如下所示:

在此输入图像描述

Here is a solution with five projects for a simple layered application: 这是一个包含五个项目的解决方案,用于简单的分层应用:

在此输入图像描述

If you would like to learn more about this,I highly recommend to study the below mentioned project.It's free and opensource . 如果您想了解更多相关信息,我强烈建议您研究下面提到的项目。它是免费的和开源的

ASP.NET Boilerplate ASP.NET Boilerplate

From wiki ( https://en.wikipedia.org/wiki/Multitier_architecture ): 来自wiki( https://en.wikipedia.org/wiki/Multitier_architecture ):

DAL "Encapsulates the persistence mechanisms and exposes the data". DAL“封装持久性机制并公开数据”。

Patient class is not a part of DAL but is the data that DAL exposes to upper level. 患者类不是DAL的一部分,而是DAL暴露给上层的数据。

I think there are 2 possible points of view: 我认为有两种可能的观点:

  1. DefaultRepository class should be not in BLL but in DAL. DefaultRepository类不应该在BLL中,而应在DAL中。 Your code in Console should be in BLL 您在Console中的代码应该是BLL
  2. DefaultRepository is a BLL and EntityFramework serves as a DAL. DefaultRepository是BLL,EntityFramework用作DAL。

I any case, your entity "Student" is just a piece of data that can be shared. 无论如何,您的实体“学生”只是一块可以共享的数据。

If you take more specific pattern like MVC: V (View) and C (Controller) knows about M (Model). 如果您采用更具体的模式,如MVC:V(View)和C​​(Controller)知道M(模型)。

To sum up, you can: 总而言之,您可以:

  1. create a 4th project that will be shared by all projects and will contain your entities (or interfaces for these entities). 创建一个将由所有项目共享的第四个项目,它将包含您的实体(或这些实体的接口)。
  2. Don't bother and add link from Console to DAL. 不要打扰并添加从Console到DAL的链接。 The most important that DAL doesn't have links to BLL and console. 最重要的是DAL没有链接到BLL和控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM