简体   繁体   English

如何从dal填写业务object

[英]How to fill business object from dal

I'm returning DataTable in DAL.我在 DAL 中返回 DataTable。 And I have a businessObject called Customer.我有一个名为 Customer 的业务对象。 Where should I fill this object?我应该在哪里填写这个 object? Should it be done in DAL?应该在 DAL 中完成吗? Or in my front-end app?还是在我的前端应用程序中? or where?还是在哪里? A little cofused.有点迷茫。

If at all possible, you want to avoid returning DataTable in your DAL and instead return the properly filled business object.如果可能,您希望避免在 DAL 中返回DataTable ,而是返回正确填充的业务 object。 There are several tools that will help you with this, for example LINQ to SQL, Entity Framework, or (my current favorite) Dapper.NET (used by this very site).有几个工具可以帮助您解决这个问题,例如 LINQ 到 SQL、Entity Framework 或(我目前最喜欢的) Dapper.NET (由该站点使用)。 I like Dapper best because it's lightweight and works well with a Data Repository pattern, which I frequently use.我最喜欢 Dapper,因为它是轻量级的,并且与我经常使用的 Data Repository 模式配合得很好。

You could have a Base DAL, which returns DataTables.您可以有一个返回 DataTables 的 Base DAL。 Then have an Upper DAL, which does the converting to and from entities, (this is what I tend to use)然后有一个 Upper DAL,它执行实体之间的转换,(这是我倾向于使用的)

You really shouldn't be referring to DataTables in your UI layer.你真的不应该在你的 UI 层中引用 DataTables。

A half-way house that I have seen before is doing the 'object filling' within a method in the actual business object itself, but that doesn't allow you to switch out your DAL so easily.我以前见过的中途之家是在实际业务 object 本身的方法中进行“对象填充”,但这不允许您如此轻松地切换 DAL。

When crossing domains like this, where the representation of the data is different in each, you need an integration layer that will take care of the mapping between both representations.当像这样跨域时,数据的表示在每个域中都不同,您需要一个集成层来处理两种表示之间的映射 Most ORM tools do that mapping for you automatically, though.不过,大多数 ORM 工具会自动为您进行映射。

But to stick with your example, you could have a layer of mappers to do that.但是要坚持您的示例,您可以使用一层映射器来执行此操作。 CustomerMapper would then map from the DataTable to the Customer , effectively bridging the two domain representations.然后CustomerMapper将 map 从DataTableCustomer ,有效地桥接两个域表示。

It should be done in BL or Business Logic layer.它应该在 BL 或业务逻辑层中完成。

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

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