I am thinking of implementing a generic Controller in ASP.NET MVC.
PlatformObjectController<T>
where T is a (generated) platform object.
Is this possible? Is there experience / documentation?
One related question for example is how the resulting URLs are.
A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request. A controller is just a class (for example, a Visual Basic or C# class).
The main difference is: Web API is a service for any client, any devices, and MVC Controller only serve its client. The same because it is MVC platform.
yes it is, you just can't use it directly but you can inherit it and use the childs
here is one that I use:
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");
}
}
and usages:
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)
{
}
}
the code is here: http://code.google.com/p/asms-md/source/browse/trunk/WebUI/Controllers/FieldController.cs
update:
using this approach here now: http://prodinner.codeplex.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With