Skip to content

Instantly share code, notes, and snippets.

@ahjohannessen
Forked from chadmyers/EntityModelBinder.cs
Created January 21, 2011 18:59
Show Gist options
  • Save ahjohannessen/790201 to your computer and use it in GitHub Desktop.
Save ahjohannessen/790201 to your computer and use it in GitHub Desktop.
public class EntityModelBinder : IModelBinder
{
private static readonly TypeConverter _converter = TypeDescriptor.GetConverter(typeof (Guid));
public bool Matches(Type type)
{
return type.CanBeCastTo<DomainEntity>();
}
public void Bind(Type type, object instance, IBindingContext context)
{
throw new NotSupportedException();
}
public object Bind(Type type, IBindingContext context)
{
var rawId = context.Service<IRequestData>().Value("Id");
var id = (Guid)_converter.ConvertFrom(rawId);
return context.Service<IRepository>().Find(type, id) ?? Activator.CreateInstance(type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment