将自定义类型作为参数从 PL 传递到 BLL 到 DAL 以插入记录?
我在我的项目中使用 Entity framworke。我使用 3 层架构(表示层(PL)、业务逻辑层(BLL)和数据访问层(BAL))。实体框架将所有实体以及CRUD操作定义到BD。
我遇到了一个基本问题。假设我需要在数据库中插入客户。我想要 现在
---------------PL-------------
Customer ObjCustomer=new Customer();
//init object
ObjCustomer.Name="";
--------------
------------
BLL.InsertCustomer(ObjCustomer)
------------------------------------------------
-------------------BLL---------------
DAL.InsertCustomer(ObjCustomer)
------------------------------------
-------------------DAL---------------
CustomerReporitory.InsertCustomer(ObjCustomer)
------------------------------------
的问题是 customer 在 DAL 中定义为 EF 的一部分。不建议在 PL 中采用 DAL ref。我想传递自定义类类型(如客户)作为参数。如何做到这一点。请给我一些示例代码。
I am using Entity framworke in my project. I am using 3 layer architecture ( Presentation layer(PL), Business logic layer(BLL) and Data Access Layer(BAL)). Entity framework define all entity as well as CRUD operation to BD.
I have encountered one basic problem. Suppose i need to Insert customer in DB. I want to
do is as follow
---------------PL-------------
Customer ObjCustomer=new Customer();
//init object
ObjCustomer.Name="";
--------------
------------
BLL.InsertCustomer(ObjCustomer)
------------------------------------------------
-------------------BLL---------------
DAL.InsertCustomer(ObjCustomer)
------------------------------------
-------------------DAL---------------
CustomerReporitory.InsertCustomer(ObjCustomer)
------------------------------------
Now problem is that customer is defined in DAL as part of EF. It is not advisable to take DAL ref in PL. I want to pass custom class type like customer as parameter. How to do that. Please me some sample code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表示层中的客户不应该是业务层或数据层中的客户。表示层中的 Customer 应该类似于 ViewModel,因此它应该只携带属性并在业务层中声明。这是应该发送到 CreateCustomer 业务层的对象类型,业务层又从中创建业务实体或 DAO 并将其传递以进行持久化。
表示层
业务层
数据层
The Customer in your presentation layer shouldn't be the Customer from your business or data layer. The Customer in your presentation layer should be something similar to a ViewModel, so it should only carry attributes and be declared in the business layer. That is the type of object that should be sent to the business layer for CreateCustomer, which in turn creates a business entity or DAO from it and passes it for persistence.
Presentation layer
Business layer
Data layer
如果您的模型没有方法或功能,而只是一组获取/设置属性,您可以将其放置在单独的程序集/命名空间中,并让所有层将模型作为依赖项。不过,仅在非常简单的应用程序中建议这样做,对于任何更复杂的应用程序,请使用托比亚斯的方法。
If your model has no methods or functionality and is just a set of get/set properties you can place it in a separate assembly/namespace and have all the layers take the model as a dependency. This is only recommended in very simple applications though and for anything more complex use Tobias's approach.