Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
775 views
in Technique[技术] by (71.8m points)

generics - How to optimize redundant code in c#?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is not possible to put generic constraint about specific constructor availabilty, so you cannot guarantee inside the method that TDal _dal = new TDal(_connectionString); is possible.

I would refactor it then to provide dal externally:

public List<TRule> getData<TRule>(TRule perRA, IDal<TRule> dalRA, int acao)
{
    List<TRule> list = new List<TRule>();
    try
    {
        list = dalRA.getDATA(perRA, acao);
    }
    catch (Exception e)
    {
        throw e;
    }
    finally
    {
        dalRA = null;
    }
    return list;
}

assuming:

internal interface IDal<TRule>
{
    List<TRule> getDATA(TRule perRA, int acao);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...