asp.net универсальный контроллер mvc
Я имею в виду реализацию универсального контроллера в ASP.NET MVC.
PlatformObjectController<T>
где T - (сгенерированный) объект платформы.
Это возможно? Есть ли опыт / документация?
Например, один связанный вопрос - как получаются URL.
1 ответ
Решение
Да, вы просто не можете использовать это напрямую, но вы можете унаследовать это и использовать childs
вот тот, который я использую:
public class Cruder<TEntity, TInput> : Controller
where TInput : new()
where TEntity : new()
{
protected readonly IRepo<TEntity> repo;
private readonly IBuilder<TEntity, TInput> builder;
public Cruder(IRepo<TEntity> repo, IBuilder<TEntity, TInput> builder)
{
this.repo = repo;
this.builder = builder;
}
public virtual ActionResult Index(int? page)
{
return View(repo.GetPageable(page ?? 1, 5));
}
public ActionResult Create()
{
return View(builder.BuildInput(new TEntity()));
}
[HttpPost]
public ActionResult Create(TInput o)
{
if (!ModelState.IsValid)
return View(o);
repo.Insert(builder.BuilEntity(o));
return RedirectToAction("index");
}
}
и использования:
public class FieldController : Cruder<Field,FieldInput>
{
public FieldController(IRepo<Field> repo, IBuilder<Field, FieldInput> builder)
: base(repo, builder)
{
}
}
public class MeasureController : Cruder<Measure, MeasureInput>
{
public MeasureController(IRepo<Measure> repo, IBuilder<Measure, MeasureInput> builder) : base(repo, builder)
{
}
}
public class DistrictController : Cruder<District, DistrictInput>
{
public DistrictController(IRepo<District> repo, IBuilder<District, DistrictInput> builder) : base(repo, builder)
{
}
}
public class PerfecterController : Cruder<Perfecter, PerfecterInput>
{
public PerfecterController(IRepo<Perfecter> repo, IBuilder<Perfecter, PerfecterInput> builder) : base(repo, builder)
{
}
}
код здесь: http://code.google.com/p/asms-md/source/browse/trunk/WebUI/Controllers/FieldController.cs
Обновить:
используя этот подход здесь и сейчас: http://prodinner.codeplex.com/