Created
August 30, 2010 13:36
-
-
Save ArnisL/557403 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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