PropertyAccessException when using memcache with property lazy loading - nhibernate

I'm working on a project that uses NHibernate (v 3.3.0.4000) and memcache (v 3.1.0.4000 which is the latest available).
I have a large field containing pretty big strings. As I do not want to load this field with every call, I've configured the property as "lazy loading" with Fluent Nhibernate .
Everything works fine (my property is loaded on demand) except when memcache is enabled. Iget this exception :
NHibernate.PropertyAccessException: The type System.Object can not be assigned to a property of type System.String setter of XYZ ---> System.ArgumentException: Impossible de convertir l'objet de type 'System.Object' en type 'System.String'.
I guess the problem is related to the version of "NHibernate.Caches.MemCache", which is quite old and doesn't seem to be alive.
I would like to know if anyone has managed to run memcache with lazy properties . If not, is it possible to disable cache for a single property ?

It looks like this was a known bug:
Retrieving object from 2nd cache with lazy property fails
And, fortunately for you, it was fixed in the latest release (3.3.2, as of today)
Just update NHibernate and the problem will be gone.

Related

Ninject InRequest Scope Losing Binding

I'm having a frustrating issue with Ninject and MVC 4.
Here is the code block:
Kernel.Bind<IUserInfo>().To<UserInfo().InRequestScope();
var userInfo = Kernel.Get<IUserInfo>();
Most of the time, this is fine, and I have a user info. Sometimes, though, I get the following error:
Error activating IUserInfo
No matching bindings are available, and the Type is not self-bindable.
Activation path:
1) Request for IUserInfo
Suggestions:
1) Ensure that you have defined a binding for IUserInfo.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.\r\n 5) If you are using automatic module loading, ensure the search path and filters are correct.
I've pared down everything I cant think to, and am at a loss. I don't know why this would fail intermittently. Based on my admittedly limited knowledge of Ninject, there should be no way for the binding to be missing.
I see a lot of references to using the Ninject MVC Nuget packages, but the app as I inherited it does not use those, it initializes Ninject using an ActionFilter. Is this pattern just broken at its core, and somehow interfering with proper binding?
Help?
Take a look at the BindFilter option
https://github.com/ninject/ninject.web.mvc/wiki/Filter-configurations
There is some sort of caching issue I believe, that makes filters behave differently to controllers. This means that the binding can fail, usually under heavy load, but unpredicatably.
It turns out that newer versions of Ninject need more setup for InRequestScope to work. By removing Ninject entirely, and readding references to Ninject, Ninject.Web.Common, and Ninject.Web.MVC, it added the Ninject.Web.Common.cs file that was neccessary for InRequestScope to work.
Previously, it was actually binding InTransientScope, which meant it would get garbage collected, which is non-deterministic, which explains my intermittent issues. I wish it would have thrown exceptions when i tried to bind InRequestScope, but c'est la vie.

Disable implicit binding/injection of non explicitly bound classes in Ninject 2+

If you request an unbound object from NInject, then the default behaviour is (if a suitable constructor is available) appears to be to create an instance of the appropriate object.
I'd like to disable this behaviour (I had a difficult to debug issue because something was auto-bound instead of picking up my custom binding in a module). This question hints that it is possible, but I'm unable to find the answer from the NInject wiki.
Remove the SelfBindingResolver from the kernel components after creation:
kernel.Components.RemoveAll<IMissingBindingResolver>();
kernel.Components.Add<IMissingBindingResolver, DefaultValueBindingResolver>();
The following is a better, more direct way of removing the SelfBindingResolver, without assuming that the DefaultValueBindingResolver is the only other IMissingBindingResolver component:
kernel.Components.Remove<IMissingBindingResolver, SelfBindingResolver>();
It's possible the Remove<T, TImplementation>() method was only added in a recent version of Ninject, but this works for me using Ninject 3.2.2.0.

ICriteriaQuery not supporting AddUsedTypedValues with higher version of NHibernate

In older version of NHibernate there was a method called "AddUsedTypedValues" in ICriteriaQuery. After upgrading to NHibernate 3.1, such a method does not exist. Does anyone know where was that method moved (to another interface) or NHibernate removed support for it explicitly?
Thanks!
Method AddUsedTypedValues was replaced as well as the (AbstractCriterion : )ICriterion internal implementation. AddUsedTypedValues was a way how to inject parameters and they were later called inside ICriterion implementer like sqlStringBuilder.AddParameter(); (so the distance from Adding and usage was significant and not explicit)
Current versions (3+) provides ICriteriaQuery method
IEnumerable NewQueryParameter(TypedValue parameter);
which can be used to get an array of Parameters and then explicitly used when SQL statement is built:
var parameter = criteriaQuery.NewQueryParameter(typedValue).Last()
sqlStringBuilder.Add(parameter);
And now it is clear which parameter is added to a SQL statement. I had to implement my own ICrietrion, so I faced that issue as well...

NH 3.2 Fluent Mapping Lazy Loading

I used NH 3.2 mapping by code and I tryied Nhibernate Mapping Generator http://nmg.codeplex.com/ which looked a great tool.
I found a big difference between my code and theirs. On each class they have a call to the function LazyLoad(). (Although I thinked that it was the default behaviour)
Now I fear that my application doesn't use lazy loading, does someone know the default behaviour of nh 3.2 with mapping by code ? (when we don't call the LazyLoad method)
Regards
Depends on the default-lazy attribute of the hibernate-mapping tag which can be changed in Fluent NHibernate by adding the DefaultLazy.Always() or DefaultLazy.Never() convention.
If no default-lazy attribute is defined (no convention added in Fluent NHibernate), lazy loading is enabled.

How to make Fluent NHibernate ignore Dictionary properties

I'm trying to make Fluent NHibernate's automapping ignore a Dictionary property on one of my classes, but Fluent is ignoring me instead. Ignoring other types of properties seems to work fine, but even after following the documentation and adding an override for the Dictionary, I still get the following exception when BuildSessionFactory is called:
The type or method has 2 generic parameter(s), but 1 generic argument(s) were provided. A generic argument must be provided for each generic parameter.
I've tried overriding by property name:
.Override<MyClass>(map => {
map.IgnoreProperty(x => x.MyDictionaryProperty);
})
and also tried implementing ignores using a custom attribute, both of which result in the same exception from BuildSessionFactory. The only thing so far that makes this exception go away is removing the Dictionary property entirely.
My question seems to be identical to this one which was never answered (though I'll expand the scope by stating it doesn't matter whether the dictionary is on an abstract base class; the problem always happens for me regardless of what class the property is on). Any takers this time around?
If you update to the latest version of Fluent NHibernate, IDictionarys are explicitly ignored automatically for this exact reason. This will remain the case until we can actually support them.