2017-06-18 44 views
-1

我们有一个Web API库,它调用业务/服务库(我们的业务逻辑所在的位置),该库又调用一个数据访问库(Repository)。这个班级如何设计得更好?

我们使用这种类型的数据传输对象到处都是。它有一个“付款人”的财产,我们可能不得不过滤(意味着,操纵其价值)。我已经开始执行这项检查,但对我来说感觉很肮脏,因为我在各地都调用了相同的功能。我曾经想过之一:

  1. 使用属性过滤器来处理这个或
  2. 制作上的RequestData类的属性,并做过滤在构造函数中。

有任何其他想法或设计图,其中这可以更有效地设计:

public class Example 
{ 
    private MyRepository _repo = new MyRepository(); 

    private void FilterRequestData(RequestData data) 
    { 
     //will call into another class that may or may not alter RequestData.Payers 
    } 

    public List<ReturnData> GetMyDataExample1(RequestData data) 
    { 
     FilterRequestData(RequestData data); 
     return _repo.GetMyDataExample1(data); 
    } 

    public List<ReturnData> GetMyDataExample2(RequestData data) 
    { 
     FilterRequestData(RequestData data); 
     return _repo.GetMyDataExample2(data); 
    } 

    public List<ReturnData> GetMyDataExample3(RequestData data) 
    { 
     FilterRequestData(RequestData data); 
     return _repo.GetMyDataExample3(data); 
    } 
} 


public class RequestData 
{ 
    List<string> Payers {get;set;} 
} 

回答

0

一个处理这样的重复代码的方法是使用一个函数功能的策略模式(以及可能的一些仿制药取决于你的具体情况)。您可以重构是为单独的类和一切,但基本思路看起来像这样:

public class MyRepository 
{ 
    internal List<ReturnData> GetMyDataExample1(RequestData arg) { return new List<ReturnData>(); } 
    internal List<ReturnData> GetMyDataExample2(RequestData arg) { return new List<ReturnData>(); } 
    internal List<ReturnData> GetMyDataExample3(RequestData arg) { return new List<ReturnData>(); } 
} 

public class ReturnData { } 

public class Example 
{ 
    private MyRepository _repo = new MyRepository(); 

    private List<ReturnData> FilterRequestDataAndExecute(RequestData data, Func<RequestData, List<ReturnData>> action) 
    { 
     // call into another class that may or may not alter RequestData.Payers 
     // and then execute the actual code, potentially with some standardized exception management around it 
     // or logging or anything else really that would otherwise be repeated 
     return action(data); 
    } 

    public List<ReturnData> GetMyDataExample1(RequestData data) 
    { 
     // call the shared filtering/logging/exception mgmt/whatever code and pass some additional code to execute 
     return FilterRequestDataAndExecute(data, _repo.GetMyDataExample1); 
    } 

    public List<ReturnData> GetMyDataExample2(RequestData data) 
    { 
     // call the shared filtering/logging/exception mgmt/whatever code and pass some additional code to execute 
     return FilterRequestDataAndExecute(data, _repo.GetMyDataExample2); 
    } 

    public List<ReturnData> GetMyDataExample3(RequestData data) 
    { 
     // call the shared filtering/logging/exception mgmt/whatever code and pass some additional code to execute 
     return FilterRequestDataAndExecute(data, _repo.GetMyDataExample3); 
    } 
} 

public class RequestData 
{ 
    List<string> Payers { get; set; } 
} 
0

这种思维自然会导致aspect oriented programming。 它专为处理横切问题而设计(例如,在这里,您的筛选功能可以跨越您的查询逻辑)。

由于@dnickless暗示,您可以通过重构呼叫以删除重复的代码。

存在更多通用的解决方案,例如PostSharp,这些解决方案为您提供了一个沿方面构建代码的稍微简洁的方法。它是专有的,但我相信免费的层次足以调查这样的例子。至少看看它在PostSharp中的外观是否有趣,以及您是否认为它可以改善它! (它强烈使用属性,这扩展了第一个建议。)

(注意:我并不是实际上建议为这样一个简单的案例安装另一个库,但强调如何总体检查这些类型的问题。)