A generic controller can save you lots of time

Last week I created a table, repository, model, controller, view with all options edit, delete, create etc. within 15 minutes.

That's the nice part of using generic models and controllers. This is the generic controller Im using voor config tables, its initialized with the model type parameter and the repository type parameter. The constructor has one parameter wich is the autofact component. This is enough the create the repository and return or pass the models:

    // T is de type
    // R is de repository
    public class BaseTypeController<T, R>: Controller where T: new() where  R: iGenericTypeRepository
    {
        private IComponentContext afContainer = null;

        public BaseTypeController(IComponentContext afcontainer)
        {
            afContainer = afcontainer;
        }

        public ActionResult Index()
        {
            var model = new BaseTypeIndexViewModel<T, R>(afContainer);
            return View(model);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(T model)
        {
            var viewmodel = new BaseTypeIndexViewModel<T, R>(afContainer);
            viewmodel.ReturnMessage = afContainer.Resolve().Create(model);
            return View("Index", viewmodel);
        }

        public ActionResult Edit()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Edit(T model)
        {
            var viewmodel = new BaseTypeIndexViewModel<T, R>(afContainer);
            viewmodel.ReturnMessage = afContainer.Resolve().Update(model);
            return View("Index", viewmodel);
        }

        public ActionResult Delete(int id)
        {
            var model = afContainer.Resolve().Single(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(T model)
        {
            var viewmodel = new BaseTypeIndexViewModel<T, R>(afContainer);
            viewmodel.ReturnMessage = afContainer.Resolve().Delete(model);
            return View("Index", viewmodel);
        }

        [ChildActionOnly]
        public ActionResult ShowReturnMessage(ReturnMessage model)
        {
            if (model != null)
                return View("ReturnMessage", model);
            return null;
        }
    }

So you noticed that this is working with a generic view model with I call the baseTypeIndexViewModel. The code:

        public class BaseTypeIndexViewModel<T,B> : BaseViewModel<T> where T: new() where  B: iGenericTypeRepository
    {

        public BaseTypeIndexViewModel(IComponentContext afcontainer )
        {
            
            ModelViewItems = afcontainer.Resolve().SelectAll();
        }
    }         

this model is just selecting all items of the repository and add them to enumerable of the type: ModelViewItems. It's inheriting from the BaseViewModel which is just the model voor ModelViewItems.

 

The last thing you have to do is create the create, edit, delete, list views and you're done!

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!