You'll use the detached criterion a lot. Don't worry if the criterion syntax
seems a bit odd. You'll pick it up fast. For SQL junkies HQL tends to go
down a bit easier...I hear that hql exhibits a little better performance but
we haven't noticed a difference.
A couple notes though, you can't resuse a DetachedCriteria once it has been
passed to activerecord. You can call .Clone though to get a replica of it.
Don't try to over optimize things on your own by using the session or static
properties. You just end up stepping on top of what Nhibernate is doing for
you. Nhibernate and active record have some interest ways to control the
caching...you just have to play with it a bit and in the long run you'll be
a lot happier.

Don't worry about opening and closing sessions....ActiveRecord does a fair
bit of the session handling for you. You'll just end up creating a bunch of
Stack Overflows. If you have a non-trivial situation and believe you need to
circumvent the automatic session scopes then place your code inside of a
using statement:

using(new SessionScope())
{

Do some active record stuff here!

}

ActiveRecord will generally try to use the last sessionscope opened...the
using statement ensures it is disposed properly.

Dan


Checkout my blog @ http://blog.agilejedi.com
Checkout my homepage @ http://www.agilejedi.com


On Wed, Feb 4, 2009 at 10:07 PM, jasonsirota <[email protected]> wrote:

>
> Interesting, that does seem easier. I thought there might be something
> with DC but I couldn't figure out how to create one.
>
> The reason I'm pulling all and caching is every page has a
> "Categories" dropdown list that's needed before any dynamic items are
> pulled from the database. So I could just pull the Name/ID pairs for
> the dropdown list, populate it into a hash and cache it, but I know I
> will need the category objects anyway and the overhead of pre-caching
> the full list on application load is pretty low.
>
> Jason
>
> On Feb 4, 8:03 pm, Daniel Pupek <[email protected]> wrote:
> > Try this:
> > DetachedCriteria crit =
> > DetachedCriteria.For<Category>()
> >                 .SetCacheable(true)
> >                 .AddOrder(new Order("SortOrder", true));
> > Category[] categories = ActiveRecordMediator<Category>.FindAll();
> >
> > On a side not why are you trying to pull all categories? Just pull them
> as
> > they are needed (using find by primary key)...they will be cached as they
> > are pulled and subsequent calls will get the cached version.
> >
> > Dan
> >
> > Checkout my blog @http://blog.agilejedi.com
> > Checkout my homepage @http://www.agilejedi.com
> >
> > On Wed, Feb 4, 2009 at 9:38 PM, jasonsirota <[email protected]> wrote:
> >
> > > Okay I got this to work with this set of code:
> >
> > >            ISessionFactoryHolder holder =
> > > ActiveRecordMediator.GetSessionFactoryHolder();
> > >            ISession session = holder.CreateSession(typeof(Category));
> > >            ICriteria crit = session.CreateCriteria(typeof(Category));
> > >            crit.SetCacheable(true);
> > >            crit.AddOrder(new Order("SortOrder",true));
> > >            IList list = crit.List();
> > >            Category[] defaultCategories = new Category[list.Count];
> > >            list.CopyTo(defaultCategories, 0);
> >
> > > I feel like this is too many lines of code, is there some built in AR
> > > functions. I feel like there should be something in AR
> > > like:
> >
> > > Criteria crit = Category.CreateCriteria(); (a method of
> > > ActiveRecordBase<Category>)
> > > crit.SetCacheable(true);
> > > crit.AddOrder(new Order("SortOrder",true));
> > > Category.FindAll(crit);
> >
> > > or even better, overloads for find all
> >
> > > FindAll(bool cacheable)
> >
> > > Maybe I'm missing something....
> >
> > > On Feb 4, 5:11 pm, jasonsirota <[email protected]> wrote:
> > > > So Ayende,
> >
> > > > Why isn't FindAll() cacheable? It seems like any time you'd want to
> do
> > > > a FindAll(), you'd want them to be cacheable...shouldn't all the
> > > > activerecord generated queries be associated to a cache region, or
> are
> > > > they already?
> >
> > > > Jason
> >
> > > > On Feb 3, 6:20 pm, Ayende Rahien <[email protected]> wrote:
> >
> > > > > you need to enable query caching as well, and you need to mark the
> > > query as
> > > > > cachable.
> > > > > I don't think you can do it using FindAll
> >
> > > > > On Wed, Feb 4, 2009 at 2:41 AM, jasonsirota <[email protected]>
> > > wrote:
> >
> > > > > > For reference, here's my config:
> >
> > > > > >    <activerecord isWeb="true" isDebug="false"
> >
> > >
> threadinfotype="Castle.ActiveRecord.Framework.Scopes.HybridWebThreadScopeInfo,
> > > > > > Castle.ActiveRecord">
> > > > > >        <config>
> > > > > >            <add key="connection.driver_class"
> > > > > > value="NHibernate.Driver.SqlClientDriver" />
> > > > > >            <add key="dialect"
> > > > > > value="NHibernate.Dialect.MsSql2005Dialect" />
> > > > > >            <add key="connection.provider"
> > > > > > value="NHibernate.Connection.DriverConnectionProvider" />
> > > > > >            <add key="connection.connection_string" value="XXX"/>
> > > > > >            <add key="proxyfactory.factory_class"
> > > > > > value="NHibernate.ByteCode.Castle.ProxyFactoryFactory,
> > > > > > NHibernate.ByteCode.Castle" />
> > > > > >            <add key="cache.provider_class"
> >
> > >
> value="NHibernate.Caches.SysCache.SysCacheProvider,NHibernate.Caches.SysCache"
> > > > > > /
> >
> > > > > >            <add key="relativeExpiration" value="300" />
> > > > > >        </config>
> > > > > >    </activerecord>
> >
> > > > > > On Feb 3, 4:37 pm, jasonsirota <[email protected]> wrote:
> > > > > > > I thought that you just had to wrap it in SessionScope, and
> > > > > > > SessionScope is enabled for the full httprequest using the
> > > httpmodule
> >
> > > > > > > However, just to be safe, I wrapped it in a transaction:
> >
> > > > > > >             Category[] defaultCategories;
> > > > > > >             using (new TransactionScope(OnDispose.Commit))
> > > > > > >             {
> > > > > > >                 defaultCategories = Category.FindAll();
> > > > > > >             }
> >
> > > > > > > However, I still get the multiple db calls.
> >
> > > > > > > On Feb 3, 4:23 pm, Stefan Sedich <[email protected]>
> wrote:
> >
> > > > > > > > Do you not need to wrap your call to get all in a
> transaction? I
> > > > > > > > thought second level cache was only commited on commit of a
> > > > > > > > transaction?
> >
> > > > > > > > Cheers
> > > > > > > > Stefan
> >
> > > > > > > > On Wed, Feb 4, 2009 at 10:15 AM, jasonsirota <
> > > [email protected]>
> > > > > > wrote:
> >
> > > > > > > > > After wrestling with the trunk version of ActiveRecord and
> > > NHibernate
> > > > > > > > > and NH-Contrib version numbers I finally got cache support
> > > enabled
> > > > > > for
> > > > > > > > > the AR project, however I'm having trouble getting it to
> work.
> >
> > > > > > > > > The call is something like:
> >
> > > > > > > > > Category.FindAll(new Order("SortOrder"),true)
> >
> > > > > > > > > which returns a list of category sorted by the db field
> > > "SortOrder".
> >
> > > > > > > > > I've enabled caching on the Category object like:
> >
> > > > > > > > > [ActiveRecord("Categories", Cache=CacheEnum.ReadOnly)]
> > > > > > > > > public class Category
> > > > > > > > > ...
> >
> > > > > > > > > However, each time the page loads, it requeries the
> database
> > > instead
> > > > > > > > > of pulling the resulting list from the cache.
> >
> > > > > > > > > SELECT this_.CategoryID as CategoryID4_0_,
> this_.Description as
> > > > > > > > > Descript2_4_0_, this_.SortOrder as SortOrder4_0_,
> this_.Type as
> > > > > > > > > Type4_0_ FROM BudgeterCategories this_ ORDER BY
> this_.SortOrder
> > > asc
> >
> > > > > > > > > Any ideas?
> > > > > > > > > Jason
> >
> > > > > > > > --
> > > > > > > > Stefan Sedich
> > > > > > > > Software Developerhttp://weblogs.asp.net/stefansedich
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to