如何优化C#中的冗余代码? [英] How to optimize redundant code in c#?

查看:522
本文介绍了如何优化C#中的冗余代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下班级。(我知道这不是一个好习惯):

  public class BussinesRuleA 
{
private字符串_connectionString;

public BussinesRuleA(string connectionString)
{
_connectionString = connectionString;
}

public List< persitenceRuleA> getDATA_A(persitenceRuleA perRA,int acao)
{
// EDITED:IT'S强制make这个DATA ACCESS类的新实例
//将connectionString从构造函数中移除
dalRuleA dalRA = new dalRuleA();
列出< persitenceRuleA> lst = new List< persitenceRuleA>();
尝试
{
lst = dalRA.getDATA(perRA,acao);
}
catch(异常e)
{
throw e;
}
finally
{
dalRA = null;
}
return lst;
}
}

我想用通用的方式做同样的事情。我如何重新创建上述方法的代码?
我尝试做下面的代码,但它不起作用。 EDITED:方法的名称已更改

  public List< TPer> getDATA_Generic< TPer,TDal>(TPer per,int acao)
其中TDal:new()
{
TDal _dal = new TDal();
列表< TPer> _lst =新列表< TPer>();
尝试
{
_lst = _dal.getDATA(TPer,acao); // ** EDITED **:调用getDATA方法已更改

catch(Exception e)
{
throw e;
}
finally
{
_dal = default(TDal);
}
return _lst;
}

EDITED :上面的代码有效,但我不想做:

  public List< TPer> getDATA< TPer,TDal>(TPer per,int acao)

EDITED :相反,我想要这样的东西:

  public List< TPer> getDATA< TPer>(TPer per,int acao)

EDITED :在方法中创建一个TDal的新实例,我不知道是否可能,或者是否存在解决此问题的解决方法:

  TDal _dal = new TDal(); 

实际上我有几个类,就是这样的:

  BussinesRuleA:方法getDATA,调用persitenceRuleA,dalRuleA 
BussinesRuleB:方法getDATA,调用persitenceRuleB,dalRuleB
BussinesRuleC:方法getDATA,调用persitenceRuleC, dalRuleC

我想减少重写代码,避免必须写很多方法,我想使用带有泛型的TPer和TDal来做到这一点:

  BussinesRuleA:method getDATA< T> ;,调用TPer,TDal 
BussinesRuleB:方法getDATA< T> ;,调用TPer,TDal
BussinesRuleC:方法getDATA< T> ;,调用TPer,TDal


解决方案

不可能对特定的构造函数可用性进行通用约束,所以您不能保证 TDal _dal = new TDal(_connectionString); 是可能的e。

我会重构它,然后在外部提供dal:

 公开列表< TRule> getData< TRule>(TRule perRA,IDal< TRule> dalRA,int acao)
{
List< TRule> list = new List< TRule>();
尝试
{
list = dalRA.getDATA(perRA,acao);
}
catch(异常e)
{
throw e;
}
finally
{
dalRA = null;
}
返回列表;
}

假设:

 内部接口IDal< TRule> 
{
列表< TRule> getDATA(TRERE perRA,int acao);
}


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

解决方案

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);
}

这篇关于如何优化C#中的冗余代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆