ISessionFactoryKeyProvider missing from CommonServiceLocator when upgrading NHibernate from 2.1 to 3.0 - nhibernate

I have a ASP.NET MVC application that uses Spring.NET 1.2 for dependecy injection and NHibernate 2.1 for ORM. It has been running on MVC 3, using S#arp Architecture in a slightly modified version which we have been compiling ourselves, something I am now striving to get rid of. (I.e. I am going to start using referenced SharpArch .dlls instead of pure source code.)
The initialization of Spring is done using tips taken from here, as well as using the SpringServiceLocatorAdapter found here. NH is initialized using SharpArch's NHibernateSession.Init, which is given our two assemblypaths. The mapping is done with HBM XML files.
The decision to move to NH 3 led to a serious case of DLL Hell, due to which I of course had to update FluentNH, NH Validator, SharpArch, Spring, and Castle Windsor binaries. To get things working a lot of manual labor was needed, changing references and doing minor code fixes. This was all expected.
Now I've got the software running well enough that Spring is initialized and the DI done correctly. But when a repository tries using it's Session object, I get the following error.
Error creating object with name '' :
no services of type
'SharpArch.NHibernate.ISessionFactoryKeyProvider'
defined
I've been trying to figure out the cause of this for the best part of a day now.
It seems to me like SpringServiceLocatorAdapter is obsolete, but I can find no information regarding that. Also, simply removing the use of it results in repositories' Sessions being null and thus NullReferenceExceptions. But as far as I know the problem could just as well be the way NH is initialized.
Hopefully someone can point me in the right direction. I'm having a hard time since I'm not the one who setup the system to begin with, and lists of breaking changes for the version upgrades are hard to find.
Thanks a lot! I will of course provide more information if needed.

You need to register an implmentation of ISessionFactoryKeyProvider with spring, the default implementation is DefaultSessionFactoryKeyProvider, in windsor, the statement would look like this:
container.Register(
Component.For(typeof(ISessionFactoryKeyProvider))
.ImplementedBy(typeof(DefaultSessionFactoryKeyProvider))
.Named("sessionFactoryKeyProvider"));

Related

Type reference forwarding in the MonoDroid project requiring it

Regarding to the solution described in this post, a third assembly is required to forward the type resolution to the correct assembly.
When adding this reference to the Android class library project using the type, the forwarding seems to not be done. The reference needs to be added in the Android application project which is the end point of the build process.
Does any solution exist to add the reference embedding the forwarding in the project requiring it ?
I mean, if in my solution architecture I use :
MyApp.Core - PCL
MyApp.Core.Droid - Android class library
MyApp.UI.Droid - Android Application
The usage of System.Net namespace (System.Net.Socket.AddressFamily for example) is done in my ViewModel, which is located in MyApp.Core.Droid (redirection of MyApp.Core with some plugins). In this case, it is more logical (and readable) to have the reference in the MyApp.Core.Droid. But in the fact, the assembly resolution is done (from what I understand) when packaging the application, so in MyApp.UI.Droid. So in this case, the reference needs to be added to MyApp.UI.Droid in order to be found (failed if added to MyApp.Core.Droid).
In this case the solution works, but its quite obvious to understand for a new programmer joining the team which, has not been facing the trouble and understands why this reference needs to be added to the UI project...
I'm not sure my thought is easy to understand by the way I introduce it. Let me know if you need more explanation.
Thanks,
Guillaume.
I'm not entirely sure why this 'fails if added to MyApp.Core.Droid' - it feels like this should be added. However, I know that Xamarin have tweaked and changed the dependency resolution scripts a few times.
With that said, I think the best answer to your question is 'don't worry about it too much' - this is only a small inconveneinve right now and it will be resolved by Xamarin's updates 'soon'.
The current PCL support is something that I and a number of others have worked on in order to make things work. This set of 'hacks' is a workaround for the lack of 'proper PCL' support - it simulates what the Microsoft PCL build platform does on WindowsPhone, WPF, etc, but it isn't a perfect implementation.
Xamarin have now committed to 'proper PCL' support. When that happens then these type-forwarding dependencies will automatically be added. The good news is that this support is perhaps now only days, weeks or at most months away.

NHibernate 3.2 - how do I turn on logging, preferably without Log4Net?

I upgraded my WinForms project to NH 3.2, and saw somewhere that it no longer requires Log4net to produce logging info.
Is this correct?
I now need to turn on logging, but I've been unable to find that reference again.
How do you enable logging in NH 3.2?
I'm not currently using a config file - if that's a requirement, please provide a sample.
Also, I prefer to log to the Visual Studio Output window, but anything that works is fine.
I may have been unduly harsh on log4net...
More than a year ago, I had set it up, adding a section in my app.config file, as suggested in all the blog posts I had read.
It worked for me briefly, and then mysteriously stopped working. There are many, many questions on SO describing similar unpleasant experiences.
Have not needed it since then, so ripped the dll and config out of my project in disgust.
Though I'm certain I've tried this before, and that it silently failed to do any logging whatsoever, I added the following single line back to my code:
log4net.Config.BasicConfigurator.Configure();
and lo and behold, it started logging to the VS Output window, just like I wanted.
So it appears there's no need to configure it at all for simple applications - it does the right thing by default. Presumably, it's the complex configuration that trips people up - it seems far too easy to mess things up.

Example of a simple ASP.NET MVC + NHibernate + Fluent with proper session handling?

I'm new to all of these technologies. I would like to see a simple (not over the top) example of how you would set up a project with these technologies. The most important being the proper NHibernate session handling (using HttpContext). Or we can build off of what I already have.
I've seen several examples of one piece or another but nothing with all of these technologies mixed. I'm having a tough time tying them together.
Right now I have an NHibernateHelper class (<-- see the pastebin link) that someone provided me as an example. I've modified it slightly. I think I need to do some stuff in my Global.asax.cs file but I'm not sure exactly what. I need to somehow initialize the NHibernateHelper by passing its constructor an Assembly... but what kind of Assembly? This is where I get really lost. What am I missing?
In your example, The Assembly parameter is the Assembly that contains all Mapping files (hbm.xml) or Mapping classes (fluent nhibernate).
I'do like to recommend you to read this Tutorial. This is how i usually do, create a new HttpModule that opens a new session once per request and binds it to the web context. At the end of the request the session is closed. This is another example of the same implementation,
although the post is written in pt-BR, the code is in english.

Entity Validation in Sharp Architecture Repository

I have created a new 1.6 Sharp Architecture project.
I have marked my only Entity with HasUniqueDomainSignatureAttribute and one string property marked DomainSignatureAttribute.
I create 2 entities with the same DomainSignature and I'm able to save them both thous having duplicates.
Am I missing some configuration? As I was under the impression that this would work out of the box.
Before saving you should manually check entity for validness. I don't know how it is done now, but in previos versions each entitity had a property IsValid.
Well yes, the Validation method IsValid() is on the Entities.
But from the documentation I get the impression that if using NHibernate and NHibernate.Validators the repositories should validate the entities before Insert and Update.
As in the documentation on nhforge.org
In SharpArch.Data.NHibernate.NHibernateSession following snippet is called each time Init() is used.
Those things make me wonder why it doesn't work.
I could roll my own, but that seems like waste if it's already there.
Can anyone point me in the right direction? Also the SchemaExport util should use the validators when generating the scripts.

Castle-ActiveRecord Tutorial with .NET 3.5 broken?

Has anyone tried the ActiveRecord Intro Sample with C# 3.5?
I somehow have the feeling that the sample is completely wrong or just out of date. The XML configuration is just plain wrong:
<add key="connection.connection_string" value="xxx" />
should be :
<add key="hibernate.connection.connection_string" value="xxx" />
(if I understand the nhibernate config syntax right..)
I am wondering what I'm doing wrong. I get a "Could not perform ExecuteQuery for User" Exception when calling Count() on the User Model.
No idea what this can be. The tutorial source differs strongly from the source on the page (most notably in the XML configuration), and it's a VS2003 sample with different syntax on most things (no generics etc).
Any suggestions? ActiveRecord looks awesome..
(This was too long for a comment post)
[#Tigraine] From your comments on my previous answer it looks like the error lies not with the configuration, but with one of your entities. Removing the "hibernate" corrected the configuration so that it geve you the real error, which appears to be that the entity "Post" is not properly attributed for ActiveRecord to create its mapping.
If you further down in the error that it gives, it likely has some details as to what about "Post" failed.
Some common things include:
THe class does not have the [ActiveRecord] attribute.
There is no property with the [PrimaryKey] attribute.
There is no matching table called "Post" (or "Posts" if PluralizeTableNames is "true").
There is no matching column(s) for attributed properties.
Your attributed properties and public methods are not virtual (this one kills me all the time).
The 'hibernate' portion of the key was removed in NHibernate version 2.0.
This version is correct for NHibernate 2.0 onwards:
<add key="connection.connection_string" value="xxx" />
Edit:
I see that the quickstart doesn't come with the binaries for Castle and NHibernate. You must have downloaded the binaries from somewhere; it would be helpful if you could provide the version number of your NHibernate.dll file.
Confusingly, at least SOME of the quickstart has been updated to be current with NHibernate (NH) 2.0, but the latest 'proper' Castle release is still the 1.0 RC3 (almost a year old now), which does not include NH 2.0.
You can go two ways. You can continue using Castle RC3 and in this case you will indeed need to add the 'hibernate' prefix to your configuration entries. Or you can download a build of Castle from the trunk, which should be running against NH 2.0. The problem with the latter approach is that some of the other breaking changes introduced in NH 2.0 might not be fixed in the quick start.
Delete the "hibernate." part for all configuration entries. Your first example is the correct one.