Is there any way I can implement IValidatableObject on Portable Class Library Project?

情到浓时终转凉″ 提交于 2019-11-26 17:14:48

问题


I would like to ask about a doubt I have concerning to Portable Class Library. I'm developing a DAL Layer and I'm using Portable project for my business objects, this project is target to .Net Framework 4.03 and higher, SilverLight 4 and higher and .Net for Windows Store Apps.

I would like to implement IValidatableObject interface on each of my business objects, but even when System.ComponentModel.DataAnnotations is available for the frameworks this project is targeted to, I haven´t access to IValidatableObject interface, is not available on the .net portable framework, neither MetadataType attribute exists on it. Is there any solution to achieve this approach?


回答1:


There's probably not an easy solution for this. You can create your own interface in a Portable Class Library with the same functionality. However that would be a different interface so other frameworks that use DataAnnotations (ASP.NET MVC and Entity Framework I think) wouldn't use your version.

To get around that problem, you could create a PCL with the interface with the exact same name and APIs as exist on the different platforms. Then create class libraries for .NET 4.0.3, SL4, and Windows Store with the exact same assembly identity (name, version, and strong name key), and in those assemblies just put a [assembly:TypeForwardedToAttribute(typeof(IValidatableObject))] attribute.

Then your PCLs can reference the PCL library with the interface, and in any apps that use the PCLs, reference the type-forwarding assembly for that platform, which will redirect the reference to the PCL version of the interface to the one that comes in the platform.




回答2:


Here's longer description of solution which Daniel described: http://log.paulbetts.org/the-bait-and-switch-pcl-trick/



来源:https://stackoverflow.com/questions/16023742/is-there-any-way-i-can-implement-ivalidatableobject-on-portable-class-library-pr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!