在服务器和客户端之间共享 DataAnnotations MetaData 类
我很好奇这是否是一个疯狂的想法。现在我有一个典型的结构如下
客户端(当前是asp.net mvc3,但将来可能是任何东西)
WCF服务层
域层
数据层
我在域层中完成了一些非常复杂的验证。数据注释还不够。因此,验证是以更手动的方式完成的,但仍然通过服务层传回到客户端。
但是,DataAnnotations 适合简单的验证(字段不为空,必须大于 0,不能超过 10 个字符)。
这是我的计划。我已经分发了一个包含 DTO 对象的基本 DataContract dll。这是因为我有多个 WCF 服务。我计划创建一个程序集,其中包含带有 DataAnnotations 属性的好友元数据类。然后,该类用于我的 DTO 和域实体。这样我就可以在不通过 Web 服务的情况下获得简单的验证,但它仍然全部集中在一个地方。但是,对于不支持该功能的客户端(例如 java),如果他们通过 WCF 服务,他们仍然会获得验证。
本来我不想向客户端分发任何内容,但为了在服务之间共享 DataContracts,我无论如何都必须这样做。
这是一个疯狂的想法吗?我知道其他人只是在做一些双重验证。有更好的方法来实现这个吗?我已经测试过它并且它确实有效,但我不确定当我的整个模型构建完成时它的扩展效果如何(现在只是原型设计)。
谢谢
I'm curious if this is a crazy idea. Right now I have a typical structure as follows
Client (currently asp.net mvc3, but could be anything in the future)
WCF Service layer
Domain Layer
Data Layer
I have some very complex validation done in my domain layer. DataAnnotations doesn't suffice. So, that validation is done in a more manual way, but still passed back through the service layer to the client.
However, there is simple validation (field not null, must be greater than 0, can't be more than 10 chars) that DataAnnotations would be good for.
Here's my plan. I will already be distributing a basic DataContract dll that has DTO objects. This is because I have multiple WCF services. I plan to create an assembly that has the buddy metadata class with DataAnnotations attributes. This class is then used for BOTH my DTO and my domain entity. This way I can get simple validation without going through the web service, but it's still all in one place. However, for clients that don't support that (say java for example), they'd still get validation if they went through the WCF service.
Originally I didn't want to distribute anything to the client, but in order to share DataContracts among services, I have to anyway.
Is this a crazy idea? I know other people are just doing some double validation. Is there a better way to implement this? I have tested it and it does work but I'm not sure how well it will scale when my entire model is built out (just prototyping right now).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种做法并没有什么问题。在我看来,将您的 DataContracts 分成单独的程序集并与 .NET 客户端共享这些合同是一种最佳实践,听起来您只是更进一步并共享验证程序集。
我还与 .NET 客户端共享我的 ServiceContracts,这样客户端就不必添加服务引用,这真是令人讨厌。
There is nothing wrong with that approach. Separating your DataContracts into a separate assembly and sharing those contracts with .NET clients is a best practice in my opinion, it sounds like you're just taking it one step further and sharing validation assembly as well.
I also share my ServiceContracts with .NET clients, that way clients do not have to add service references, which is icky poo.