How do add NOLOCK with nHibernate?

SetLockMode(LockMode.None) or connection.isolation ReadUncomitted does NOT append a NOLOCK to your queries. Ayende goes into the correct answer on his blog: If you’re using <sql-query> you can do the following: <sql-query name=”PeopleByName”> <return alias=”person” class=”Person”/> SELECT {person.*} FROM People {person} WITH(nolock) WHERE {person}.Name LIKE :name </sql-query> Note the WTIH(nolock) appended to the FROM clause.

The repository itself is not usually tested?

Repository interface belongs to domain layer. But implementation belongs to infrastructure or data access layer. This implementation is usually not tested with unit test. It uses ORM API heavily, so it is extremely hard and wasteful to test repository in isolation. This problem is not specific to repositories: Don’t mock types you don’t own. Repository … Read more

List vs Set vs Bag in NHibernate

NHibernate semantics: List: Ordered collection of entities, duplicate allowed. Use a .NET IList in code. The index column will need to be mapped in NHibernate. Set: Unordered collection of unique entities, duplicates not allowed. Use Iesi.Collection.ISet in code (NH prior to v4) or System.Collections.Generic.ISet (NH v4+). It is important to override GetHashCode and Equals to … Read more

Identifying NHibernate proxy classes

You can detect if a class is a NHibernate proxy by casting it to (unsurprisingly) INHibernateProxy. If you need to get the underlying “real” object, use: Session.GetSessionImplementation().PersistenceContext.Unproxy(proxiedObject) You don’t need to test for proxies to call Unproxy; it returns the original parameter if it’s not a proxy. Edit: I now use a different approach to … Read more

JSON.NET and nHibernate Lazy Loading of Collections

I was facing the same problem so I tried to use @Liedman’s code but the GetSerializableMembers() was never get called for the proxied reference. I found another method to override: public class NHibernateContractResolver : DefaultContractResolver { protected override JsonContract CreateContract(Type objectType) { if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType)) return base.CreateContract(objectType.BaseType); else return base.CreateContract(objectType); } }

How can I have NHibernate only generate the SQL without executing it?

You can get the generated sql queries without execution with the following methods: For the NHibernate.Linq queries: public String GetGeneratedSql(System.Linq.IQueryable queryable, ISession session) { var sessionImp = (ISessionImplementor) session; var nhLinqExpression = new NhLinqExpression(queryable.Expression, sessionImp.Factory); var translatorFactory = new ASTQueryTranslatorFactory(); var translators = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImp.EnabledFilters, sessionImp.Factory); return translators[0].SQLString; } For Criteria queries: public … Read more