Wednesday, 4 June 2014

Using Data Annotations with Portable Class Libraries

I am currently writing an application that’s targeting multiple platforms – iOS, Android, Windows Phone as clients, and naturally .NET on the server which will be hosted in Azure. I’m making good use of portable class libraries, so that I can share as much code between my clients as possible (and I’m using Xamarin to make all this possible).

For my client projects I have two shared libraries – one with interfaces, another with implementations. As an example I have a DTO class on the client as follows…

public class AccountInformationRequest
public string Token { get; set; }

public string MSISDN { get; set; }

public string Carrier { get; set; }

This class is also used on the server in a controller method…

public async Task<AccountInformation> PostAccountInformation(AccountInformationRequest request)
return await _accountInformationService.GetAccountInformation(request);

On the server however I want to use data annotations to minimise the amount of code I need to write in order to validate the incoming request but this poses a problem – the AccountInformationRequest type is defined in a Portable Class Library, and if I update it to add on the data annotation attributes then my PCL will be re-targeted and won’t then run on all of my desired client platforms.

As usual in programming, another level of indirection solves every problem. Since .NET 1.0 we’ve had ICustomTypeDescriptor, and indeed one of the first articles I wrote online was about this interface (the article is long gone, it was written in 2001 ish). Since .NET 2 things have got measurably better for anyone wanting to do some spelunking with types, as it released the TypeDescriptor and TypeDescriptorProvider support. Fast forward a few more years and we now have a very simple way to augment one class with metadata from another – the massively named ‘AssociatedMetadataTypeTypeDescriptionProvider’.

What this class allows you to do is say “Hey, this class X provides metadata for that class Y”. Or more to the point for this article, “This server class which has all the attributes I need replaces the metadata for the PCL class”. All you need to do is create a second class which has all the metadata attributes you need (in my case just validation), then register it. My server side class is as follows…

public class AccountInformationRequestServer
public string Token { get; set; }

public string MSISDN { get; set; }

public string Carrier { get; set; }

Then I can just register it inside Global.asax…

(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(AccountInformationRequest),

The registration looks a little cryptic, so I’ve altered it below to show the orginalClass (that being the class defined in the PCL) and the metadataClass (that being the one defined server side)…

(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(originalClass), typeof(metadataClass)),

That’s all there is to it. Now I can make an API call from the client, and have API validation on the server, whilst still using the same datatypes in both PCL and full fat .NET.

You’ll find the AssociatedMetadataTypeTypeDescriptionProvider class defined within System.ComponentModel.DataAnnotations (in the assembly with the same name).


Unknown said...

Very cool stuff. I am just about to start a strikingly similar project for a client and this will be very helpful! Thank you sir.

Unknown said...

Portable class libraries contain some of the articles and materials for students as well. Saint Michael's College libraries have been using an old method of essay writing help service assignments within the class activities. Among those - citations and reminders as well as footnotes.

Richard Majece said...

I think that randomness isn't the best idea here. I am sure that will help you to understand all the importance of my words. Just pay attention on education.

blackkutty said...

Extremely cool stuff. I am going to begin a strikingly comparative task for a customer and this will be extremely useful!
Article Submission sites | Latest Updates

admin said...

It’s very excellent information and more real facts to provided that post.Thank you for sharing this information. 500-701 exam dump

Unknown said...

Thanks for sharing the valuable information.
DOT NET Training Institutes in Chennai|Best DOT NET Training in Chennai|DOT NET Training in Chennai

Martha Jones said...

Code Writing taking to much time because that is not ordinary writing programmer face many errors while writing code. I am also writing a quality code of Tutors Help Online website.

Puremelda said...

You attain full value for the money which you put in to access when you seek the services of our custom dissertation writing help, as well as other types of top research papers from our firm which include dissertations, theses, reviews any different writings.

vinudevan said...

Thanks for sharing informative article with us..
DOT NET Training in Chennai
DOT NET Training Chennai
dot net course fees in chennai
.net training
dot net training in Velachery
Html5 Training in Chennai
Spring Training in Chennai
Struts Training in Chennai
Wordpress Training in Chennai
SAS Training in Chennai

Unbiased said...

Very informative article, Which you have shared here about the Data Annotations. After reading your article I got very much information and it is very useful for us. Thank you for sharing this article here. unbiased is the best Data annotation platform & block chain based decentralized data marketplace.

Mani Mekalai said...

Valuable one,waiting for next upate...thanks for sharing...
Html5 Training in Chennai
html course fees in chennai
Html5 Training
Html5 Training in Tambaram
Html5 Training in Anna Nagar
DOT NET Training in Chennai
core java training in chennai
Hibernate Training in Chennai
Mobile Testing Training in Chennai
SAS Training in Chennai

Vivek Vichu said...

Nice Blog...Thanks for sharing the article waiting for next update...
Artificial Intelligence Course in Chennai
artificial intelligence training in chennai
Mobile Testing Training in Chennai
C C++ Training in Chennai
javascript training in chennai
Html5 Training in Chennai
QTP Training in Chennai
Spring Training in Chennai
DOT NET Training in Chennai

Pooja Patel said...

More impressive blog!!! Thanks for shared with us.... waiting for you upcoming data.
Software Testing Training in Chennai
software testing course in chennai
testing courses in chennai
best software testing training institute in chennai with placement
Software testing training in Annanagar
Software testing training in vadapalani
Android training in Chennai
Python Training in Chennai
Big data training in chennai
JAVA Training in Chennai

shemadevan said...

Awesome blog, informative content...thanks for sharing...
LoadRunner Training in Chennai
Loadrunner Training
loadrunner training in vadapalani
loadrunner training in Guindy
loadrunner training in Thiruvanmiyur
QTP Training in Chennai
core java training in chennai
C C++ Training in Chennai
Mobile Testing Training in Chennai
Manual Testing Training in Chennai

Prakash said...

More valuable post!!! Thanks for sharing this great post with us.
JAVA Training in Chennai
JAVA Course in Chennai
advanced java training in chennai
Java training institute in chennai
Java classes in chennai
java training in porur
java training in OMR
Big data training in chennai
Selenium Training in Chennai
IOS Training in Chennai

The India said...

Content Writing Company in Delhi
Content Writing Services in Delhi
Mobile App Development Company Delhi
PPC Company in Delhi
PPC Company in India

norhan said...

نجار ابواب بالرياض نجار ابواب بالرياض
تركيب ستائر بالرياض افضل شركة تركيب ستائر بالرياض
تنظيف مكيفات بالرياض شركة غسيل مكيفات بالرياض
شركة تنظيف افران الغاز بالرياض تنظيف افران بالرياض

ahmed5987 said...

لذلك ، من المهم بالنسبة لك تحديد العقرب إذا كنت مصابًا بالصدمات. إذا لم تكن متأكدًا ، فمن الأفضل أن تطلب المساعدة الطبية لاتخاذ الاحتياطات اللازمة.افضل شركة مكافحة حشرات
شركة مكافحة النمل الابيض بالجبيل
شركة مكافحة حشرات بالجبيل
شركة رش مبيدات بالجبيل

Sankar said...

Great Article
IEEE Android Projects for CSE

Java Training in Chennai

FInal Year Project Centers in Chennai

Java Training in Chennai

sasi said...

This was an excellent post and very good information provided, Thanks for sharing.
Python Training in Chennai
Python Training in Bangalore
Python Training in Coimbatore
Python course in bangalore
angular training in bangalore
web design training in coimbatore
python training in hyderabad
Python Course in Coimbatore

sindhuvarun said...

Learned a lot of new things in this post. This post gives a piece of excellent information.
PHP Training in Chennai
PHP Training in Bangalore
PHP Training in Coimbatore
PHP Course in Madurai
AWS Training in Bangalore
Data Science Courses in Bangalore
PHP Course in Chennai
PHP Course in Bangalore
PHP Training in Coimbatore
PHP Training in Madurai

Reshma said...

Get inspired by your blog. Keep doing like this....
Ethical Hacking Course in Chennai
Ethical hacking course in bangalore
Ethical hacking course in coimbatore
Ethical Hacking Training in Bangalore
Certified Ethical Hacking Course in Chennai
Ethical Hacking in Bangalore
Hacking Course in Bangalore
Ethical Hacking institute in Bangalore
Selenium Training in Bangalore
Software Testing course in Bangalore

rinjuesther said...

valuable blog,Informative content...thanks for sharing, Waiting for the next update...
clinical sas training in chennai
clinical sas course
clinical sas Training in Porur
clinical sas Training in Velachery
clinical sas Training in Tambaram
SAS Training in Chennai
Spring Training in Chennai
LoadRunner Training in Chennai
QTP Training in Chennai
javascript training in chennai