Page 1 of 1

Using a custom type mapping with NHibernate

Posted: Mon 05 Dec 2011 12:14
by picrap
Hi,

I'm trying to use a custom YesNo (a "OuiNon" since I'm french) and can't figure out how to get it working.
I created a new complex type, inheriting from CharBooleanType (exactly like YesNotType does) named OuiNonType, set it to my property, but instead of being boolean, the property is a OuiNonType (and I don't want to; I wan a bool).

Is there a way to achieve this?

Thanks,
Pascal.

Posted: Tue 06 Dec 2011 14:36
by Shalex
1. Could you please describe the problems you have encountered when the property type in the conceptual part is OuiNonType (not bool)?

2. As a workaround, you should change this piece of code in the GetPropertyTypeName() method in a standard NHibernate template in the following way:

Code: Select all

          else
            propertyDataType = codeProvider.GetNullableType(property.Nullable, property.Type);
-->

Code: Select all

          else{
            propertyDataType = codeProvider.GetNullableType(false, property.Type);
            if (propertyDataType == "OuiNonType")
              propertyDataType = property.Nullable ? "bool?" : "bool";
            else
              propertyDataType = codeProvider.GetNullableType(property.Nullable, property.Type); 
          }

Posted: Wed 07 Dec 2011 12:52
by picrap
1. I am not sure I understand what you mean by "conceptual part", but I want a type just like the NHibernate YesNoType, except that true and false are 'O' and 'N' (I recently discovered a similar thing with '0' and '1' but NH will probably be able to map it to a boolean harmlessly).

2. The workaround does not work, because:
- it loses the relation to a column when using a custom complex type.
- it creates an instance in ctor

So basically, I would like to create my own custom types which would be handled exactly as NH native types are.

Posted: Fri 09 Dec 2011 16:15
by Shalex
We do not offer you to change the generated code but change the template for code generation:
1) right click on the NHibernate template in Model Explorer > Copy To Model Folder;
2) double click on the template, replace some standard code (mentioned in my last post), save the modified template, and generate the code.
picrap wrote:I am not sure I understand what you mean by "conceptual part"
This is a type of your property in the DataModel1.Class1.cs file.

Posted: Wed 14 Dec 2011 08:09
by picrap
I got that, and used it. Unfortunately, there is no way to change the HBM.XML Nhibernate temple

Posted: Thu 15 Dec 2011 14:31
by Shalex
picrap wrote:there is no way to change the HBM.XML Nhibernate temple
The generation of HBM.XML is predefined in our assembly and cannot be changed.
As a solution, you can implement your own template (Model Explorer > Templates > Add Template | Copy to Model Folder) either to generate your own HBM.XML or to edit the one which is generated by our provider.