添加方法POCO类

编程入门 行业动态 更新时间:2024-10-24 04:41:03
本文介绍了添加方法POCO类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下设置:MVC>服务>存储库。现在我想,让用户能够将注释添加到文档。只有与文档相关的用户(无论是作为业主或审阅)可以添加注释所以在我NoteService我这样做,以确保用户有选择的文件权限:

公共注GetNewNote(INT documentID)    {        如果(!userHasAccess(Thread.CurrentPrincipal.Identity.Name))            抛出新BusinessLogicException();        //这里其他的东西...    }

我的问题是,我应该定义userHasAccess方法?这使得在NoteService没有意义,因为它是在一个文件检查。我可以在DocumentService定义它,但随后将NoteService需要访问这个似乎是引入更多的耦合。

对我来说更有意义,定义它的文档POCO本身,然后调用document.userHasAccess(...)。这会是很好的做法还是应该域POCO限于简单的属性?我担心,这确实是验证的一部分,通过将法在POCO我服务分隔条件和POCO的验证。

我试图确保的是,我的应用程序易于维护和测试。我应该如何解决这个任何建议将是最AP preciated!

解决方案   

我应该在哪里定义userHasAccess方法?

这是有道理的符合设计的其余部分保持一致,虽然我不知道完整的设计,我至少可以说,一个在POCO本身叫UserHasAccess()方法是有道理的。

  

如果一个域名POCO限于简单的属性?

没有,一个域POCO应包含与对象相关的逻辑(尤其是验证逻辑)。否则,它最终被无行为一个对象 - 这是你绝对应该避免。

但是,没有得到一个域(业务)对象和视图对象,通常会包含一些逻辑的混淆。

  

您担心您分离的验证  服务与POCO。

我把验证了POCO,和跨域逻辑的服务。

I have the following setup: MVC > Services > Repositories. Now I want to allow users to be able to add a Note to a Document. Only Users associated with the Document (either as owners or reviewers) can add Notes so in my NoteService I do the following to ensure the User has permission on the selected Document:

public Note GetNewNote(int documentID) { if (!userHasAccess(Thread.CurrentPrincipal.Identity.Name)) throw new BusinessLogicException(); // Other stuff here... }

My question is, where should I define the userHasAccess method? It makes no sense in the NoteService as it is checking on a Document. I could define it in the DocumentService but will then NoteService will need access to this which seems to be introducing more coupling.

To me it makes more sense to define it on the Document POCO itself and then call document.userHasAccess(...). Would this be good practice or should a domain POCO be limited to simple properties? I am concerned that this is really part of the validation and that by placing the method in the POCO I am seperating the validation between Service and POCO.

What I am trying to ensure is that my application is easy to maintain and test. Any suggestions on how I should tackle this would be most appreciated!

解决方案

Where should I define the userHasAccess method?

It makes sense to be consistent with the rest of the design, while I don't know the full design I can at least say that a method called UserHasAccess() on the POCO itself makes sense.

Should a domain POCO be limited to simple properties?

No, a domain POCO should contain logic (especially validation logic) related to the object. Otherwise, it ends up being an object with no behaviour - something you should definitely avoid.

However, don't get confused between a domain (business) object and a view object, which will typically contain little logic.

You are concerned that you are separating the validation between Service and POCO.

I'd put validation in the POCO, and cross-domain logic in the services.

更多推荐

添加方法POCO类

本文发布于:2023-07-27 11:54:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   POCO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!