-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the Nerula wiki!
Using in memory database increases speed, but it has several gotchas. Creation is simple enough:
var configuration = new Configuration()
.SetProperty(Environment.ReleaseConnections, "on_close")
.SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
.SetProperty(Environment.ShowSql, "true")
.SetProperty(Environment.ConnectionString, "data source=:memory:")
Most important:There is a can be only one session, once any session is closed, SQLite will destroy database once connection to it is closed and that happens when session is closed (at latest). This has several unpleasant consequences
- When creating schemas using SchemaExport, you have to use the new SchemaExport(cfg).Export(true,true,false, session.Connection, null), the short version .Export(true,true,false) will create its session and closes it = the created schema is immediately destroyed and not available onward.
- You must set "Connection Release Modes" to "on_close" so NHibernate closes connection once you dispose of session. There are also other settings that can close connection sooner = db is destroyed even earlier (NHibernate 2.0, SQLite, and In-Memory Databases)
It is great to use IntelliSense, it is second greatest advancement after garbage collection. Everyone want it, in order to have IntelliSense for mapping files, simply
- Open Visual Studio, open some XML file so you can see XML menu item.
- Go to menu XML -> Schema, Click on the Add... button.
- Select and open nhibernate-mapping.xsd (it is in NHibernate package)
Great, that gives us IntelliSense about tokens (tags and attributes), but we want more, e.g. comments about default attribute values and what is typed tag for. In order to have that you need to download slightly modified xsd file rejected bug NH-2552 from ArnonA.
.NET has finally implemented ISet<> properly in version 4.0. Before that we had to use Iesi.Collections
, but now we have two ISet - one from .NET and one from Iesi. It would be nice to use .NET version everywhere. It is possible, even easy thanks to wonder of NuGet as described in .
Using in mappings without Iesi.Collections
Short version:
- Right click on your solution and choose Manage NuGet Packages for the Solution
- Search online for package NHibernate.SetForNet4
- Install it
It is a very good idea to override equals, but it takes core to do it correctly. The official documentation recommends to use business key equals, but IMO (and others) it is a bad idea. They are mutable and other stuff.
Be careful about proxying, it is a kind of magic with dragons. In some cases of lazy loading, the NH will create a proxy (e.g. when you use session.Load(1)) tah can access few fields without touching DB (e.g. Id, sometimes Equals or etHashCode). When the proxy lazy loads the entity, it stores it in separate instance (i.e. proxy pattern). Because Equals and GetHashType may be called from proxy type, it would return some proxy type (e.g. ProxyProductType), but call some other method, eg private Type GetUnproxiedType() {return GetType();} will be called on separate instance and not on proxy, thus getting the real type of entity.
Very good way to to do following (See How I stopped worrying and override Equals/GetHashCode):
- Check that other is not null and of correct type (beware of proxy).
- If one is transient and other not, return false
- If both are transient return reference equality
- If both are peristent, return id equality
Resources
- Official documentaion
- Ayende - Generic Entity Equality
- How I Learned to Stop Worrying and Override Equals/GetHashCode
- NHibernate Equals implementation with proxies vs. ReSharper
- Why Equals and GetHashCode are so important to NHibernate
- Equals implementation of NHibernate Entities, unproxy question
- NHForge - Identity Field, Equality and Hash Code