Skip to content

Instantly share code, notes, and snippets.

@bredstrup
Created September 11, 2014 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bredstrup/1a7bed56a0360883fea3 to your computer and use it in GitHub Desktop.
Save bredstrup/1a7bed56a0360883fea3 to your computer and use it in GitHub Desktop.
Glass Delegate implementaion
public class DelegatePropertyConfiguration<T>: AbstractPropertyConfiguration where T : AbstractDataMappingContext
{
/// <summary>
/// The default Constructor.
/// </summary>
public DelegatePropertyConfiguration()
{
}
public Action<T> MapToCmsAction { get; set; }
public Func<T, object> MapToPropertyAction { get; set; }
}
public class SitecoreDelegate<T> : AbstractPropertyBuilder<T, SitecoreDelegatePropertyConfiguration>
{
public SitecoreDelegate(Expression<Func<T, object>> ex): base(ex){}
public SitecoreDelegate<T> SetValue(Action<SitecoreDataMappingContext> mapAction)
{
this.Configuration.MapToCmsAction = mapAction;
return this;
}
public SitecoreDelegate<T> GetValue(Func<SitecoreDataMappingContext,object> mapFunction)
{
this.Configuration.MapToPropertyAction = mapFunction;
return this;
}
}
public class SitecoreDelegatePropertyConfiguration : DelegatePropertyConfiguration<SitecoreDataMappingContext>
{
/// <summary>
/// The default Constructor.
/// </summary>
public SitecoreDelegatePropertyConfiguration()
{
}
}
public static class SitecoreTypeExtensions
{
public static SitecoreDelegate<T> Delegate<T>(this SitecoreType<T> type, Expression<Func<T, object>> ex)
{
var sitecoreDelegate = new SitecoreDelegate<T>(ex);
type.Config.AddProperty(sitecoreDelegate.Configuration);
return sitecoreDelegate;
}
}
@cardinal252
Copy link

I have nuget packages available for this as well as glass using Agnostic Ioc on private feeds and my mapping framework if you would like to mess around some more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment