我試圖修復通用存儲庫的結構。存儲庫通用
我想了解如何使用一種方法,每次返回正確的存儲庫。
我有一個發票窗口,每個發票類型保存在不同的表上。所以我創建了一個通用庫。現在我已經創建了一個方法來返回發票類型的存儲庫。
這是我的通用倉庫的代碼:
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表達式來動態對象。
我該如何解決這個問題?
爲什麼你返回一個動態類型?只需使工廠方法通用並返回na RepositoryBase –
我該如何取回repoitorybase? – Brux88