Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc generic controller

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.

like image 904
Frank Michael Kraft Avatar asked Aug 05 '10 10:08

Frank Michael Kraft


People also ask

What is ASP.NET MVC controller?

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).

What is the difference between API controller and MVC controller?

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.


1 Answers

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

like image 174
Omu Avatar answered Sep 25 '22 14:09

Omu