Access NHibernate.ISession from Castle ActiveRecord

If you are a Castle ActiveRecord user you may currently encountered situations that it is needed to do some specific operation that is possible just via NHibernate or even just need to direct access to NHibernate’s ISession. For example if you want to use LINQ-to-NHibernate or run SQL query with Castle ActiveRecord, there is no way other than direct access to ISession.

To access NHibernate’s ISession you must utilize ActiveRecordMediator.Execute() or ActiveRecordBase.Execute(). This method accepts a NHibernateDelegate delegate. Then through this delegate you have access to NHibernate’s ISession. Consider following code that uses LINQ-to-NHibernate through Castle ActiveRecord:

[ActiveRecord(Lazy = true)]
public class Car: ActiveRecordBase
public static List GetCars()
return (List)Execute(
delegate(ISession session, object instance)
//here you have direct access to ISession

var q = from car in session.Linq()
select car;

return q.ToList();
}, null);

For more examples go to this, this and this link.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *