Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 294
user2275999 Avatar asked Apr 15 '13 19:04

user2275999


1 Answers

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.

like image 145
Daniel Plaisted Avatar answered Jun 29 '23 10:06

Daniel Plaisted