2010-03-19 129 views
2

我想知道您对我们计划的这一部分的认识:这是一个好的设计吗?

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

合作伙伴打电话给我们,让所有营地附近的GPS位置或所有提供酒吧的营地(我们称之为服务)。

那么我是如何意识到的呢?

这里是我们的数据库:

Campsite 
- ID 
- NAME 
- GPS_latitude 
- GPS_longitude 

CampsiteServices 
-Campsite_ID 
-Services_ID 

所以我的代码(C#,但它是不相关的,我们说这是一个面向对象的语言)看起来像这样

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

所以在我的web服务代码:

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没有那么好笑:)

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

如果事情是不明确的请咨询

回答

2

看起来相当清楚,并有可能工作。它与查询对象模式有一点不同(参见Fowler,Martin 企业架构模式。Addison Wesley,2003),但不是太遥远。

它有一个名为Query的类,它具有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 
    } 
} 

查询对象将有你的基本查询:

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工具的位置,当你到达这一点时会变得更加艰难。

+0

我的GPS过滤器是在距离上,所以我将不得不为GPS_latitude字段创建一个距离标准。但无论如何我明白。谢谢 – 2010-03-19 16:01:53

相关问题