asp.net mvc 3 - Wrapper class in MVC3 -
i want create wrapper class queries should not in controller. select queries placed in controller. want create layer abstraction.
i created viewmodel class. wrapper class else.
how do that?
i don't queries directly in controllers. have service layer controller call, , each service layer call repository insert, update or delete data or bring data.
the sample code below uses asp.net mvc3
, entity framework code first
. lets assume want bring countries , use whatever reason in controller/view:
my database context class:
public class databasecontext : dbcontext { public dbset<country> countries { get; set; } }
my country repository class:
public class countryrepository : icountryrepository { databasecontext db = new databasecontext(); public ienumerable<country> getall() { return db.countries; } }
my service layer calls repository:
public class countryservice : icountryservice { private readonly icountryrepository countryrepository; public countryservice(icountryrepository countryrepository) { // check nulls on countryrepository this.countryrepository = countryrepository; } public ienumerable<country> getall() { // whatever else needs done return countryrepository.getall(); } }
my controller call service layer:
public class countrycontroller : controller { private readonly icountryservice countryservice; public countrycontroller(icountryservice countryservice) { // check nulls on countryservice this.countryservice = countryservice; } public actionresult list() { // countries ienumerable<country> countries = countryservice.getall(); // whatever need return view(); } }
there lots of info on internet on how data , display it, inserting, editing, etc. place start @ http://www.asp.net/mvc. work through tutorials, good. best.
Comments
Post a Comment