c# - Using same specification for code and NHibernate QueryOver<T> -


scenario

i'm working on application has little section dedicated displaying statistics current state of system. includes such things how many items overdue, count of pending items, count of items without filters. when user opens application must select item labels wish work on before seeing items in ui. once done database gets queried , returns list user. each item has associated label. item label auto-generated before users see it. 1 of these labels "overdue", indicates user should completed first.

problem

i'm struggling find way centralize logic determines if item overdue.

the way simple create future nhibernate query , statistic counts in 1 database transaction. issue once retrieve items database (which assigned label, if item overdue system supposed override label). once data retrieved (after users select labels) parse tree , need check each item whether it's overdue or not. logic quite simple located in 2 places because 2 different parts of system need different purposes.

my view model bound ui contains this:

public bool isoverdue {         {         return             labelname == "please advise" &&             datetimeprovider.current.datetime.adddays(2) > worktobegindatetime;     } }  public void takeactionifoverdue() {     if(isoverdue)     {         labelname = "overdue";         labelbackground = "#123456";     } } 

i call takeactionifoverdue() whenever i'm building tree, overrides label name if required.

and nhibernate query looks this:

domain.locate locatealias = null; status statusalias = null;  var overduecountquery = _session.queryover(() => locatealias)     .select(projections.rowcount())     .joinalias(() => locatealias.status, () => statusalias)     .where(() => statusalias.name == "please advise")     .where(() => locatealias.worktobegindatetime         .isbetween(datetimeprovider.current.datetime.subtract(new timespan(48, 0, 0)))         .and(datetimeprovider.current.datetime))     .where(() => locatealias.iscomplete == false)     .futurevalue<int>(); 

as can see logic determining if item overdue in 2 places.

my treeview (which gets parsed , displayed once user selects label they'd see) looks this.

- heading     - item #1 (foo label)     - item #3 (bar label) - heading     - item #2 (bar label)     - item #4 (bar label)     - item #6 (overdue) 

in scenario user selected label foo label , bar label. sake of example, let's pretend item #6 had label bar label. however, ui displaying has label overdue because due 2 days ago.

and little statistics box looks this.

items overdue: 4 pending items: 45 items: 49 

question

how can centralize logic?

if using nhibernate 3.0 or above, should have specification pattern.

there implementation details here http://www.packtpub.com/article/nhibernate-3-using-linq-specifications-data-access-layer nhibernate 3.0 cookbook.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -