7

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?

Avi
  • 21,182
  • 26
  • 82
  • 121
user2275999
  • 71
  • 1
  • 2

2 Answers2

6

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.

Daniel Plaisted
  • 16,674
  • 4
  • 44
  • 56
1

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

Community
  • 1
  • 1
czesio
  • 356
  • 1
  • 4
  • 13