Nhib1_3-extending the LINQ provider to fix some system. notsupportedexception

Source: Internet
Author: User

With the release of the new version nhib.pdf (3.0 alpha1), I 've decided to give it a try and branch my current solution to switch to this new version and see Hoe it goes.

I was especially interested in the new LINQ support, cause I 've decided to use it as the basis for my data access strategies.

After the necessary reference changes I run all my test suit... and I had bad news from the LINQ provider in the form of some system. notsupportedexception like this one:

"Unittest. test03_linq.querywithequalsguid:
System. notsupportedexception: Boolean equals (system. guid )"

Being Nhibernate an open source project, instead of bothering the guys responsible for the project, my first approach is always to look at the Code; so I downloaded the trunk and started looking around at the LINQ provider. watching how equals () methods are handled by the parser it comes out fast that only the specific version to deal with strings is currently supported [bool equals (string)], all other types have to rely on the = Operator.

But in my code I had a lot of Filters Based on equals () call for various object types (INT, guid and so on ...) and I didn't wanted to touch that code especially considering that with the previous LINQ provider everything was working well.

However the solution is easy, just extend the default generator sgenerator adding the support for the missing methods; but I didn't wanted to compile a specific 'patched' version of nhib.pdf and this post from Fabio maulo confirmed me You can easily extend the LINQ provider. great! That's was exactly what I was looking!

I started working on it and I had my second surprise. the LINQ provider was subhect of a heavy refactoring activity to provide better extensibility from the version you have in 3.0 alpha1. using reflector and looking around in the binaries it comes out that to extend that provider you have to register your extension methods calling the methods of the nhib.pdf. LINQ. functions. functionregistry class. but in all honesty I think that the way it works in alpha2 is way more elegant and it follows better the standard approach Nhibernate have when it comes to configure its components.

So if you have to extend the LINQ provider forget of alpha1 and compile your own version of Nhibernate getting it from the trunk.

Back to the job now: Following Fabio's instructions (and looking at the code) I came out with these classes:

View sourceprint?
Public Class Extendequalsgenerator: basehqlgeneratorformethod
{
Public Extendedequalsgenerator ()
{
// The Methods call are used only to get info about the signature, the actual parameter is just ignored
Supportedmethods =New[] {
Reflectionhelper. getmethoddefinition <byte> (x => X. Equals (byte) 0 )),
Reflectionhelper. getmethoddefinition <sbyte> (x => X. Equals (sbyte) 0 )),
Reflectionhelper. getmethoddefinition <int16> (x => X. Equals (int16) 0 )),
Reflectionhelper. getmethoddefinition <int32> (x => X. Equals (int32) 0 )),
Reflectionhelper. getmethoddefinition <int64> (x => X. Equals (int64) 0 )),
Reflectionhelper. getmethoddefinition <uint16> (x => X. Equals (uint16) 0 )),
Reflectionhelper. getmethoddefinition <uint32> (x => X. Equals (uint32) 0 )),
Reflectionhelper. getmethoddefinition <uint64> (x => X. Equals (uint64) 0 )),
Reflectionhelper. getmethoddefinition <single> (x => X. Equals (single) 0 )),
Reflectionhelper. getmethoddefinition <double> (x => X. Equals (double) 0 )),
Reflectionhelper. getmethoddefinition <Boolean> (x => X. Equals (True)),
Reflectionhelper. getmethoddefinition <char> (x => X. Equals (char) 0 )),
Reflectionhelper. getmethoddefinition <decimal> (x => X. Equals (decimal) 0 )),
Reflectionhelper. getmethoddefinition <guid> (x => X. Equals (guid. Empty )),
};
}
  
Public Override Hqltreenode buildhql (methodinfo method, expression targetobject, readonlycollection <expression> arguments, hqltreebuilder treebuilder, ihqlexpressionvisitor visitor)
{
Return Treebuilder. Equality (
Visitor. Visit (targetobject). asexpression (),
Visitor. Visit (arguments [0]). asexpression ());
}
}
  
Public Class Extendedlinqtohqlgeneratorsregistry: defalinlinqtohqlgeneratorsregistry
{
Public Extendedlinqtohqlgeneratorsregistry ()
{
This. Merge (New Extendedequalsgenerator ());
}
}

After registering them (actually it can be done only in code) using:

View sourceprint?
Configuration. setproperties ("Linqtohql. generatorsregistry","Nhibsions. Extensions. extendedlinqtohqlgeneratorsregistry, nhibstry. Extensions");
  
Or
  
Configuration. linqtohqlgeneratorsregistry <extendedlinqtohqlgeneratorsregistry> ();

My tests passed again and I didn't had to touch a single line of code. This is simply amazing! (But I think the full support for all the equals Methods shocould have been added to the core anyway ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.