【问题标题】:Is it a oop good design?这是一个好的设计吗?
【发布时间】:2010-03-19 23:56:34
【问题描述】:

我想知道您对我们计划的这一部分实现的看法:

我们的数据库中有一个露营地列表。

合作伙伴致电我们以获取 GPS 位置附近的所有露营地或提供酒吧(我们称之为服务)的所有露营地。

那我是怎么意识到的呢?

这是我们的数据库:

Campsite
- ID
- NAME
- GPS_latitude
- GPS_longitude

CampsiteServices
-Campsite_ID
-Services_ID

所以我的代码(c# 但它不相关,假设它是一种 OO 语言)看起来像这样

public class SqlCodeCampsiteFilter{
  public string SqlCode;
  public Dictionary<string, object> Parameters;
}

interface ISQLCampsiteFilter{
   SqlCodeEngineCore CreateSQLCode();
}

public class GpsLocationFilter : ISQLCampsiteFilter{
  public float? GpsLatitude;
  public float? GpsLongitude;
   public SqlCodeEngineCore CreateSQLCode()
          {
    --return an sql code to filter on the gps location like dbo.getDistance(@gpsLat,@gpsLong,campsite.GPS_latitude,campsite.GPS_longitude) with the parameters
  }
}
public class ServiceFilter : : ISQLCampsiteFilter{
  public int[] RequiredServicesID;
   public SqlCodeEngineCore CreateSQLCode()
          {
    --return an sql code to filter on the services "where ID IN (select CampsiteServices.Service_ID FROm CampsiteServices WHERE Service_ID in ...)
  }
}

所以在我的网络服务代码中:

List<ISQLFilterEngineCore> filters = new List<ISQLFilterEngineCore>();
if(gps_latitude.hasvalue && gps_longitude.hasvalue){
  filters.Add (new GpsLocationFilter (gps_latitude.value,gps_longitude.value));
}
if(required_services_id != null){
  filters.Add (new ServiceFilter (required_services_id ));
}
string sql = "SELECT ID,NAME FROM campsite where 1=1"
foreach(ISQLFilterEngineCore aFilter in filters){
  SqlCodeCampsiteFilter code = aFilter.CreateSQLCode();
  sql += code.SqlCode;
  mySqlCommand.AddParameters(code.Parameters);//add all the parameters to the sql command
}
return mySqlCommand.GetResults();

1) 我不使用 ORM 的原因很简单,因为该系统已经存在 10 年了,并且从一开始就在这里的唯一开发人员开始了解公共和私人之间的区别。
2) 我不喜欢 SP,因为:我们可以做覆盖,而 t-sql 用起来不是很有趣:)

那你怎么看?清楚吗 ?你有什么我应该看看的模式吗?

如果有不清楚的地方请追问

【问题讨论】:

    标签: sql oop


    【解决方案1】:

    看起来相当清晰,并且可能会起作用。它与查询对象模式略有不同(参见 Fowler, Martin。企业架构模式。Addison Wesley,2003),但相差不大。

    这有一个名为 Query 的类,它有一个 Criterion 对象的集合。

    Criterion 对象将具有要过滤的运算符、字段和过滤器值(在 Java 中,抱歉):

    Class FloatCriterion implements Criterion {
        String _operator;  // = "="
        String _fieldName; // = "GPS_latitude"
        Float _value;     // = 43.21
    
        String getSql(){
            // build the where criteria
        }
        Param  getValue(){
            // return the param value
        }
    }
    

    Query 对象将包含您的基本查询:

    Class CampsiteQuery implements Query {
        String _baseQuery = "SELECT ID,NAME FROM campsite where 1=1"
        Collection<Criteria> _criteria;
    
        void addCriterion(Criterion crit) {
            _criteria.add(crit);
        }
    
        String buildSql{
            // concat _baseQuery with each Criterion.getSql
        }
    
        List<Param> getParams {
            // build list of params from criteria
        }
    
        List<Campsite> get Results {
    
        }
    
    }
    

    从那里应该有一个服务可以接受查询并与数据库进行对话。

    这将使您处于这样一个位置,当您到达那个点时,迁移到 ORM 工具的难度会小一些。

    【讨论】:

    • 我的 gps 过滤器在距离上,所以我必须为距离创建 1 个标准,而不是为字段 GPS_latitude。但无论如何我明白。谢谢
    • 优秀的答案!
    猜你喜欢
    • 2012-02-10
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2010-09-24
    • 2021-10-15
    相关资源
    最近更新 更多