实体框架和业务层/逻辑
我正在自学 MVVM-light - EF 应用程序的架构 我正在尝试构建一个类似产品/收据的应用程序。 我有一个 Db/EF,其中包含多对多关系的产品和收据表/实体。 然后我有一个 DAL,它只使用 Linq 来执行简单的 CRUD。
问题是在哪里以及如何将我的业务逻辑放入此应用程序中。
我想到了几个想法
选项 1 -制作一个ReceiptBo(收据业务对象) 继承Entity Receipt类和Icollection(ProductBo) ReceiptBo 类负责添加 Product、计算总计和小计并调用 Dal 进行插入。 也许这个选择似乎有点矫枉过正。
选项 2 -使用分部类将计算方法放入生成的Entity对象中 并简单地使用 BuisnessLayer 来调用 Dal。 在我看来,这将使 Buisnesslayer 类过时,并且我不确定实体类是否应该用于业务逻辑?
选项3 -创建业务类,但不必使用继承,只需将产品添加到实体并在那里进行计算并调用 Dal 进行插入。 看起来很简单但不是很优雅。
选项 4 -以上都不是,我
现在一无所知,我没有使用 WCF,但我的想法是我想让这个应用程序松散耦合,以便很容易实现它并进一步扩展它。
我对什么是业务层也有点困惑。在某些示例中,它更多地像 Dal 一样使用,也进行计算,然后其他人说这还没有完成。
一些帮助会很好。 thx
ps:抱歉我的英语不好
im doing some self-study on architecture of a MVVM-light - EF Application
im trying to build a product/Receipt like app.
I have a Db/EF with a Product and Receipt Table/Entity in a many to many relation.
then i have a DAL wich simply uses Linq to do simple CRUD.
the question is where and how to put my business logic in this app.
a couple of ideas came to mind
option 1
-make a ReceiptBo (Receipt business object)
wich inherit the Entity Receipt class and Icollection(ProductBo)
the ReceiptBo class would be responsible for adding Product,calculating total and subtotal and calling the Dal for inserting.
maby this option seemed a little overkill.
option 2
-put the calculating methods in the generated Entity objects by using partial classes
and simply using the BuisnessLayer to call the Dal.
this would make the Buisnesslayer Classes obsolete in my opinion and im not sure that Entity classes should be used for Business logic at all ?
option 3
-make the Business Classes but dont bother using inheritance, just add products to the Entity's and do the calculations there and call the Dal for insert.
wich seems simple but not very elegant.
option 4
-none of the above and im clueless
right now im not using WCF but the idea is that i want to make this app loosly coupled so that it would be easy to implement it and further extend it.
also im a little confused about what an Business layer is. in some examples it is more used like a Dal that also does the computing, then others say this is not done.
some help would be great. thx
ps: sorry for my bad english
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,我会在这里简单地选择一个通用的多层架构,设计如下:
我不会直接在实体中添加任何方法。所有方法都在业务层中定义,并由服务层公开。
Really, I would go simple here and choose a common multi-tier architecture designed as follows:
I wouldn't add any method directly in the entities. All methods are defined in the business layer, and exposed by the service layer.
通常,我将业务逻辑保存在我的
ViewModels
中,而不是我的Models
中,并且我将 EF 对象视为Models
。他们最多会进行一些基本的数据验证,例如验证长度或必填字段。例如,
EditRecieptViewModel
将验证业务规则,例如验证值是否在特定范围内,或验证用户是否有权编辑对象,或在值更改时执行一些自定义计算。另外,不要忘记
ViewModels
应该反映视图,而不是Model
,因此并非每个Model
都会有ViewModel< /code> 自己的
Usually I keep my business logic in my
ViewModels
, not myModels
, and I view EF objects asModels
. At most, they'll have some basic data validation, such as verifying length or required fields.For example, an
EditRecieptViewModel
would validate business rules such as verifying values are in a specific range, or verifying that users have access to edit the object, or performing some custom calculations when a value changes.Also, don't forget that
ViewModels
should reflect the View, not aModel
, so not everyModel
will have aViewModel
of its own