我试图修复通用存储库的结构。存储库通用
我想了解如何使用一种方法,每次返回正确的存储库。
我有一个发票窗口,每个发票类型保存在不同的表上。所以我创建了一个通用库。现在我已经创建了一个方法来返回发票类型的存储库。
这是我的通用仓库的代码:
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