Using a custom type mapping with NHibernate

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
picrap
Posts: 11
Joined: Thu 17 Nov 2011 11:34

Using a custom type mapping with NHibernate

Post by picrap » Mon 05 Dec 2011 12:14

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.

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Tue 06 Dec 2011 14:36

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); 
          }

picrap
Posts: 11
Joined: Thu 17 Nov 2011 11:34

Post by picrap » Wed 07 Dec 2011 12:52

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.

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Fri 09 Dec 2011 16:15

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.

picrap
Posts: 11
Joined: Thu 17 Nov 2011 11:34

Post by picrap » Wed 14 Dec 2011 08:09

I got that, and used it. Unfortunately, there is no way to change the HBM.XML Nhibernate temple

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Thu 15 Dec 2011 14:31

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.

Post Reply