2015-10-06 84 views
0

我試圖修復通用存儲庫的結構。存儲庫通用

我想了解如何使用一種方法,每次返回正確的存儲庫。

我有一個發票窗口,每個發票類型保存在不同的表上。所以我創建了一個通用庫。現在我已經創建了一個方法來返回發票類型的存儲庫。

這是我的通用倉庫的代碼:

public class RepositoryBase<T> where T : class,IEntityBase 
{ 
    private readonly DbSet<T> ctx; 
    internal DbContext context; 
    private readonly UtilityDomain utilityDomain; 

    public RepositoryBase(DbContext _context) 
    { 
     context = _context; 
     ctx = _context.Set<T>(); 
     utilityDomain = new UtilityDomain(); 
    } 


    public void Aggiungi(T oggetto) 
    { 
     ctx.Add(oggetto); 

    } 

    public void Elimina(Expression<Func<T, bool>> predicate) 
    { 
     var entityToDelete = ctx.Where(predicate); 
     if (entityToDelete.Count() != 0) 
     { 
      foreach (var entity in entityToDelete) 
      { 
       ctx.Remove(entity); 
      } 
     } 
    }  public T Prendi(Expression<Func<T, bool>> predicate) 
    { 
     var trovato = ctx.FirstOrDefault(predicate); 
     return trovato; 
    } 

    public T PrendiPerId(Guid id) 
    { 
     return ctx.Find(id); 
    } 
    public T PrendiPerId(Guid id,string corpo1,string corpo2,string corpo3) 
    { 
     return ctx.Include(corpo1).Include(corpo2).Include(corpo3).FirstOrDefault(x=>x.Id == id); 
    } 
} 


public interface IEntityBase 
{ 
    Guid Id { get; set; } 
    int NumeroRecord { get; set; } 
    int NumeroRiga { get; set; } 
    string Codice { get; set; } 
    DateTime DataCreazione { get; set; } 
    DateTime DataModifica { get; set; } 
    string UsernameLogin { get; set; } 
    string DatabaseLogin { get; set; } 
    string NomePcLogin { get; set; } 
    string CodiceDittaAssociata { get; set; } 
    string RagioneSocialeDittaAssociata { get; set; } 
} 

以下是我的課應該返回根據類型發票適當的庫:

public static class DocumentoMerceHelper 
{ 
    public static dynamic RepositoryDocumenti(string _tipo4, dynamic nuovoCtx) 
    { 
      switch (_tipo4) 
     { 
      case "VFI": 
       return new RepositoryBase<TestataFatturaImmediataVendita>(nuovoCtx); 
      case "VDT": 
       return new RepositoryBase<TestataDocumentoDiTrasportoVendita>(nuovoCtx); 
      case "VFD": 
       return new RepositoryBase<TestataFatturaDifferitaVendita>(nuovoCtx); 
      case "VNC": 
       return new RepositoryBase<TestataNotaCreditoGenericaVendita>(nuovoCtx); 
      case "VNG": 
       return new RepositoryBase<TestataNotaCreditoGenericaVendita>(nuovoCtx); 
      case "VBU": 
       return new RepositoryBase<TestataBuonoDiConsegnaVendita>(nuovoCtx); 
     } 
    } 
} 

現在我回來一個動態對象,但這種方式我無法訪問存儲庫的所有方法,例如方法「Elimina」方法或我使用謂詞的「Prendi」,因爲顯然在ViewModel中,它告訴我不能使用lambda表達式到一個動態的對象。

這是我的ViewModel呼籲,應該給我回相應的存儲庫中的類中的方法:

private void AggiornaIdDocumentoAcquistoInFatturaVendita(string _tipo4,Guid? _idDocumentoVendita) 
    { 
     var newCtx = RitornaNuovoContesto(); 

     var repositoryDocumento = DocumentoMerceHelper.RepositoryDocumenti(_tipo4, newCtx); 
     var documento = repositoryDocumento.Prendi(x => x.Id == _idDocumentoVendita); 
} 

我在這裏通過的錯誤,因爲我不能使用lambda表達式來動態對象。

我該如何解決這個問題?

+0

爲什麼你返回一個動態類型?只需使工廠方法通用並返回na RepositoryBase

+0

我該如何取回repoitorybase? – Brux88

回答

0

如果你打算爲存儲庫模式去工作單元,爲什麼?它會在您需要執行多個表插入或操作時添加抽象層及其光芒,通常您需要維護事務範圍,但現在使用unitofwork不需要手動處理,因爲所有人都共享數據庫上下文類。

public class UnitOfWork : IDisposable 
    { 
     private DbContext context = new DbContext(); 
     private RepositoryBase<TestataFatturaImmediataVendita> testataFatturaImmediataVenditaRepository; 
     private RepositoryBase<TestataFatturaImmediataVendita1> testataFatturaImmediataVenditaRepository1; 
     continued...... 

     public GenericRepository<TestataFatturaImmediataVendita> testataFatturaImmediataVenditaRepository 
     { 
      get 
      { 

       if (this.testataFatturaImmediataVenditaRepository == null) 
       { 
        this.testataFatturaImmediataVenditaRepository = new RepositoryBase<TestataFatturaImmediataVendita>(context); 
       } 
       return testataFatturaImmediataVenditaRepository; 
      } 
     } 

     public void Save() 
     { 
      context.SaveChanges(); 
     } 

     private bool disposed = false; 

     protected virtual void Dispose(bool disposing) 
     { 
      if (!this.disposed) 
      { 
       if (disposing) 
       { 
        context.Dispose(); 
       } 
      } 
      this.disposed = true; 
     } 

     public void Dispose() 
     { 
      Dispose(true); 
      GC.SuppressFinalize(this); 
     } 

OR

如果你想堅持下去使用通用DocumentoMerceHelper,或創建一個簡單的工廠,但不是工廠去工作單位。

有關如上所定義回購圖案的詳細信息:

  1. http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
+0

你要保存數據的表格是相同的,全部來自一個類。我試圖給我創建一個類,我返回適合的倉庫,以避免在操作過程中有任何時間發生各種情況 – Brux88

0

我要去哪裏保存數據是相同的表中,所有從一個類派生。我試圖創建我的類,我回到合適,以避免任何時間內操作的CRUD有不同的情況下,資源庫: 我的方法Elimina在我的視圖模型:

if (_tipo4 == "VFI") 
     { 
      testataFatturaImmediataVenditaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "VDT") 
     { 
      testataDocumentoDiTrasportoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "VFD") 
     { 
      testataFatturaDifferitaVenditaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "VNC") 
     { 
      testataNotaCreditoVenditaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "VNG") 
     { 
      testataNotaCreditoGenericaVenditaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "VND") 
     { 
      testataNotaDebitoVenditaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "VBU") 
     { 
      testataBuonoDiConsegnaVenditaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "VFG") 
     { 
      testataFatturaGenericaVenditaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "VFA") 
     { 
      testataFatturaAccontoVenditaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "AFI") 
     { 
      testataFatturaImmediataAcquistoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "ADT") 
     { 
      testataDocumentoDiTrasportoAcquistoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "AFD") 
     { 
      testataFatturaDifferitaAcquistoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "ACV") 
     { 
      testataContoVenditaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "ANC") 
     { 
      testataNotaCreditoAcquistoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "ANG") 
     { 
      testataNotaCreditoGenericaAcquistoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "AND") 
     { 
      testataNotaDebitoAcquistoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "ABU") 
     { 
      testataBuonoDiConsegnaAcquistoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "AFG") 
     { 
      testataFatturaGenericaAcquistoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "AAF") 
     { 
      testataAutofatturaRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "AFA") 
     { 
      testataFatturaAccontoAcquistoRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "ARM") 
     { 
      testataRicezioneMerceRepository.EliminaPerId(_idTestata); 
     } 
     if (_tipo4 == "ARI") 
     { 
      testataRimanenzeInizialiRepository.EliminaPerId(_idTestata); 
     } 

我的方法Ricerca在我的視圖模型:

if (_tipo4 == "VFI") 
     { 
      _selectedItem = testataFatturaImmediataVenditaRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoFatturaImmediataVendita"); 
      newTestata = Mapper.Map<TestataFatturaImmediataVendita, TestataDocumento>(
        (TestataFatturaImmediataVendita)_selectedItem); 
     } 
     if (_tipo4 == "VDT") 
     { 
      _selectedItem = testataDocumentoDiTrasportoRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoCorpoDocumentoDiTrasportoVendita"); 
      newTestata = 
       Mapper.Map<TestataDocumentoDiTrasportoVendita, TestataDocumento>(
        (TestataDocumentoDiTrasportoVendita)_selectedItem); 
     } 
     if (_tipo4 == "VFD") 
     { 
      _selectedItem = testataFatturaDifferitaVenditaRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoFatturaDifferitaVendita"); 
      newTestata = 
       Mapper.Map<TestataFatturaDifferitaVendita, TestataDocumento>(
        (TestataFatturaDifferitaVendita)_selectedItem); 
     } 
     if (_tipo4 == "VNG") 
     { 
      _selectedItem = testataNotaCreditoGenericaVenditaRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoNotaCreditoGenericaVendita"); 
      newTestata = 
       Mapper.Map<TestataNotaCreditoGenericaVendita, TestataDocumento>(
        (TestataNotaCreditoGenericaVendita)_selectedItem); 
     } 
     if (_tipo4 == "VBU") 
     { 
      _selectedItem = testataBuonoDiConsegnaVenditaRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoBuonoDiConsegnaVendita"); 
      newTestata = 
       Mapper.Map<TestataBuonoDiConsegnaVendita, TestataDocumento>(
        (TestataBuonoDiConsegnaVendita)_selectedItem); 
     } 
     if (_tipo4 == "VND") 
     { 
      _selectedItem = testataNotaDebitoVenditaRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoNotaDebitoVendita"); 
      newTestata = 
       Mapper.Map<TestataNotaDebitoVendita, TestataDocumento>(
        (TestataNotaDebitoVendita)_selectedItem); 
     } 
     if (_tipo4 == "VFG") 
     { 
      _selectedItem = testataFatturaGenericaVenditaRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoFatturaGenericaVendita"); 
      newTestata = 
       Mapper.Map<TestataFatturaGenericaVendita, TestataDocumento>(
        (TestataFatturaGenericaVendita)_selectedItem); 
     } 
     if (_tipo4 == "VNC") 
     { 
      _selectedItem = testataNotaCreditoVenditaRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoNotaCreditoVendita"); 
      newTestata = Mapper.Map<TestataNotaCreditoVendita, TestataDocumento>(
        (TestataNotaCreditoVendita)_selectedItem); 
     } 
     if (_tipo4 == "VFA") 
     { 
      _selectedItem = testataFatturaAccontoVenditaRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoFatturaAccontoVendita"); 
      newTestata = 
       Mapper.Map<TestataFatturaAccontoVendita, TestataDocumento>(
        (TestataFatturaAccontoVendita)_selectedItem); 
     } 
     if (_tipo4 == "AFI") 
     { 
      _selectedItem = testataFatturaImmediataAcquistoRepository.Prendi(x => x.Id == _idDocumentoRicerca, "CorpoFatturaImmediataAcquisto"); 
      newTestata = 
       Mapper.Map<TestataFatturaImmediataAcquisto, TestataDocumento>(
        (TestataFatturaImmediataAcquisto)_selectedItem); 
     } 

每次我必須重複這些文檔類型的所有情況。 爲此,我正在嘗試創建一個類,重複它們一次,並且我的類返回正確的存儲庫。