[英]Repository Generic
我正在嘗試修復通用存儲庫的結構。
我試圖了解如何使用每次正確的存儲庫都會返回的方法。
我有一個發票窗口,每種發票類型都保存在不同的表中。 因此,我創建了一個通用存儲庫。 現在,我已經創建了一種方法來向我返回發票類型的存儲庫。
這是我的通用存儲庫的代碼:
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表達式。
我該如何解決?
如果要使用存儲庫模式,請選擇工作單元,為什么? 當需要執行多個表插入或操作時,它增加了抽象層,並具有其亮點。通常,您需要維護事務范圍,但是現在有了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);
}
要么
如果您要堅持使用它,請使用通用的DocumentoMerceHelper,或者創建一個簡單的工廠,而不是工廠,而是使用工作單元。
有關上述回購模式的更多信息:
我要保存數據的表是完全相同的,所有這些表都派生自一個類。 我正在嘗試創建一個類,使我返回適合於避免在操作期間出現各種情況的任何時間的存儲庫的類:ViewModel中的My Method 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);
}
我的ViewModel中的我的方法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);
}
每次我必須針對這些文檔類型重復所有情況時。 為此,我試圖創建一個類,在其中重復一次,然后類返回正確的存儲庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.