Serializable Extra Types for .NET 4
You may follow this code @ serializableex.codeplex.com
Introduction
This is an updated article and code base from my original publication [Serialization for Rapid Application Development: A Better Approach] here at CodeProject. After several years of growth in ability and advances in technology I decided it was time to update that code base and article for use in .Net 4. The result of this is a much smaller code base, unit tests to back them up and fixes to some issues concerning discovery within a web environment.
Background
In short, the problems with the XmlSerializer that existed when I wrote the first article, still exist today. They are classic issues that there is still no unified approach in dealing with. The major problem is still how do you resolve unknown classes when serializing and deserializing.
There have been several attempts that I've followed over the years to get around this, anywhere from the creation of entire frameworks like YAXLib to instructions on how to make use of the IXmlSerializable interface at various levels of sophistication.
With all of these solution, the major problem from a developer perspective that has always crept in is cumbersomeness of the implementation. IXmlSerializable asks the developer to write some form of a customized implementation per class. YAXLib and others asks the developer to use different attributes or handle outputs that don't look like the clean(ish) xml generated by the XmlSerializer. The [XmlInclude(Type type)] attibute demands that all types to be serialized be within the same library. None of them (that I know of) in my opinion relieves the developer from the tedium of working with these solutions.
Goals RealizedSerializable Extra Types is designed to make be as thoughtless as possible with an absolute minimum of develop consideration. What it does is make use of the standard XmlSerializer and a slightly un-hyped overloaded constructor it has to incorporate extra type definitions for use in type resolution during serialization and deserialization.
Its actually a very simple idea. Keep a list of all the possible types that the XmlSerializer may have need of during a serialization/deserialization process. Register those types using Attribute adornments and provide some extension methods to make incorporating those lists thoughtless to the developer.
It works under a parent child relationship. I can best describe it as saying, its the reverse of the XmlInclude attribute. The XmlInclude attribute is placed on a class to give the serializer knowledge of other classes when serializing the class that is adorned. SerializableExtraType is placed on a class to give the other class knowledge of the adorned class when the other class is serialized.
So...
XmlInclude = Parent => Child
SerializableExtraType = Child => Parent
This allows the SerializableExtraTypes code to integrate related classes across libraries. Additionally, I have exposed a method by which you may register additional relationships at runtime. This solves any situation that you may come across with libraries and applications having complex implied relationships.
Using the code
The code available in the download and at codeplex site. Both have a series of test libraries and a consuming test project that show usage very well. Here I will outline the quick and dirty of how to make use of it.
First adorn a class with the required attribute like this:
// example 1 of registering class and all derived classes
[SerializableExtraType(typeof(Foo))]
// example 2 of registering class with an unrelated class
[SerializableExtraType(typeof(SomethingElse))]
// example 3 of registering with multiple classes in same attribute
[SerializableExtraType(typeof(ClassOne), typeof(ClassTwo))]
public class Foo { public Foo() {} }
The extension methods to make use of the SerializableExtraTypes is under System.Xml.Serialization
.
Now make use of the extension methods to serialize and deserialize objects.
// example 1 assuming that ClassOne and ClassTwo inherit from Foo
string xml = new Foo { ClassList = {new ClassOne(), new ClassTwo(), }, }.SerializeEx();
Foo obj = "<Foo><ClassList><ClassOne /><ClassTwo /></Foo>".DeserializeEx<Foo>();
// example 2 assuming that Foo bears no relationship with SomethingElse
string xml = new SomethingElse { ObjectList = { new Foo(), new Foo(), }, }.SerializeEx();
SomethingElse obj = "<SomethingElse><ArrayOfObject><Foo /><Foo /></ArrayOfObject></SomethingElse>".DeserializeEx<SomethingElse>();
There is more functionality built into the code base but that is a quick and dirty sample. Please take a look at the download for further examples.
History
- 25th April, 2008: Initial post
- 24th May, 2011: First Draft of second publication
This is the second publication of this method. Please send any comments or suggestions on how to improve it. You can email me at danatcofo@gmail.com
Post Comment
No problem, and further more if you want update alerts from this site at that time you have to subscribe for it, it will be a better for you Jackson. Have a lovely day!
of course, study is paying off. Is not it good whenever you uncover an excellent article? My personal internet searching seem full.. thank you. Great ideas you have got here..
you could have an amazing blog here! would you prefer to make some invite posts on my blog?
Regards for this post, I am a big fan of this site would like to continue updated.
thanks for sharing source files. many thanks
It seems like you are generating problems oneself by trying to remedy this concern instead of looking at why their can be a difficulty in the first place
I value the article.Much thanks again. Fantastic.
Pretty! This was an incredibly wonderful article. Many thanks for supplying these details.
Websites we recommend Wow, superb blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is excellent, as well as the content!
Vu que nous voulons que votre bien, nous offrons le premier
I think the admin of this site is genuinely working hard
It is actually a nice and helpful piece of info. I am happy that you simply shared this useful info with us. Please keep us up to date like this. Thanks for sharing.
Thank you ever so for you article post.Really thank you!
It as not that I want to replicate your web page, but I really like the design. Could you let me know which design are you using? Or was it especially designed?
Way cool! Some very valid points! I appreciate you writing this post and also the rest of the site is really good.
Really appreciate you sharing this blog post.Much thanks again. Great.
Im no pro, but I suppose you just made an excellent point. You naturally understand what youre talking about, and I can truly get behind that. Thanks for being so upfront and so honest.
Wonderful items from I like to make use of a treatment for my personal itchy vagina because it helps keep me personally esteem as opposed to hearing simply a doctor.
There is certainly a lot to find out about this subject. I love all of the points you ave made.
Thanks for the blog.Much thanks again. Cool.
This unique blog is really cool and besides informative. I have chosen many interesting advices out of this blog. I ad love to come back over and over again. Thanks!
There as definately a great deal to learn about this issue. I love all of the points you made.
Spot on with this write-up, I honestly think this website needs far more attention. I all probably be back again to see more, thanks for the info.
Really enjoyed this blog post.Thanks Again. Really Great.
I trust supplementary place owners need to obtain this site as an example , truly spick and span and fantastic abuser genial smartness.
Wonderful ideas you have here.. Take pleasure in the blog you available.. Truly appreciate the entry you made available.. Enjoying the blog post.. thank you
It as really very complicated in this full of activity life to listen news on Television, therefore I simply use the web for that purpose, and take the newest information.
Very good information. Lucky me I discovered your site by chance (stumbleupon). I have bookmarked it for later!
Visit this I was suggested this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are wonderful! Thanks!
This can be a really very good study for me, Should admit which you are one of the best bloggers I ever saw.Thanks for posting this informative article.
Simply wanna remark that you have a very nice website , I love the layout it actually stands out.
There is clearly a lot to realize about this. I suppose you made various nice points in features also.
you could have an remarkable weblog below! would you like to make a number of invite posts on my own blog?
Scene erotique amateur video ejaculation femme Here is my webpage film x
Perfectly composed articles , regards for selective information.
My blog; how to burn belly fat how to burn belly fat [Tyree]
wow, awesome article post.Really thank you! Want more.
Im obliged for the article.Much thanks again. Want more.
You will require to invest a substantial quantity
pretty handy material, overall I think this is worthy of a bookmark, thanks
This website was how do I say it? Relevant!! Finally I have found something that helped me. Thanks a lot!
This website definitely has all the information I needed concerning this subject and didn at know who to ask.
Just Browsing While I was surfing yesterday I saw a excellent article concerning
this is wonderful blog. A great read. I all certainly be back.
I really liked your article post.Thanks Again. Cool.
Some really excellent blog posts on this site, thanks for contribution.
You are so awesome! I do not think I have read a single thing like that before. So great to find someone with a few unique thoughts on this topic.
it is something to do with Lady gaga! Your own stuffs excellent.
you could have an important weblog here! would you prefer to make some invite posts on my weblog?
I value the article.Much thanks again. Keep writing.