Skip to content

Instantly share code, notes, and snippets.

@TimGeyssens
Created April 30, 2013 15:28
Show Gist options
  • Save TimGeyssens/5489463 to your computer and use it in GitHub Desktop.
Save TimGeyssens/5489463 to your computer and use it in GitHub Desktop.
Custom value injection classes (for use with http://valueinjecter.codeplex.com/) to map from a umbraco's IPublishedContent to a view model and from a view model to IContent (first rough version)
public class InjectionToIContent : ValueInjection
{
protected override void Inject(object source, object target)
{
var content = target as IContent;
var type = source.GetType();
if (type.GetProperty("Name") != null)
content.Name = type.GetProperty("Name").GetValue(source).ToString();
foreach (var prop in content.Properties.Where
(prop => type.GetProperty(prop.Alias, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) != null))
{
content.SetValue(prop.Alias, type.GetProperty(prop.Alias, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(source));
}
}
}
public class InjectionToIContent : ValueInjection
{
protected override void Inject(object source, object target)
{
var content = target as IContent;
var type = source.GetType();
if (type.GetProperty("Name") != null)
content.Name = type.GetProperty("Name").GetValue(source).ToString();
foreach (var prop in content.Properties.Where
(prop => type.GetProperty(prop.Alias, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) != null))
{
content.SetValue(prop.Alias, type.GetProperty(prop.Alias, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(source));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment