嘿所有,我似乎到处都有这些类型的方法.
下面的方法想要做这些简单的任务:
所以这里是代码......我想要做的就是将这一点削减到最好和最快的方式来完成这些检查.我不关心数据库连接问题或类似的问题.
private static bool isLastIntervalNewerThanDB(string muiMethod)
{
using (var db = new IntLMPDB())
{
// Try to load a matching record.
LastIntervalUpdated liRec = db.LastIntervalUpdateds.FirstOrDefault(rec => rec.method == muiMethod);
// If it could not be loaded, exit because there's no way to determine if we should run.
if (liRec == null) { throw new Exception(string.Format("Could NOT find LastIntervalUpdated record for muiMethod: {0}", muiMethod)); }
else
{
// we have a valid interval record, so lets check that it has been updated since the last webPostTime.
// put another way, there are three datetime values in the LastIntervalUpdated table. First is the
// interval itself, second is the retrievalTime and third is the webPostTime. Whenever the MUI is
// checked for a new interval if one is found then the code updates the retrievalTime to the current
// instant in time. This tells us what the last interval this application found on its last run was.
// The thrid value is the webPostTime, this time instant is only updated by this very method we're in
// right here. We can use this logic: if the retrievalTime is greater than the webPostTime then there's
// a newer interval that we haven't yet processed and inserted into the databse. So we should run the
// method below and update ALL of the syncable values into the databse. Then we'll set the dbPostTime to
// the current instance. As it goes, if this program then runs again before the interval is updated
// then the dbPostTime will be greater than the retrieval time and we'll know to do nothig. Simple Right? :)
// or check here includes a NULL check on dbPostTime because it's possible that dbPostTime is NULL,
// in the example of the first time the system runs. It might have run a LastUpdate sync and not yet
// done this method, so dbPostTime would be NULL. None of the other columns are allowed to be null.
if (liRec.dbPostTime_EST == null || liRec.retrievalTime_EST > liRec.dbPostTime_EST)
{ return true; }
else { return false; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
And*_*are 10
我认为逻辑很好.我建议你做这样的事情来提高可读性*:
private static bool isLastIntervalNewerThanDB(string muiMethod)
{
using (var db = new IntLMPDB())
{
LastIntervalUpdated liRec
= db.LastIntervalUpdateds
.FirstOrDefault(rec => rec.method == muiMethod);
if (liRec == null)
{
throw new Exception(
string.Format("Could NOT find LastIntervalUpdated record for muiMethod: {0}", muiMethod));
}
return liRec.dbPostTime_EST == null
|| liRec.retrievalTime_EST > liRec.dbPostTime_EST;
}
}
Run Code Online (Sandbox Code Playgroud)
*尽管可读性是主观的我认为一个else后if (liRec == null)仅增加了不必要的嵌套和最终条件可以折叠成一个单一的表达.也永远不要低估长表达式中良好放置的换行符 - 它可以使可读和不可读代码之间的所有区别.
| 归档时间: |
|
| 查看次数: |
592 次 |
| 最近记录: |