How does TransactionScope roll back transactions?

Essentially TransactionScope doesn’t track your Adapter’s, what it does is it tracks database connections. When you open a DB connection the connections will looks if there is an ambient transaction (Transaction Scope) and if so enlist with it. Caution if there are more the one connection to the same SQL server this will escalate to … Read more

Best way to convert IList or IEnumerable to Array

Which version of .NET are you using? If it’s .NET 3.5, I’d just call ToArray() and be done with it. If you only have a non-generic IEnumerable, do something like this: IEnumerable query = …; MyEntityType[] array = query.Cast<MyEntityType>().ToArray(); If you don’t know the type within that method but the method’s callers do know it, … 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

Moq: Invalid setup on a non-overridable member: x => x.GetByTitle(“asdf”)

In order to control the behavior of a mock object (in Moq, at least), you either need to mock an interface, or make sure that the behavior you’re trying to control is marked virtual. In your comment, I understand it so that the instantiating of _mockArticleDao is done something like this: _mockArticleDao = new Mock<ArticleDAO>(); … Read more

NHibernate vs LINQ to SQL

LINQ to SQL forces you to use the table-per-class pattern. The benefits of using this pattern are that it’s quick and easy to implement and it takes very little effort to get your domain running based on an existing database structure. For simple applications, this is perfectly acceptable (and oftentimes even preferable), but for more … Read more

NHibernate.MappingException: No persister for: XYZ

Sounds like you forgot to add a mapping assembly to the session factory configuration.. If you’re using app.config… . . <property name=”show_sql”>true</property> <property name=”query.substitutions”>true 1, false 0, yes ‘Y’, no ‘N'</property> <mapping assembly=”Project.DomainModel”/> <!– Here –> </session-factory> . .

NHibernate ISession Flush: Where and when to use it, and why?

Briefly: Always use transactions Don’t use Close(), instead wrap your calls on an ISession inside a using statement or manage the lifecycle of your ISession somewhere else. From the documentation: From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection’s state with the state of objects held in … Read more

What are the First and Second Level caches in (N)Hibernate?

1.1) First-level cache First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every … Read more