I have the following class. EDITED: (And I know it's not a good practice):
public class BussinesRuleA
{
private string _connectionString;
public BussinesRuleA(string connectionString)
{
_connectionString = connectionString;
}
public List<persitenceRuleA> getDATA_A(persitenceRuleA perRA, int acao)
{
//EDITED: IT′S MANDATORY make A NEW instance to this DATA ACCESS class
// The connectionString was removed from the constructor
dalRuleA dalRA = new dalRuleA();
List<persitenceRuleA> lst = new List<persitenceRuleA>();
try
{
lst = dalRA.getDATA(perRA, acao);
}
catch (Exception e)
{
throw e;
}
finally
{
dalRA = null;
}
return lst;
}
}
I Want to do the same thing in Generic way. How can I recreate the code to the method above?
I try to do the code below but it′s not working. EDITED: The name of the method was changed
public List<TPer> getDATA_Generic<TPer, TDal>(TPer per, int acao)
where TDal: new()
{
TDal _dal = new TDal();
List<TPer> _lst = new List<TPer>();
try
{
_lst = _dal.getDATA(TPer,acao); //**EDITED**: The call for getDATA method was changed
}
catch (Exception e)
{
throw e;
}
finally
{
_dal = default(TDal);
}
return _lst;
}
EDITED: The code above works, but I dont want to do:
public List<TPer> getDATA<TPer, TDal>(TPer per, int acao)
EDITED: Instead, I want something like this :
public List<TPer> getDATA<TPer>(TPer per, int acao)
EDITED: And create a new instance of TDal inside the method, i dont know if it's possible, or if exists a workaround to solve this issue:
TDal _dal = new TDal();
Actually I have several class, something like that:
BussinesRuleA: method getDATA, call to persitenceRuleA , dalRuleA
BussinesRuleB: method getDATA, call to persitenceRuleB , dalRuleB
BussinesRuleC: method getDATA, call to persitenceRuleC , dalRuleC
I want to reduce the rewrite of code, avoiding have to write a lot of methods, I want to use TPer and TDal with generics to make this:
BussinesRuleA: method getDATA<T>, call to TPer , TDal
BussinesRuleB: method getDATA<T>, call to TPer , TDal
BussinesRuleC: method getDATA<T>, call to TPer , TDal
See Question&Answers more detail:
os