c# - NHibernate: Create iCriteria without session -
i want construct icriteria's , pass on function. function open session , transaction. function executes icriteria.list(); , returns list of objects, code below.
i want because want write using(isession session = ...
, using(itransaction transaction = ...
once collecting list of objects. otherwise repeating myself many times.
/// <summary> /// executes icriterion in new session using transaction. /// </summary> /// <typeparam name="t">the type of object operate on.</typeparam> /// <param name="criterion">the criterion list of objects by.</param> /// <returns>the result of <c>(list<t>)session.createcriteria(typeof(t)).add(criterion).list()</c></returns> public static list<t> criteriontolist<t>(icriterion criterion) { list<t> objects = default(list<t>); using (isession session = sessionfactory.opensession()) { using (itransaction transaction = session.begintransaction()) { objects = (list<t>)session.createcriteria(typeof(t)).add(criterion).list<t>(); transaction.commit(); } } return objects; }
the thing icriteria.add() accepts icriterion
.
the question
icriterion
not have .add(..
can't this:
icriterion criterion = restrictions.eq(property, value).add(...
how can still achieve this, should cast icriteria
first so?
icriterion criterion = ((icriteria)restrictions.eq(property, value)).add(...
note: problem converting huge project uses datatables strong typed objects (compatible nhibernate). have many, many compile errors prevents me testing code without converting whole project first.
i think can achieve detachedcriteria. syntax , usage this:
var det = detachedcriteria.for<t>.add(restrictions.eq(prop, val)); using (var session = config.opensession()) using (var txn = session.begintransaction()) { var result= det.getexecutablecriteria(session).list(); }
you encapsulate transaction in separate function:
public ilist<t> getlist<t>(detachedcriteria detachedcriteria) { ilist<t> result; using (var session = config.opensession()) using (var txn = session.begintransaction()) { result = detachedcriteria.getexecutablecriteria(session).list<t>(); txn.commit(); } return result; }
Comments
Post a Comment