Using types in NHibernate.Type for user types

Kevin Thiart's Avatar

Kevin Thiart

07 Jul, 2010 09:08 AM via web

Hi

I want to map all my boolean properties to char(1) columns using Nhibernate.Type.CharBooleanType.
Some of the other types in the NHibernate.Type namespace will also be useful.

Is it possible to use these types instead of implementing IUserType for all my custom types?
Can I apply the custom type to all compatible properties in a convention similar to UserTypeConvention?

Thanks!

Kevin

  1. 2 Posted by Kevin Thiart on 07 Jul, 2010 10:39 AM

    Kevin Thiart's Avatar

    I'm using IUserType for now, but I've run into another issue:

    If I define a class in my domain model and give it a user type, do I still need to map the class?

    I get the error "An association from the table TABLE_NAME refers to an unmapped class: CLASS_NAME" when building the session factory.

    But if I map the class, I need to specify an ID property, etc, which doesn't make sense in this case.

    Context: I'm trying to implement custom enumeration types similar to this: http://stackoverflow.com/questions/1593773/mapping-custom-enum-clas...

  2. Support Staff 3 Posted by Paul Batum on 07 Jul, 2010 11:08 AM

    Paul Batum's Avatar

    Have you tried using a convention for your user type?
    http://wiki.fluentnhibernate.org/Available_conventions#UserTypeConvention.3CT.3E

  3. 4 Posted by Kevin Thiart on 07 Jul, 2010 12:33 PM

    Kevin Thiart's Avatar

    I've narrowed the issue down to the way I'm using generics.

    I'm beginning to get the idea that what I'm trying to accomplish doesn't make sense:

    I'm trying to create a generic IUserType for all classes that inherit from a class called Enumeration (Note they aren't actual enums, but are classes that behave like enums). The declaration is:

    public class EnumerationType<TEnum>: IUserType { ... }

    And it contains logic that maps between the DB value and an in-memory object of type TEnum.

    Is there a way to apply this UserType globally without creating a convention for every class inheriting from Enumeration?

    I guess I'll have to change my approach a bit and use multiple conventions in the end?

  4. Support Staff 5 Posted by Paul Batum on 11 Jul, 2010 06:19 AM

    Paul Batum's Avatar

    It might be possible to do it with one convention. Here's the code for
    UserTypeConvention:

    public abstract class UserTypeConvention<TUserType> : IUserTypeConvention
            where TUserType : IUserType, new()
        {
            public virtual void Accept(IAcceptanceCriteria<IPropertyInspector>
    criteria)
            {
                var userType = Activator.CreateInstance<TUserType>();

                criteria.Expect(x => x.Type == userType.ReturnedType);
            }

            public virtual void Apply(IPropertyInstance instance)
            {
                instance.CustomType<TUserType>();
            }
        }

    You could make your own version of this that implements the same interface
    but has a slightly different implementation that is aware of the generic
    nature of your user type. I don't know user types very well so I can't
    really give you any concrete advice, but essentially I am thinking that in
    the Apply method you could call CustomType passing a type object (instead of
    using the generic version that takes no parameters), and you could
    dynamically determine the type you pass in by doing some sort of reflection
    or querying the properties on the "instance" argument.

    Hope that helps!

    Paul.

  5. 6 Posted by Kevin Thiart on 16 Jul, 2010 11:14 AM

    Kevin Thiart's Avatar

    That helped a lot! I got it working using the code you posted as a starting point.

    I guess the answer to the original question is a simple "No"?

    Thanks

    Kevin

  6. Support Staff 7 Posted by Paul Batum on 18 Jul, 2010 06:08 AM

    Paul Batum's Avatar

    I'm not sure.. can you show me what the xml would look like?

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What number comes after 20?