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

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;
}
4599 次点击
所在节点    程序员
44 条回复
repus911
2015-11-23 11:44:01 +08:00
好在当年没有继续干 C#...

可维护性很好?
单元测试?抽象化或者函数拆分?再不济注释?

你要是说实现一个底层实现 别人用用不知道你怎么实现也就算了 可你发出来并讨论可维护性...再接再厉
wizardforcel
2015-11-23 12:49:41 +08:00
@dbfox 你要说 linq (以及其他 orm )不好配置这个我倒是同意

要说他不好用 返回的对象不比 resultset 好用的多嘛。
dbfox
2015-11-23 13:38:47 +08:00
@wizardforcel

不是很喜欢生成的那一堆东西,频繁增删改字段的时候,你就知道有多么烦了
dbfox
2015-11-23 13:40:16 +08:00
@xujif
@repus911

开发方向不一样,某些类型的项目是不需要写的太严禁的,我就是把 C# 当 PHP 用了

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

https://ex.noerr.eu.org/t/237257

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

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

© 2021 V2EX