asp.net mvc - Generic CRUD controllers and views -


i'm going through intro tutorials asp.net , i've got decent idea of how implement simple crud admin app.

are there commonly used patterns implement generic list/create/update/delete actions? seems pretty tedious have build scaffolding every model, , maintain of add, edit , list views , controllers. lot more efficient , less error-prone implement generic actions like:

/list/model /edit/model/id /update/model/id /delete/model/id 

that handle model.

i've done similar, think, you're talking in admin application built. basically, key use generics. in other words, create controller like:

public abstract class admincontroller<tentity> : controller     tentity : ientity, class, new() {     protected readonly applicationdbcontext context;      public virtual actionresult index()     {         var entities = context.set<tentity>()         return view(entities);     }      public virtual actionresult create()     {         var entity = new tentity();         return view(entity);     }      [httppost]     public virtual actionresult create(tentity entity)     {         if (modelstate.isvalid)         {             context.set<tentity>().add(entity);             context.savechanges();             return redirecttoaction("index");         }          return view(entity);     }      ... } 

in other words, build entire reusable controller structure, key parts being you're using generic tentity instead of concrete class. notice tentity defined ientity, class, new(). few things. first, class allows treat concrete type , new() means type can instantiated, rather abstract class. ientity placeholder whatever may using in application ensure types have common denominator. @ least crud-style application, you'll need gain access id or similar property things edit , delete actions. saying tentity implements ientity lets utilize properties on ientity. if use concrete type here instead of interface, can leave off class part, e.g. where tentity : entity, new().

then, in order use this, define new controller inherits admincontroller<> , specify type you're working with:

public class widgetcontroller : admincontroller<widget> {     public widgetcontroller(applicationdbcontext context)     {         this.context = context;     } } 

that potentially need individual controllers. also, worth noting here i've set employ dependency injection context. change constructor like:

public widgetcontroller() {     this.context = new applicationdbcontext(); } 

but, recommend using dependency injection, in general. also, i'm using context directly here ease of explanation, you'd employing services, repositories, etc. here instead.

finally, if find need customize parts of crud action, not whole thing, can add methods extension points. example, let's needed populate select list 1 particular entity, might like:

public abstract class admincontroller<tentity> : controller     tentity : ientity, class, new() {     ...      public virtual actionresult create()     {         var entity = new tentity();         beforereturnview();         return view(entity);     }      ...      protected virtual void beforereturnview()     {     }      ... 

and then:

public class widgetcontroller : admincontroller<widget> {     ...      protected override void beforereturnview()     {         viewbag.myselectlist = new list<selectlistitem>         {             ...         };     } } 

in other words, have hook in base action method override change particular bit of functionality instead of having override whole action itself.

you can take farther include things view models, might expand generic class definition like:

 public abstract class admincontroller<tentity, tentityviewmodel, tentitycreateviewmodel, tentityupdateviewmodel>      tentity : ientity, class, new()      tentityviewmodel : class, new()      ... 

and then:

public class widgetcontroller : admincontroller<widget, widgetviewmodel, widgetcreateviewmodel, widgetupdateviewmodel> {     ... } 

it depends on application needs.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -