使用 POCO 时,行为逻辑应该放在哪里?
我从未使用过 POCO,因此我习惯将大量逻辑放入业务对象类中。因此,我相信我错过了一些关于类布局的重要概念,以及这里所需的思考过程。
因此,我希望有一些想法可以将我的大脑引向正确的方向;
假设你有两节课;公司和员工。您能否举一些例子,说明您将“围绕”这些类构建哪些类来处理各种行为/验证等? (例如一些类名称及其用途的简短描述)
(或者我想任何其他示例也同样有用。)
I never used POCOs, so I have the habit of putting a lot of logic in my business object classes. Hence I believe I'm missing some important concepts about class-layouts, and the thought-process that is needed here.
So I would appreciate some ideas to point my brain in the right direction;
Say if you have two classes; Company and Employee. Could you give some examples of what classes you would build "around" these that take care of various behavior/validation etc.? (Like some class names, and a brief description of their purpose)
(Or any other examples would be just as useful I guess.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过为该业务类创建元数据类来放置验证和业务逻辑
像这样
这些类被称为“好友类”
请参阅此
you can put validation and business logic by creating a metadata class for that business class
like this
these classes are called 'buddy classes'
Refer this
我通常为系统中的任何逻辑代码分组创建一个外观层。例如,假设用户拥有管理员凭据,则只需处理公司数据和员工数据。因此,在这种情况下,我将创建一个 AdminFacade 类,如下所示:
我通常让 GUI 实例化所有 Facade 类,这就是 GUI 处理的全部内容。您还可以在 GetCompanyByEmployee 方法的开头添加验证,并在假设 employee.startdate 小于一年时抛出异常。
希望有帮助。
I usually create a facade layer for any logical code groupings in my system. For instance let's say a user will only need to deal with company data and employee data if the user has Admin Credentials. So in that case i would create an AdminFacade class as follows:
I usually have my GUI instantiate all the Facade classes and that is all what GUI deals with. You can also add validation at the start of GetCompanyByEmployee method and throw an exception if lets say employee.startdate is less than a year.
Hope that helps.