/// <summary>
        /// Maps the given object to a xml-dom.
        /// </summary>
        /// <param name="entity">The entity as object.</param>
        /// <param name="entityType">The entiy type.</param>
        /// <returns>The XElement filled with data from the given entity..</returns>
        protected XElement ToXElement(object entity, Type entityType)
        {
            XmlRepository.AddDefaultPropertyMappingsFor(entityType, this.PropertyMappings);

            var mappings = this.PropertyMappings[entityType];

            var element = this.CreateElement(entityType.Name);

            foreach (var mapping in mappings)
            {
                Func <string> valueGetter =
                    () =>
                    GetStringValueOrEmptyString(mapping.EntityType.GetProperty(mapping.Name).GetValue(entity, null));

                switch (mapping.XmlMappingType)
                {
                case XmlMappingType.Attribute:
                    element.Add(new XAttribute(mapping.MappedName, valueGetter()));
                    break;

                case XmlMappingType.Content:
                    element.Add(valueGetter());
                    break;

                case XmlMappingType.Element:
                    // Primitive property.
                    if (!mapping.IsClassPropertyType)
                    {
                        element.Add(this.CreateElement(mapping.MappedName, valueGetter()));
                        continue;
                    }

                    // Class property.
                    if (!mapping.IsGenericCollectionPropertyType)
                    {
                        object propertyObjectValue = entityType.GetProperty(mapping.Name).GetValue(entity, null);

                        if (propertyObjectValue == null)
                        {
                            element.Add(this.CreateElement(mapping.MappedName));
                        }
                        else
                        {
                            element.Add(this.ToXElement(propertyObjectValue, mapping.PropertyType));
                        }

                        continue;
                    }

                    // Generic collection property.
                    XElement collectionElement = this.CreateElement(mapping.MappedName);
                    element.Add(collectionElement);

                    PropertyInfo propertyType = entityType.GetProperty(mapping.Name);
                    Type         listType     = propertyType.PropertyType;
                    IEnumerable  collection   = propertyType.GetValue(entity, null) as IEnumerable;

                    if (collection != null)
                    {
                        Type collectionItemType = listType.GetGenericArguments()[0];

                        foreach (var item in collection)
                        {
                            collectionElement.Add(this.ToXElement(item, collectionItemType));
                        }
                    }

                    break;
                }
            }

            return(element);
        }
        /// <summary>
        /// Maps the given XElement that represents one node to a object.
        /// </summary>
        /// <param name="entityElement">The root element.</param>
        /// <param name="entityType">The entiy type.</param>
        /// <returns>The object filled with the data of given XElement.</returns>
        protected object ToObject(XElement entityElement, Type entityType)
        {
            XmlRepository.AddDefaultPropertyMappingsFor(entityType, this.PropertyMappings);

            var propertyMappings = this.PropertyMappings ?? XmlRepository.PropertyMappings;
            var mappings         = propertyMappings[entityType];

            object entity = Activator.CreateInstance(entityType);

            foreach (var mapping in mappings)
            {
                Action <string> valueSetter =
                    value =>
                    mapping.EntityType.GetProperty(mapping.Name).SetValue(entity,
                                                                          GetConvertedValue(mapping.PropertyType,
                                                                                            value), null);

                switch (mapping.XmlMappingType)
                {
                case XmlMappingType.Attribute:
                    if (entityElement.HasAttributes)
                    {
                        valueSetter(entityElement.Attribute(mapping.MappedName).Value);
                    }
                    break;

                case XmlMappingType.Content:
                    valueSetter(entityElement.Value);
                    break;

                case XmlMappingType.Element:
                    // Primitive propert.
                    if (!mapping.IsClassPropertyType)
                    {
                        if (entityElement.HasElements)
                        {
                            valueSetter(entityElement.Element(mapping.MappedName).Value);
                        }

                        continue;
                    }

                    // Class property.
                    if (!mapping.IsGenericCollectionPropertyType)
                    {
                        object possibleValue = this.ToObject(entityElement.Element(mapping.Name),
                                                             mapping.PropertyType);

                        if (possibleValue != null)
                        {
                            mapping
                            .EntityType
                            .GetProperty(mapping.Name)
                            .SetValue(entity, possibleValue, null);

                            continue;
                        }
                    }

                    // Generic collection property.
                    PropertyInfo propertyInfo = entityType.GetProperty(mapping.Name);

                    Type  listType       = propertyInfo.PropertyType;
                    IList entityList     = propertyInfo.GetValue(entity, null) as IList;
                    Type  entityItemType = listType.GetGenericArguments()[0];

                    if (entityList == null)
                    {
                        entityList =
                            ((IList)
                             Activator.CreateInstance(typeof(List <>).MakeGenericType(entityItemType)));
                    }

                    var collectionElement = entityElement.Element(mapping.MappedName);

                    if (collectionElement.HasElements)
                    {
                        foreach (var element in collectionElement.Elements())
                        {
                            entityList.Add(this.ToObject(element, entityItemType));
                        }

                        mapping.EntityType.GetProperty(mapping.Name).SetValue(entity, entityList, null);
                    }

                    break;
                }
            }

            return(entity);
        }