Archive for tag: C#

Available for hire from April 1st

Today it became finalized that I will be available for hire from April 1st and on, since Milagro chose not to extend my contract. It has nothing to do with my skills, but is rather an economic issue.

Sad as it may be, it leaves me available for hire, to do Umbraco or EPiServer work, wherever I may be needed. I have several years experience, and am certified, on both platforms. I work well in teams, and solo, and you're really missing out if you don't hire me. If needed, I'll travel for start up meetings etc. So don't hesitate just because I'm Swedish. I'm that good.

There's an online resume-ish site here. And I'll even send you a PDF with more info, if you still have doubts.

I know, cheap trick, but I have a family to support. :)

Making objects behave across a client-server relationship. Part 4 - To hell and back. Deserializing my baby.

How to enforce that initialization is done properly once the object has been de-serialized?

So far so good. But as with all serializable objects, they need to have a default (parameterless) constructor. And I want my objects to behave, remember?

Enter another useful attribute. OnDeserialized.

[Serializable]
[DataContract(IsReference = true)]
public abstract class Element {
...
[OnDeserialized]
private void OnDeserialized(StreamingContext context) {
if (this.Children == null) { this.Children = new ElementList(this); }
//Make sure that any new child added gets a correct reference to its parent.
if ( this.Children.Element == null) { this.Children.Element = this; }
//Make sure that all children have correct parental references.
foreach (Element e in this.Children) { e.Parent = this; }
}
...
}

This will be executed every time my object has been de-serialized, enforcing my constructor logic.

Now, as far as I'm concerned, that's behaving.

Making objects behave across a client-server relationship. Part 3 - Serializing the hell out my object.

How to make sure that my object can be serialized in an orderly fashion?

Making sure that my object kan be serialized is simple, we just add the [Serializable] attribute to it.

[Serializable]
public abstract class Element {
...
}

However, since my object has a reference to its Parent object, serializing it will most likely result in a cyclic, never ending, recursion. Fail.

Enter the DataContract.

[Serializable]
[DataContract(IsReference = true)]
public abstract class Element {
...
}

By applying it to my class, it enables serialization and deserialization with, for instance, the DataContractSerializer. I simply apply the [DataMember] attributes to all members I want to serialize, and voilá, I get a nice and clean, reference enabled, serialization.

But, you might ask, how does this work with the fancy List implementation we did?

Simple, there's a [CollectionDataContract] attribute available for that. That ensures that my List gets serialized to exactly that, and not a stupid array.

Next up: What happens on deserialization.

Making objects behave across a client-server relationship. Part 2 - Setting the parent reference.

How to make sure that any objects added to the Children property, gets a correct reference to the parent object (this)?

Attempt 1: Adding Add and Insert methods to the Element

public abstract class Element {
public Element() {
this.Children = new List<Element>();
}
public int ID { get; protected set; }
public string Name { get; protected set; }
public virtual Element Parent { get; internal set; }
public virtual List<Element> Children { get; private set; }
public string Instructions { get; set; }

public void Add(Element item){
item.Parent = this;
this.Children.Add(item);
}
...
}

This solution works all-right for many cases, however, it doesn't solve the issue when the Add method is used on the List<Element> directly. We'll want this.

Attempt 2: Changing the List type

public abstract class Element {
public Element() {
this.Children = new ElementList();
}
public int ID { get; protected set; }
public string Name { get; protected set; }
public virtual Element Parent { get; internal set; }
public virtual ElementList Children { get; private set; }
public string Instructions { get; set; }
}

public class ElementList : List<Element> {
public Element Element { get; internal set; }
public ElementList(Element element) {
if (element != null){ this.Element = element; }
else { throw new ArgumentNullException("element"); }
}
new public void Add(Element item) {
if ( item.Parent == null) { item.Parent = Element; }
base.Add(item);
}
new public void Insert(int index, Element item) {
if (item.Parent == null) { item.Parent = Element; }
base.Insert(index, item);
}
new public void InsertRange(int index, IEnumerable<Element> collection){
foreach (Element e in collection){
if (e.Parent == null) { e.Parent = Element; }
}
base.InsertRange(index, collection);
}
new public void AddRange(IEnumerable<Element> collection) {
foreach (Element e in collection) {
if (e.Parent == null) { e.Parent = Element; }
}
base.AddRange(collection);
}
}

This solution ensures that all Elements added to an ElementList, gets their Parent property re-set to reference the ElementLists Element property. This way, one can always assume that an Element's Children, always have their Parent references set.

Next up: Serialization.

Making objects behave across a client-server relationship. Part 1 - The scenario.

Ok, so here's the scenario. I have a set of objects, which all inherit from Element (shown below). I need these objects to behave properly when transferred across a web service, and more importantly, when they return. One assumption I want to be able to make, is that any Element contained in the Children property, should have its Parent property set to the containing object, so it's a node-like structure. Furthermore, I need my non-default constructor logic to apply, to de-serialized objects, as if they were initialized with my parameterized constructor.

public abstract class Element {
public Element() {
this.Children = new List<Element>();
}
public int ID { get; protected set; }
public string Name { get; protected set; }
public virtual Element Parent { get; internal set; }
public virtual List<Element> Children { get; private set; }
public string Instructions { get; set; }
}

The problems:

How to make sure that any objects added to the Children property, gets a correct reference to the parent object (this)?

How to make sure that my object can be serialized in an orderly fashion?

How to enforce that initialization is done properly once the object has been de-serialized?