分享下我写不需要太严谨的项目的代码风格

2015-11-19 11:41:03 +08:00
 dbfox
public static Common.DB.ResultList GetList(Common.DB.NVCollection queryParams)
{

//string pageString = queryParams["page"] as string ?? "1";
//string psString = queryParams["pagesize"] as string ?? string.Empty;
//string cidString = queryParams["cid"] as string ?? string.Empty;

string sort = queryParams["sort"] as string ?? string.Empty;
string cate = queryParams["cate"] as string ?? string.Empty;
string type = queryParams["type"] as string ?? string.Empty;
string ch = queryParams["ch"] as string ?? string.Empty;
string kind = queryParams["kind"] as string ?? string.Empty;

string andwhere = "1=1";
string rootKey = null;
switch (type)
{
case "appsoft":
rootKey = "iphones";
break;
case "appgame":
rootKey = "iphoneg";
break;
case "azsoft":
rootKey = "soft";
break;
case "azgame":
rootKey = "game";
break;
}

var pnvc = new Common.DB.NVCollection();

if (rootKey != null)
{
var pcate = DBCache.SoftCategoryCache.Get(rootKey);
if (pcate != null)
{
andwhere += " and charIndex(@path,categoryPath)>0 ";
pnvc["path"] = "/" + pcate.ID + "/";
}
}

if (ch == "az")
{
andwhere += " and (charIndex(@path1,categoryPath)>0 or charIndex(@path2,categoryPath)>0)";
pnvc["path1"] = "/" + DBCache.SoftCategoryCache.Get("game").ID + "/";
pnvc["path2"] = "/" + DBCache.SoftCategoryCache.Get("soft").ID + "/";
}
else if (ch == "app")
{
andwhere += " and (charIndex(@path1,categoryPath)>0 or charIndex(@path2,categoryPath)>0)";
pnvc["path1"] = "/" + DBCache.SoftCategoryCache.Get("appgame").ID + "/";
pnvc["path2"] = "/" + DBCache.SoftCategoryCache.Get("appsoft").ID + "/";
}


if (!string.IsNullOrEmpty(cate) && !string.IsNullOrEmpty(rootKey))
{
var cateEnt = DBCache.SoftCategoryCache.Get(rootKey, cate);

if (cateEnt != null)
{
andwhere += " and categoryid=@cid ";
pnvc["cid"] = cateEnt.ID;
}
}




int cid = (queryParams["cid"] as int?) ?? 0;
if (cid > 0)
{
var cateEnt = DBCache.SoftCategoryCache.Get(cid);

if (cateEnt != null)
{
andwhere += " and categoryid=@cid ";
pnvc["cid"] = cateEnt.ID;
}
}

string orderby = " id desc";
switch (sort)
{
case "new":
orderby = " id desc";
break;
case "hot":
orderby = " viewTimes desc,id desc";
break;
case "rank":
orderby = " downWeekTimes desc,id desc";

break;
default:
break;
}

switch (kind)
{
case "number":
andwhere += " and number>0";
orderby = " number desc,id desc ";
break;
case "hprec":
andwhere += " and recommend=1 and homepage=1 and number=0 ";
orderby = " viewtimes desc,id desc ";
break;
case "recnothp":
andwhere += " and recommend=1 and homepage=0 and number=0 ";
orderby = " viewtimes desc,id desc ";
break;
case "rec":
andwhere += " and recommend=1 ";
break;

case "all":

break;

case "normal":
default:
andwhere += " and recommend=0 and homepage=0 and number=0 ";
//orderby = " id desc ";
break;
}





int page = (queryParams["page"] as int?) ?? 1;
if (page <= 0)
{
page = 1;
}

int pagesize = (queryParams["pagesize"] as int?) ?? 10;

if (pagesize <= 0)
{
pagesize = 10;
}

Common.DB.ResultList result = new Common.DB.ResultList(pagesize);

var dbh = Common.DB.Factory.Default;

//List<Common.DB.NVCollection> list = new List<Common.DB.NVCollection>(pagesize);

var query = Common.DB.Factory.DefaultPagerQuery;
query.AbsolutePage = page;
query.Fields = "id,name,version,viewtimes,categoryid";
query.PageSize = pagesize;
query.Sort = orderby;
query.Table = "soft";
query.Where = andwhere;

string csql = query.GetCountQueryString();
string qsql = query.GetQueryString();

int rc = dbh.ExecuteScalar<int>(csql, pnvc);
var ls = dbh.GetDataList(qsql, pnvc);

int pc = Convert.ToInt32(Math.Ceiling((decimal)rc / (decimal)pagesize));


result.Page = page;
result.PageCount = pc;
result.RecordCount = rc;
result.PageSize = pagesize;

for (int i = 0; i < ls.Count; i++)
{
var o = ls[i];

var ent = new Common.DB.NVCollection();
var entcate = DBCache.SoftCategoryCache.Get((int)o["categoryid"]);

ent["id"] = o["id"];
ent["name"] = o["name"];
ent["version"] = o["version"];
ent["cid"] = o["categoryid"];
ent["cpath"] = Services.PathService.GetListPath(entcate);
ent["cname"] = entcate.Name;
ent["path"] = Services.PathService.GetPath(entcate, (int)o["id"]);
ent["dtimes"] = o["viewtimes"];

result.Add(ent);
}

return result;
}
4287 次点击
所在节点    程序员
44 条回复
dong3580
2015-11-19 11:48:06 +08:00
C#?如果是 web 端,看起来一堆可以放到前端判断了,没必要浪费服务器资源,
你这种写法。。。
看不下去了,也不封个方法。。。
wizardforcel
2015-11-19 11:53:54 +08:00
能把一大堆 case 换成 dictionary 嘛
jarlyyn
2015-11-19 12:01:42 +08:00
这有什么分享的价值么……

不是应该写一个类判断用户输入,一个类处理 sql 么。

控制器里负责其他的么……

数据库调一个字段名 /排序要遍历并修改所有相关的不相关的控制器结构。

要杀了接受的程序员的节奏。
vivisidea
2015-11-19 12:02:49 +08:00
我总觉得手动拼接 sql 的方式不仅容易出问题,而且难以维护
jarlyyn
2015-11-19 12:02:57 +08:00
@dong3580

前端判断, ORZ ,收下我的膝盖吧……
liujiangbei
2015-11-19 12:54:39 +08:00
直接被 fire
lifanxi
2015-11-19 13:01:28 +08:00
@dong3580 也许他前端判断了呢?但是不管前端有没有判断,服务器端总应该判断一次。前端判断是性能优化需求,但是服务器判断是正确性需求。
dong3580
2015-11-19 13:18:24 +08:00
@lifanxi
也是也是,哈哈,拼接 sql 语句,不能忍,
dbfox
2015-11-19 18:28:09 +08:00
@jarlyyn 太繁琐,说了是不严谨,按照 传统规格去写,写死我了
dbfox
2015-11-19 18:28:56 +08:00
@dong3580
@vivisidea

很讨厌用框架,尤其是注重性能的地方
dbfox
2015-11-19 18:33:39 +08:00
dong3580
2015-11-19 18:40:06 +08:00
@dbfox
是的。 sql 语句的话如果自己抓东西,还是直接用 sql 语句快点。
但是自己写也可以加参数嘛,你这样拼接代码就有问题,
jarlyyn
2015-11-19 18:40:22 +08:00
@dbfox

和繁琐严谨有什么关系呢?

只不过是把代码适当的区分开而已。能多些几行代码呢?

我觉得,说到底是没被这样的代码坑过而已,又或者不知道这样的代码坑在哪罢了。
bramblex
2015-11-19 18:50:53 +08:00
@dbfox

盲目相信自己写的东西“性能”比框架好是一种盲目且愚蠢的做法,框架本就是平衡多项考虑的工程产物。

当然啦,如果你要自己造玩具当然可以乱来。我造玩具的时候乱来不是一点半点。
longaiwp
2015-11-19 20:33:00 +08:00
@dbfox 这个写的我觉得真是要疯了啊
wizardforcel
2015-11-19 20:54:51 +08:00
@dong3580 后端不判断就等着被渗透吧

@dbfox 过度追求执行效率而忽视开发效率是不对的 性能优化的一条重要原则就是 优化带来的性能提升至少要能抵消代码可读性的下降

你以前用了一个数组存东西 后来发现用 set 会更好一点 这叫优化

你以前用了至少执行几十次的一个循环 后来把循环展开了 这不叫优化 这叫作死
JamesRuan
2015-11-19 21:28:20 +08:00
函数太长,差评!
lawrencexu
2015-11-19 22:22:00 +08:00
C#和 Java 程序员被黑就是因为楼主这样的太多了。
dbfox
2015-11-20 09:30:10 +08:00
@lawrencexu
@jarlyyn
@lawrencexu
@longaiwp
@wizardforcel


可能我表达的不够清楚还是不要管我内部怎么实现了,主要是在 方法的参数上


如果是以前,我会这样定义方法:

public static Common.DB.ResultList GetList(int page,int pagesize,string sort,string type,....)


调用的时候,大家应该都知道,要进行繁琐的类型转换 int.Parse()

需求一旦一改,增加一个参数或者改变一个参数,函数就被破坏掉了,或者要追加一个方法,同时要改变方法内部的实现


public static Common.DB.ResultList GetList(int page,int pagesize,int cid,string sort,string type,....)




后来我想可以增加一个参数类


public class QueryParams
{
public int Page{get;set;}
public int PageSize{get;set;}
public string Sort{get;set;}
public int Cid{get;set;}
}



这时候方法就变成这样:
public static Common.DB.ResultList GetList(queryParams Query);

增加参数,减少参数,都可以通过修改 QueryParams 的属性 和方法内部实现,而不用破坏方法对外的改变


再后来,我发现这样还是比较繁琐,写得太严谨,反而很费时

干脆把参数变更为 和 PHP 相似的 key-object 弱类型,返回值也是弱类型,这样开发效率高了很多倍

在外部传入参数的时候
KeyObjectCollection kvc = new KeyObjectCollection();
kvc["page"] = int.Parse(接收到传输过来的 Page);//这里是已经经过转换的,所以不用担心注入

public static KeyObjectResultList GetList(KeyObjectCollection query){

int page = query["page"] as int; //把 object 类型拆箱,为 int 类型

}

我发现这样更灵活,更方便,这里我本来不想讨论我 sql 拼接,我之前也用过一些框架 entity framework 等,觉得真的不好用,而且我懒得深入学习这些东西,性能和开发效率还有项目的灵活性,都不太容易掌控,所以我不愿意去用,我当然能体会到一些 orm 的好处,但是带来的一些诟病也不少,不如直接我什么都不想,直接拼 sql 好了,诸位大神,也可以说说,你们怎么写的,顺便学习学习。

我追求的东西其实也很简单:就是 - “简”,
只有把项目尽可能做到非常简单,依赖的东西非常少,项目才省心。
就像象棋,很少的规则,可以玩得很有意思。
dbfox
2015-11-20 09:57:53 +08:00
@bramblex 不仅仅是性能,还有框架无法实现的细节,框架也不够灵活,必要的时候还是 tmd 要用 sql

生成出来一堆 code ,在追加字段,更改字段,删除某字段的时候,烦的一笔,发布项目更烦, entity framework 至少是这样

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/237257

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX