-
-
Save ahjohannessen/790201 to your computer and use it in GitHub Desktop.
This file contains 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
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