views:

76

answers:

2

Is there a way to generate mappings for NHibernate from POCO? This reply to my question makes NHibernate look very interesting. However i have a lot of classes that look like this

public class register_email_job
{
    public PK id;
    public user_data user;
}

public class user_comment : BaseComment2
{
    [IsNullable]
    public user_comment parent;
    public user_data user;
}

I would like to easily translate this into something compatible with NHibernate. I wouldnbt mind modifying some queries but i prefer not to rewrite each class to use properties and modify class in such a way i need to change how its used everywhere.

-edit- Note that i use inheritance and user_comment has an object to user_comment (thus why it must be nullable. so it doesn't infinitely recurse. null is root).

+3  A: 

You may want to take a look at the Auto Mapping abilities of Fluent NHibernate: http://wiki.fluentnhibernate.org/Auto_mapping

Eric
A: 

In order for NHibernate to construct proxies for your entity classes, you will need to make your non-private members virtual. Public fields will not work with proxy objects, these should be converted to properties.

public class register_email_job
{
    public virtual PK id { get; set; }
    public virtual user_data user { get; set; }
}

Fluent NHibernate is able to create mapping from classes. It can automap based on conventions, or you can write your own mappers.

Your entities and tables may not match the default conventions, there a several ways to override them.

Using classmap, your mapping might look like this:

public class register_email_job_map : ClassMap<register_email_job>
{
    public register_email_job_map()
    {
        Id( x => x.Id );
        References( x=> x.user );
    }
}

public class user_comment_map : ClassMap<user_comment>
{
    public register_email_job_map()
    {
        // properties from BaseComment2
        References( x=> x.user );
        References( x=> x.parent );
    }
}
Lachlan Roche
ugh, this looks like it will take massive work. There are dozens of classes. All simple. Maybe i should take my existing reflection code and tweak it to output mappings. But, this question should go on.
acidzombie24