vendredi 29 mars 2019

Two classes but same object

I have 2 tables, tblA and tblB, with same fields and types. I get datas through linq to sql so I have 2 partial classes clsTblA and clsTblB.

I have a combo to choose tblA or tblB and I have to read in that table and do some query.

What I'am trying to do is evitate to duplicate code to run the same methods. So now I have (in pseudo-code):

if (combo == "A")
{
  List<clsTblA> listUserNow = ctx.clsTblA.Where(p => p.blabla).ToList();
  List<clsTblA> listUserLastYear = ctx.clsTblA.Where(q => q.blabla).ToList();
}
if (combo == "B")
{
  List<clsTblB> listUserNow = ctx.clsTblB.Where(p => p.blabla).ToList();
  List<clsTblB> listUserLastYear = ctx.clsTblB.Where(q => q.blabla).ToList();
}

But I have in mind something like this (in pseudo-code):

SupClsTable clsTblX = null;
if (combo == A)
  clsTblX = new clsTblA();
if (combo == B)
  clsTblX = new clsTblB();

List<clsTblX> listUserNow = tblX.QueryForNow();
List<clsTblX> listUserLastYear = tblX.QueryForLastYear();

Does it exist something like this? I also searched in design pattern but without results.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire