这是一个很好的设计吗? [英] Is it a oop good design?

查看:42
本文介绍了这是一个很好的设计吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to know what you think about this part of our program is realized :

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

We have in our database a list of campsite.

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

Partners call us to get all the campsites near a GPS location or all the campsites which provide a bar (we call it a service).

那我是怎么意识到的?

这是我们的数据库:

Campsite
- ID
- NAME
- GPS_latitude
- GPS_longitude

CampsiteServices
-Campsite_ID
-Services_ID

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

So my code (c# but it's not relevant, let say it's an OO language) looks like this

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

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

So in my webservice code :

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 使用起来不是那么有趣:)

1) I don't use ORM for the simple reason that the system exists since 10 years and the only dev who is here since the beginning is starting to learn about difference between public and private.
2) I don't like SP because : we can do override, and t-sql is not so funny to use :)

那你怎么看?清楚吗 ?你有什么图案我应该看看吗?

So what do you think ? Is it clear ? Do you have any pattern that I should have a look to ?

有不明白的请追问

推荐答案

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

Looks reasonably clear, and would probably work. It is a little different from the Query Object pattern (See Fowler, Martin. Patterns of Enterprise Architecture. Addison Wesley, 2003), but not too far off.

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

This has a class named Query, which has a collection of Criterion objects.

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

The Criterion objects would have the operator, field, and filter value to filter on (in Java, sorry):

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 对象将包含您的基本查询:

The Query object would have your base 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 {

    }

}

从那里应该有一个接受查询并完成与数据库对话的服务.

From there there should be a service that will accept a query and do the work of talking to the database.

这将使您处于这样一个位置,当您到达该点时,转向 ORM 工具将不那么困难.

This will put you in a position where moving to an ORM tool will be a little less arduous when you get to that point.

这篇关于这是一个很好的设计吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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