Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I want to know the best implementaion of MVC with EF5 for the next sample

if we have a view like this
HTML
@model RegistirationModel

@using(Html.BeginForm())
{
@Html.TextBoxFor(x=>x.UserName)
@Html.PasswordFor(x=>x.Passowrd)
@Html.PasswordFor(x=>x.ConfirmPassword)
@Html.DropdownListFor(x=>x.Country_ID,new SelectList(ViewBag.Countries,"ID","Name"))

}


Sample For model
C#
public class RegistirationModel
{
public string UserName {get;set;}
public string Password {get;set;}
public string ConfirmPassword {get;set;}
public string Country_ID {get;set;}
}


Sample of Controller
C#
public partial class AccountController :Controller
{
[HttpGet]
[AllowAnonymouse]
public ActionResult Register()
{
  ViewBage.Countries=context.Contries.ToList<Country>();//direct from  context
}
[HttpPost] 
public ActionResult Register(RegistirationModel model)
{
if (ModelState.IsValid)
{
//do something
return RedirectToAction();
}
return view(model);
}
}

now if the ModelState is not valid i should reassign the ViewBage.Countries so should i bring it from database again or there is any way to re assign it to model (best implementation )

i know that i can put it in application state or event session state but is that decrease the web site performance .

and if i have a repository for the countries what should i put inside
and i need advice should i make the repository classes in Domain Project or Web project and if i put in in the domain project i should have the EF5 reference inside both Web and Domain

Best Regards
Ahmed Alsharu
Posted
Updated 22-Jun-13 9:42am
v4

1 solution

Hello Ahmed,
Based on your understanding, you have done a good job. I appreciate, but I would like to suggest you something here.:)
First is never use Context in a controller Action Method.
Try using Services where the Context method would be written i.e. the Database operations should be done.
Have a Interface and a Service that would implement the members in the Interface.
Then you can inject dependency i.e. use the Interfaces in the controllers to access the DB/Query operations. For this you need to understand the Dependency Injection.
Getting Started with Dependency Injection[^]
The above link would give you some idea.
Then the Controller would look like below:
C#
public partial class AccountController :Controller
{
    private IDemoVariable _demoVariable;
    public AccountController(IDemoVariable demoVariable)
     {
        _demoVariable = demoVariable;
     }     
[HttpGet]
[AllowAnonymouse]
public ActionResult Register()
{
  ViewBage.Countries=_demoVariable.MethodReturningTheQueryContextResults;//
}
[HttpPost]
public ActionResult Register(RegistirationModel model)
{
if (ModelState.IsValid)
{
//do something
return RedirectToAction();
}
return view(model);
}
}

The service would define or implement the Context .
Hope you get some idea on this.
Thanks
:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900