Mapping a list of components with Fluent NHibernate

by Benny 5. January 2009 20:42

This took me some time to figure out since documentation for Fluent NHibernate is still a little scarce. Let's say we have an entity "ActivityType" which has an icon and a name, but the name is stored for each language so we have a one to many relationship. This relationship is not only modelled in the entity "ActivityType" but also in several others. The translation is modelled in the "LocalizedEntity" class. As illustrated in the class diagram below:

The DB is illustrated in the next diagram, to make it easy the tables have a different name compared to the entity diagram :p.

The mapping for the "Language" entity is pretty straightforward. The mapping for the "ActivityType" and its localizations was a bigger challenge since I got lost in all the delegates after the Component method.

    public class LanuageMapping
        : ClassMap<Language>
    {
        public LanuageMapping()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            WithTable("[7de_Languages]");
        }
    }

    public class ActivityTypeMapping
        : ClassMap<ActivityType>
    {
        public ActivityTypeMapping()
        {
            WithTable("[7de_EventTypes]");
            Id(activityType => activityType.Id);
            Map(activityType => activityType.IconName);
            HasMany<LocalizedEntity>(activityType => activityType.Descriptions)
                .WithTableName("[7de_LocalizedEventTypes]")
                .Component(description =>
                            {
                                description.References<Language>(desc => desc.Language)
                                    .CanNotBeNull();
                                description.Map(desc => desc.Localization)
                                    .CanNotBeNull();
                            });
        }
    }
 

Tags: ,

Software Development

Comments

1/22/2010 1:14:09 PM #

Miguel Marques

Hi, i was mapping a similar model, but the problem is that, using an analogy with your example it says that LocalizedEntity is not mapped.

Miguel Marques Portugal

1/23/2010 3:34:13 PM #

Benny Michielsen

This worked at the time of writing this post. Haven't checked it out recently, will add it on my todo list. Sorry I can't be of any more help at the moment.

Benny Michielsen Belgium

2/4/2010 7:16:01 PM #

Miguel Marques

You are right, i had another problem in the mapping, its works fine

Miguel Marques Portugal

2/25/2010 6:27:05 AM #

Grant Oshields

This is a amazing post, but I was wondering how do I suscribe to the RSS feed?

Grant Oshields United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

Recent Posts

Page List

Recent Comments

Comment RSS