2016-04-27 7 views
1

私のアプリケーションを作成中に私はModel layerRepository layerを別々のプロジェクトに投げた。私のアプリの各フォームは、独自のViewModel classで表されます。 ViewModel classesはすべて、MyApplicationName.Web内のフォルダViewModelsに保存されています。 MyDrafts formためGET request時には、以下の機能が起動されます。複雑なクエリをコントローラからリポジトリに移動する最適なソリューションは何ですか?

public ActionResult MyDrafts() 
     { 
      MyDraftsVM dataVM = GetDataMyDrafts(); 
      return View(dataVM); 
     } 


     private MyDraftsVM GetDataMyDrafts() 
     {   
      MyDraftsVM dataVM = new MyDraftsVM(); 

      using (var context = new PrincipalContext(ContextType.Domain)) 
      { 
       List<MyDraftsVM.MyDraftVM> userInvoices = new List<MyDraftsVM.MyDraftVM>(); 
       userInvoices = _repoExhibitor.Context.Exhibitors 
               .Join(_repoExhibitor.Context.Invoices.Where(x => x.CreatedBy == User.Identity.Name && x.Status == (int)(ModelEnums.Invoice.Status.Przygotowanie)), 
               e => e.Id, 
               i => i.Id, 
               (e, i) => new { e, i }) 
               .ToList() 
               .Select(s => new MyDraftsVM.MyDraftVM(s.e, s.i, UserPrincipal.FindByIdentity(context, s.i.CreatedBy).DisplayName)) 
               .ToList(); 

       List<MyDraftsVM.MyDraftVM> userCorrespondence = new List<MyDraftsVM.MyDraftVM>(); 
       userCorrespondence = _repoExhibitor.Context.CorrespondenceSenders 
                .Join(_repoExhibitor.Context.Correspondences.Where(x => x.CreatedBy == User.Identity.Name && x.Status == (int)(ModelEnums.Invoice.Status.Przygotowanie)), 
                sen => sen.Id, 
                c => c.Id, 
                (sen, c) => new { sen, c }) 
                .ToList() 
                .Select(s => new MyDraftsVM.MyDraftVM(s.c, UserPrincipal.FindByIdentity(context, s.c.CreatedBy).DisplayName))             
                .ToList();              

       dataVM.Documents.AddRange(userInvoices); 
       dataVM.Documents.AddRange(userCorrespondence); 
      } 
      return dataVM; 
     } 

MyDraftsVMクラスには、次のようになります。

public class MyDraftsVM 
    { 
     public MyDraftsVM() 
     { 
      this.Documents = new List<MyDraftVM>(); 
      this.Layout = "~/Views/Shared/_LayoutBox.cshtml"; 
     } 

     public List<MyDraftVM> Documents { get; set; } 
     /// <summary> 
     /// layout path 
     /// </summary>  
     public string Layout { get; set; } 

     public class MyDraftVM 
     { 


      public MyDraftVM() 
      { 
       this.DocumentPartial = new DocumentMemebership(); 
       this.InvoicePartial = new InvoiceMembership(); 
       this.ExhibitorPartial = new ExhibitorMembership(); 
       this.CorrespondencePartial = new CorrespondenceMembership(); 
      } 

      public MyDraftVM(Exhibitor e, Invoice i, string createdBy) 
      {    
       InvoicePartial = Mapper.Map<MyDraftsVM.MyDraftVM.InvoiceMembership>(i); 
       ExhibitorPartial = Mapper.Map<MyDraftsVM.MyDraftVM.ExhibitorMembership>(e); 
       DocumentPartial = new MyDraftsVM.MyDraftVM.DocumentMemebership() 
       { 
        DocumentType = "Invoice", 
        Number = i.InvoiceNumber, 
        IssuingDate = i.IssuingDate, 
        CreatedBy = createdBy 
       }; 
      } 

      public MyDraftVM(Correspondence c, string createdBy) 
      { 
       CorrespondencePartial = Mapper.Map<MyDraftsVM.MyDraftVM.CorrespondenceMembership>(c); 
       DocumentPartial = new MyDraftsVM.MyDraftVM.DocumentMemebership() 
       { 
        DocumentType = "Correspondence", 
        Number = c.Signature, 
        IssuingDate = c.IssuingDate, 
        CreatedBy = createdBy, 
       }; 
      } 

      public DocumentMemebership DocumentPartial { get; set; } 
      public InvoiceMembership InvoicePartial { get; set; } 
      public CorrespondenceMembership CorrespondencePartial { get; set; } 
      public ExhibitorMembership ExhibitorPartial { get; set; } 


      public class InvoiceMembership : InvoiceVM 
      { 
       public virtual int Id { get; set; } 
      } 


      public class CorrespondenceMembership : CorrespondenceVM 
      { 

      } 


      public class ExhibitorMembership : ExhibitorVM 
      { 
      } 


      public class DocumentMemebership : DocumentVM 
      { 
      } 


     } 

    } 

私はここにrepositoryjoinsで複雑なクエリを移動したいのですが、問題が表示されます。プロジェクトMyApplicationName .Webに参照MyApplicationName.Repositoryがあるので、私はこの問題を「相互作用プロジェクトの問題」と呼びます。クエリをMyApplicationName.Repositoryに移動したい場合は、MyApplicationName.Webに属しているMyDraftsVM objectに移動する必要があります。 私が今見ている唯一の解決策は、別のプロジェクトとしてViewModelsフォルダを分けて、このプロジェクトに参照MyApplicationName.WebMyApplicationName.Repositoryを与えることです。私はこれが良い考えであるかどうかわからないので、私はあなたに尋ねているのです。 良い考えではない場合は、複雑なクエリをrepositoryに移動するより良い解決策を教えてください。ありがとうございました。

答えて

0

あなたは正しい方法で、別のレポ・プロジェクトでクエリを移動しています。しかし、MyApplicationName.Repositoryはあなたのビューモデルについてあなたのウェブプロジェクトについて知ってはいけません。ドメインモデルでのみ動作します。したがって、私がお勧めするオプションは、あなたのドメインとAutoMapperのようなビューモデルの間でいくつかのマッピングツールを使用することです。 これはあなたの建築上の問題を解決するはずです。

関連する問題