开发者

Share DataAnnotations MetaData class between server and client

开发者 https://www.devze.com 2023-02-28 03:10 出处:网络
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)

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消