Skip to content

Instantly share code, notes, and snippets.

@ArnisL
Created August 30, 2010 13:36
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 ArnisL/557403 to your computer and use it in GitHub Desktop.
Save ArnisL/557403 to your computer and use it in GitHub Desktop.
namespace MyProject.Web.Binders{
using System.Web.Mvc;
using Domain;
public class RootBinder<TRoot,TRepository>:DefaultModelBinder
where TRepository:IRepository<TRoot> where TRoot:IRoot{
private readonly TRepository _repository;
public RootBinder(TRepository repository){
Guard.AgainstNull(repository);
_repository=repository;
}
public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext){
if(bindingContext.ModelType!=typeof(TRoot))
return base.BindModel(controllerContext,bindingContext);
var vp=bindingContext.ValueProvider;
var v=vp.GetValue(bindingContext.ModelName)??vp.GetValue("id");
var id=int.Parse(v.AttemptedValue);
return _repository.Get(id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment