2015-10-06 83 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); 
     } 

每次我必须重复这些文档类型的所有情况。 为此,我正在尝试创建一个类,重复它们一次,并且我的类返回正确的存储库。