这是 n 层架构的正确实现吗?
我在过去一年左右的时间里一直在学习 C#,并尝试在此过程中融入最佳实践。在 StackOverflow 和其他网络资源之间,我认为我处于正确分离我的关注点的正确轨道上,但现在我有一些疑问,并希望在将整个网站转换为这个新网站之前确保我走的是正确的道路。建筑学。
当前的网站是旧的 ASP VBscript,并且现有的数据库非常丑陋(没有外键等),因此至少对于 .NET 中的第一个版本,我此时不想使用并且必须学习任何 ORM 工具。
我有以下项目位于单独的命名空间和设置中,以便 UI 层只能看到 DTO 和业务层,而数据层只能从业务层看到。这是一个简单的示例:
productDTO.csproductBLL.csproductDAL.cs
public class ProductDTO
{
public int ProductId { get; set; }
public string Name { get; set; }
public ProductDTO()
{
ProductId = 0;
Name = String.Empty;
}
}
在我的 UI中
public class ProductBLL
{
public ProductDTO GetProductByProductId(int productId)
{
//validate the input
return ProductDAL.GetProductByProductId(productId);
}
public List<ProductDTO> GetAllProducts()
{
return ProductDAL.GetAllProducts();
}
public void Save(ProductDTO dto)
{
ProductDAL.Save(dto);
}
public bool IsValidProductId(int productId)
{
//domain validation stuff here
}
}
我
public class ProductDAL
{
//have some basic methods here to convert sqldatareaders to dtos
public static ProductDTO GetProductByProductId(int productId)
{
ProductDTO dto = new ProductDTO();
//db logic here using common functions
return dto;
}
public static List<ProductDTO> GetAllProducts()
{
List<ProductDTO> dtoList = new List<ProductDTO>();
//db logic here using common functions
return dtoList;
}
public static void Save(ProductDTO dto)
{
//save stuff here
}
}
,我会执行以下操作:
ProductBLL productBll = new ProductBLL();
List<ProductDTO> productList = productBll.GetAllProducts();
为了保存:
ProductDTO dto = new ProductDTO();
dto.ProductId = 5;
dto.Name = "New product name";
productBll.Save(dto);
完全偏离基地了吗?我是否应该在 BLL 中具有相同的属性而不将 DTO 传回我的 UI?请告诉我什么是错的,什么是对的。请记住,我还不是专家。
我想为我的架构实现接口,但我仍在学习如何做到这一点。
I have been learning C# for the last year or so and trying to incorporate best practices along the way. Between StackOverflow and other web resources, I thought I was on the right track to properly separating my concerns, but now I am having some doubts and want to make sure I am going down the right path before I convert my entire website over to this new architecture.
The current website is old ASP VBscript and has a existing database that is pretty ugly (no foreign keys and such) so at least for the first version in .NET I do not want to use and have to learn any ORM tools at this time.
I have the following items that are in separate namespaces and setup so that the UI layer can only see the DTOs and Business layers, and the Data layer can only be seen from the Business layer. Here is a simple example:
productDTO.cs
public class ProductDTO
{
public int ProductId { get; set; }
public string Name { get; set; }
public ProductDTO()
{
ProductId = 0;
Name = String.Empty;
}
}
productBLL.cs
public class ProductBLL
{
public ProductDTO GetProductByProductId(int productId)
{
//validate the input
return ProductDAL.GetProductByProductId(productId);
}
public List<ProductDTO> GetAllProducts()
{
return ProductDAL.GetAllProducts();
}
public void Save(ProductDTO dto)
{
ProductDAL.Save(dto);
}
public bool IsValidProductId(int productId)
{
//domain validation stuff here
}
}
productDAL.cs
public class ProductDAL
{
//have some basic methods here to convert sqldatareaders to dtos
public static ProductDTO GetProductByProductId(int productId)
{
ProductDTO dto = new ProductDTO();
//db logic here using common functions
return dto;
}
public static List<ProductDTO> GetAllProducts()
{
List<ProductDTO> dtoList = new List<ProductDTO>();
//db logic here using common functions
return dtoList;
}
public static void Save(ProductDTO dto)
{
//save stuff here
}
}
In my UI, I would do something like this:
ProductBLL productBll = new ProductBLL();
List<ProductDTO> productList = productBll.GetAllProducts();
for a save:
ProductDTO dto = new ProductDTO();
dto.ProductId = 5;
dto.Name = "New product name";
productBll.Save(dto);
Am I completely off base? Should I also have the same properties in my BLL and not pass back DTOs to my UI? Please tell me what is wrong and what is right. Keep in mind I am not a expert yet.
I would like to implement interfaces to my architecture, but I am still learning how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
凯德有很好的解释。为了避免贫血域模型,您可以考虑做一些事情:
Cade has a good explination. In order to avoid the Anemic domain model some things you could consider doing:
您想要考虑添加的内容:验证、属性更改通知、数据绑定、
等等...将每个类分成多个类(DAL、BLL 等...)时的一个常见问题是,您通常会得到需要复制的大量代码。另一个问题是,如果您需要这些类之间的某种亲密关系,则必须创建内部成员(接口、字段等)。
这就是我要做的,构建一个独特的一致域模型,如下所示:
Things you want to consider adding: validation, property change notification, data binding,
etc... One common issue when separating each class in multiple classes (DAL, BLL, etc...) is often you end up with a lot of code you need to duplicate. Another problem is if you need some intimacy between those classes, you'll have to create internal members (interfaces, fields, etc.)
This is what I would do, build a unique consistent Domain Model, something like this:
贫乏域是指产品或其他类除了数据设置器和获取器之外实际上没有实现任何其他内容 - 没有域行为。
例如,产品域对象应该公开一些方法、一些数据验证、一些实际业务逻辑。
否则,BLL 版本(域对象)并不比 DTO 好。
http://martinfowler.com/bliki/AnemicDomainModel.html
这里的问题是你是- 假设您的模型贫乏并将 DTO 暴露给业务层消费者(UI 或其他)。
您的应用程序代码通常希望与
一起使用,而不是任何 BLL 或 DTO 或其他任何东西。这些是实现类。它们不仅对应用程序程序员的思维水平毫无意义,而且对表面上了解问题领域的领域专家也毫无意义。因此,如果您明白我的意思,它们应该只在您在管道工作时可见,而不是在您设计浴室时可见。我将 BLL 对象命名为业务域实体的名称。而DTO是内部的业务实体和DAL之间的。当域实体除了 DTO 之外不做任何事情时,那就是它贫乏的时候。
另外,我还要补充一点,我经常忽略显式 DTO 类,并让域对象转到通用 DAL,并在配置中定义有组织的存储过程,并将其自身从普通的旧数据读取器加载到其属性中。通过闭包,现在可以拥有非常通用的带有回调的 DAL,让您可以插入参数。
我会坚持使用可能可行的最简单的方法:
然后您可以将明显的部分移到 DAL 中。 DataRecords 充当您的 DTO,但它们的寿命非常短暂 - 它们的集合从未真正存在过。
这是 SqlServer 的静态 DAL.Retrieve(您可以看到它很简单,可以将其更改为使用 CommandText);我有一个版本,它封装了连接字符串(因此它不是静态方法):
稍后您可以继续使用完整的框架。
Anemic domain is when a product or other class doesn't really implement anything more than data setters and getters - no domain behavior.
For instance, a product domain object should have some methods exposed, some data validations, some real business logic.
Otherwise, the BLL version (the domain object) is hardly better than a DTO.
http://martinfowler.com/bliki/AnemicDomainModel.html
The problem here is that you are pre-supposing your model is anemic and exposing the DTO to the business layer consumers (the UI or whatever).
Your application code generally wants to be working with
<Product>
s, not any BLL or DTO or whatever. Those are implementation classes. They not only mean little to the application programmer level of thought, they mean little to domain experts who ostensibly understand the problem domain. Thus they should only be visible when you are working on the plumbing, not when you are designing the bathroom, if you see what I mean.I name my BLL objects the name of the business domain entity. And the DTO is internal between the business entity and the DAL. When the domain entity doesn't do anything more than the DTO - that's when it's anemic.
Also, I'll add that I often just leave out explcit DTO classes, and have the domain object go to a generic DAL with organized stored procs defined in the config and load itself from a plain old datareader into its properties. With closures, it's now possible to have very generic DALs with callbacks which let you insert your parameters.
I would stick to the simplest thing that can possibly work:
You can then move the obvious parts out into a DAL. The DataRecords serve as your DTO, but they are very short-lived - a collection of them never really exists.
Here's a DAL.Retrieve for SqlServer which is static (you can see it's simple enough to change it to use CommandText); I have a version of this which encapsulates the connection string (and so it's not a static method):
Later you can move on to full blown frameworks.
其他人对于使用 ORM 的看法是——随着模型的扩展,如果没有 ORM,您将会有大量的代码重复。但我想评论一下你的“5000 怎么样”的问题。
复制类不会创建其方法的 5,000 个副本。它仅创建数据结构的副本。将业务逻辑放在域对象中不会损失效率。如果某些业务逻辑不适用,那么您可以创建子类来装饰该对象以用于特定目的,但这样做的目的是创建符合您的预期用途的对象,而不是效率。贫乏的设计模型并没有效率更高。
另外,请考虑如何在应用程序中使用数据。我想不出我曾经使用过像“GetAllOfSomething()”这样的方法,除了参考列表之外。检索数据库中的所有内容的目的是什么?如果要执行某些流程、数据操作、报告,您应该公开执行该流程的方法。如果您需要公开列表以供外部使用(例如填充网格),则公开 IEnumerable 并提供对数据进行子集化的方法。如果您一开始就认为要处理内存中的完整数据列表,那么随着数据的增长,您将遇到严重的性能问题。
What others have said about using an ORM - as your model expands, you are going to have a lot of code repetition without one. But I wanted to comment on your "what about 5,000" question.
Copying a class does not create 5,000 copies of its methods. It only creates a copy of the data structures. There is no lost efficiency to having the business logic in the domain object. If some of the business logic is not applicable, then you could create subclasses that decorate the object for specific purposes, but the purpose of this is to create objects that match your intended use, not efficiency. An anemic design model is not more efficient.
Also, think about how you will use data in your application. I can't think of a single time I've ever used a method like "GetAllOfSomething()", except for maybe a reference list. What is the purpose of retrieving everything in your database? If it's to do some process, data manipulation, report, you should be exposing a method that performs that process. If you need to expose a list for some external use, like populating a grid, then expose an
IEnumerable
and provide methods for subsetting data. If you start with the idea that you work with complete lists of data in memory you'll have serious performance problems as the data grows.