私は現在、Entity Framework v4エンティティ(CRUDとGET操作が実装されています)に基づいたリポジトリを持っています。私はこれらのエンティティの対応するビューモデルを作成中です。どこに変換/マッピングを置くべきですか?コントローラでリポジトリを変更し、そのメソッドでマッピングを実行し、View Model型のオブジェクトを返す(または受け入れる)ViewModelオブジェクトからEFモデルエンティティへの変換ここで、
私はで再利用するために起こっている場合、私は、コントローラ上のプライベートメソッドにそれを抽出したり、サービスクラスにそれを置くだろうこの
public ActionResult Index()
{
var person = new PersonRepository().Get();
var personViewModel = new PersonViewModel();
personViewModel.InjectFrom(person)
.InjectFrom<CountryToLookup>(person);
return View(personViewModel);
}
またはこの
public ActionResult Index()
{
var personViewModel = new PersonRepository().Get(); // returns object of type PersonViewModel
// and move this to repository
// var personViewModel = new PersonViewModel();
// personViewModel.InjectFrom(person)
// .InjectFrom<CountryToLookup>(person);
return View(personViewModel);
}
valueinjecterを使用しているので、ここからライブデモのソースコードを参照することを強くお勧めします。http://awesome.codeplex.com/、look IBuilderのCruder(Controller)と実装では、 –
Omu