Created
January 24, 2011 07:33
-
-
Save dragan/792940 to your computer and use it in GitHub Desktop.
Quick gist to show the use of hydrating an entity from a dictionary(i.e. CouchDocument).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.ComponentModel.DataAnnotations; | |
namespace SineSignal.Ottoman | |
{ | |
public class CouchDocument : Dictionary<string, object> | |
{ | |
public const string ID_KEY = "_id"; | |
public const string REVISION_KEY = "_rev"; | |
public const string TYPE_KEY = "Type"; | |
public CouchDocument() | |
{ | |
} | |
public CouchDocument(object entity, PropertyDescriptor identityProperty) | |
{ | |
DehydrateEntity(entity, identityProperty); | |
} | |
public T HydrateEntity<T>(PropertyDescriptor identityProperty) | |
{ | |
Type entityType = typeof(T); | |
object instance = CreateInstance(entityType); | |
HydrateProperties(instance, entityType, identityProperty, this); | |
return (T)instance; | |
} | |
private void DehydrateEntity(object entity, PropertyDescriptor identityProperty) | |
{ | |
var entityType = entity.GetType(); | |
this[TYPE_KEY] = entityType.Name; | |
foreach (PropertyDescriptor property in GetEntityProperties(entityType)) | |
{ | |
var key = property.Name; | |
if (key == identityProperty.Name) | |
continue; | |
this[key] = property.GetValue(entity); | |
} | |
} | |
private static void SetProperty(object entity, PropertyDescriptor propertyDescriptor, object propertyValue) | |
{ | |
if (propertyDescriptor.Converter.CanConvertFrom(propertyValue.GetType())) | |
{ | |
propertyDescriptor.SetValue(entity, propertyDescriptor.Converter.ConvertFrom(propertyValue)); | |
} | |
else | |
{ | |
object instance = CreateInstance(propertyDescriptor.PropertyType); | |
HydrateProperties(instance, propertyDescriptor.PropertyType, (IDictionary<string, object>)propertyValue); | |
propertyDescriptor.SetValue(entity, instance); | |
} | |
} | |
private static void HydrateProperty(object entity, PropertyDescriptor identityPropertyDescriptor, PropertyDescriptor propertyDescriptor, IDictionary<string, object> entityValues) | |
{ | |
object propertyValue; | |
if (identityPropertyDescriptor == null || propertyDescriptor.Name != identityPropertyDescriptor.Name) | |
{ | |
propertyValue = entityValues[propertyDescriptor.Name]; | |
} | |
else | |
{ | |
propertyValue = entityValues[ID_KEY]; | |
} | |
SetProperty(entity, propertyDescriptor, propertyValue); | |
} | |
private static PropertyDescriptorCollection GetEntityProperties(Type entityType) | |
{ | |
return TypeDescriptor.GetProperties(entityType); | |
} | |
private static void HydrateProperties(object entity, Type entityType, IDictionary<string, object> entityValues) | |
{ | |
HydrateProperties(entity, entityType, null, entityValues); | |
} | |
private static void HydrateProperties(object entity, Type entityType, PropertyDescriptor identityPropertyDescriptor, IDictionary<string, object> entityValues) | |
{ | |
PropertyDescriptorCollection properties = GetEntityProperties(entityType); | |
foreach (PropertyDescriptor propertyDescriptor in properties) | |
{ | |
HydrateProperty(entity, identityPropertyDescriptor, propertyDescriptor, entityValues); | |
} | |
} | |
private static object CreateInstance(Type entityType) | |
{ | |
return Activator.CreateInstance(entityType); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment