How to use enums with NHibernate and Custom Code

Posts   
 
    
hilmar
User
Posts: 10
Joined: 30-Jul-2010
# Posted on: 30-Jul-2010 16:06:39   

Hi, I'm evaluating LLBLGen as generator for NHibernate and have three questions:

  • How can I use enums in NHibernate? I have int-columns in the database which shall map to enums in the .net project. As far as I've read no type converters are required for this task but I haven't found how else to do this.

  • Some classes need custom code within. Can I just insert a partial class within the folder the generated classes are written in?

  • My old generator added a PropertyChangedEventHandler to the entities. This handler is not used in the generated classes with LLBLGen. Isn't this one needed?

Thanks, Hilmar

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 30-Jul-2010 16:11:39   

hilmar wrote:

Hi, I'm evaluating LLBLGen as generator for NHibernate and have three questions:

  • How can I use enums in NHibernate? I have int-columns in the database which shall map to enums in the .net project. As far as I've read no type converters are required for this task but I haven't found how else to do this.

Simply define a type import file as described here: http://www.llblgen.com/documentation/3.0/Designer/hh_goto.htm#Functionality%20Reference/EnumTypeImports.htm

then re-load the project, the enums should be present in the Type shortcuts editor. specify your fields using the typeshortcuts for the enums. You can rename the type shortcuts in the type shortcuts editor (project menu-> edit type shortcuts)

  • Some classes need custom code within. Can I just insert a partial class within the folder the generated classes are written in?

Yes.

  • My old generator added a PropertyChangedEventHandler to the entities. This handler is not used in the generated classes with LLBLGen. Isn't this one needed? Thanks, Hilmar

NHibernate encourages to use an interceptor for the proxies. See: http://ayende.com/Blog/archive/2007/04/17/Advance-Extending-NHibernate-Proxies.aspx

Frans Bouma | Lead developer LLBLGen Pro
hilmar
User
Posts: 10
Joined: 30-Jul-2010
# Posted on: 09-Aug-2010 17:12:01   

Otis wrote:

hilmar wrote:

  • How can I use enums in NHibernate? I have int-columns in the database which shall map to enums in the .net project. As far as I've read no type converters are required for this task but I haven't found how else to do this.

Simply define a type import file as described here: http://www.llblgen.com/documentation/3.0/Designer/hh_goto.htm#Functionality%20Reference/EnumTypeImports.htm

then re-load the project, the enums should be present in the Type shortcuts editor. specify your fields using the typeshortcuts for the enums. You can rename the type shortcuts in the type shortcuts editor (project menu-> edit type shortcuts)

I just tried it with a typeImports class:

<typeImports>
    <typeImport typeName="Namespace.ProductImageTypes" assemblyFile="bin\Debug\AssemblyName.dll"/> 
</typeImports>

The enum is defined like this:

public enum ProductImageTypes:int
{
        ListPreview=1,
        MainImage=2,
        AdditionalImage=3,
}

In the Process Monitor I can see that the typeImports file is loaded and the assembly dll as well. However I cannot see it in the Type Shortcut Editor but only the System... stuff.

Can I check what went wrong here?

Thanks, Hilmar

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 09-Aug-2010 19:17:57   

Did you compile the enum dll with .NET 4 perhaps? if not, could you attach the dll and the typeimports file along with your llblgenproj file to this thread, or if you want, to a thread in the helpdesk forum (so only you and us can see the thread) and refer there to this thread? Then we can check where things go wrong.

If you compiled the dll in .net 4, you have to make sure that the designer runs on .net 4 as well: open the llblgenpro.exe.config file in a texteditor and uncomment as described in the file the two lines for .net 4, restart the designer and load your project.

Frans Bouma | Lead developer LLBLGen Pro
hilmar
User
Posts: 10
Joined: 30-Jul-2010
# Posted on: 12-Aug-2010 19:27:59   

Otis wrote:

Did you compile the enum dll with .NET 4 perhaps? if not, could you attach the dll and the typeimports file along with your llblgenproj file to this thread, or if you want, to a thread in the helpdesk forum (so only you and us can see the thread) and refer there to this thread? Then we can check where things go wrong.

If you compiled the dll in .net 4, you have to make sure that the designer runs on .net 4 as well: open the llblgenpro.exe.config file in a texteditor and uncomment as described in the file the two lines for .net 4, restart the designer and load your project.

Works fine now, thank you very much.

There is just one issue: I defined all enums as int-enums:


public enum EnumTypeXYZ:int
    {
        Type1=1,
        Type2=2,
    }

Doing this will result in a runtime exception (HibernateException: Can't Parse 2 as EnumTypeXYZ).

A way to prevent this is to give a CustomType in the mapping:

Map(x=>x.EnumTypeXYZField).Access.CamelCaseField(Prefix.Underscore).CustomType(typeof(int));

instead of:

Map(x=>x.EnumTypeXYZField).Access.CamelCaseField(Prefix.Underscore);

Maybe you can integrate this in the next build.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 12-Aug-2010 20:42:00   

I'll pass this back to the dev team to get them to take a look.

Matt

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 13-Aug-2010 10:38:36   

hilmar wrote:

Otis wrote:

Did you compile the enum dll with .NET 4 perhaps? if not, could you attach the dll and the typeimports file along with your llblgenproj file to this thread, or if you want, to a thread in the helpdesk forum (so only you and us can see the thread) and refer there to this thread? Then we can check where things go wrong.

If you compiled the dll in .net 4, you have to make sure that the designer runs on .net 4 as well: open the llblgenpro.exe.config file in a texteditor and uncomment as described in the file the two lines for .net 4, restart the designer and load your project.

Works fine now, thank you very much.

There is just one issue: I defined all enums as int-enums:


public enum EnumTypeXYZ:int
    {
        Type1=1,
        Type2=2,
    }

Doing this will result in a runtime exception (HibernateException: Can't Parse 2 as EnumTypeXYZ).

A way to prevent this is to give a CustomType in the mapping:

Map(x=>x.EnumTypeXYZField).Access.CamelCaseField(Prefix.Underscore).CustomType(typeof(int));

instead of:

Map(x=>x.EnumTypeXYZField).Access.CamelCaseField(Prefix.Underscore);

Maybe you can integrate this in the next build.

And if you don't specify ':int' it works? Which would be odd, as enums by default have their native type set to int.

I'll see if I can reproduce it and check when it will work and when it doesn't

Frans Bouma | Lead developer LLBLGen Pro
hilmar
User
Posts: 10
Joined: 30-Jul-2010
# Posted on: 13-Aug-2010 10:59:10   

Otis wrote:

And if you don't specify ':int' it works? Which would be odd, as enums by default have their native type set to int.

I'll see if I can reproduce it and check when it will work and when it doesn't

Just checked and if I don't specify ":int" it doesn't work either.

My environment is: - Enum project (.NET 3.5) used in LLBLGen and in the application - LLBLGen produces .NET 4.0 project - Application is .NET 4.0

Edit: I'm using FluentNHibernate in Version 1.1.0.685 from May 24th.

Best, Hilmar

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 13-Aug-2010 12:10:49   

Thanks, will look into it.

(edit) it works in hbm mappings. Now testing with fluentnh.

(edit) indeed fails. Wonder whether this is a problem with fluentnh btw.... it doesn't detect if the type is an enum, while it should. I'll mail James. (and give you a workaround, no worries wink )

(edit) James said it was by design this way, as some people wanted the 'char' equivalent. simple_smile Almost done with the workaround in the template, will attach the template to this thread when I'm done.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 13-Aug-2010 14:52:42   

Place the attached template in: <llblgen pro installation folder>\Frameworks\NHibernate\Templates\Shared\Shared

then, re-generate the code. If you map an enum on a byte column, it will also work now in fluentNH.

Attachments
Filename File size Added on Approval
generalTemplateUtils.lpt 19,039 13-Aug-2010 14:52.48 Approved
Frans Bouma | Lead developer LLBLGen Pro
hilmar
User
Posts: 10
Joined: 30-Jul-2010
# Posted on: 13-Aug-2010 16:50:32   

Works great now.

Thanks, Hilmar