HasManyToMany is correct. The behavior of the relationship will be dependent
on how you maintain the relationship in your code and what cascades modes
you've selected.

Some many-to-many relationships don't really need members on both sides of
the relationship. For example, if I was implementing a many-to-many
relationship between Users and Roles, I might do it like this:

public class User
{
  public int Id { get; set; }
  public string Name { get; set; }
  public IList<Roles> { get; set; }
}

public class Role
{
  public int Id { get; set; }
  public string Name { get; set; }
}

I'd do it like this because I never need to navigate to all the users for a
particular role via the domain model (I'll do a query if I want to find all
users with a particular role). When you model many-to-many relationships in
this way, its very easy to get it right.

So with that said, the first question I'd be asking myself is whether Genres
in your domain model really need to know what Shows are related to them.

Now if you really do need to have your classes the way they are, here's what
I would do:

public class Show
{

    public virtual int ID { get; set; }

    public virtual string Name { get; set; }

    private IList<Genre> _genres { get; set; }

    public IEnumerable<Genre> Genres { get { return _genres; }}

    public void AddGenre(Genre g)

    {

      _genres.Add(g);

      g.AddShow(this);

    }}

My Genre Object is:

public class Genre
{

    public virtual int ID { get; set; }


    public virtual string Name { get; set; }


    private IList<Show> _shows { get; set; }

    public IEnumerable<Show> Shows { get { return _shows; }}

    internal void AddShow(Show s)

    {

       _shows.Add(s);

    }}

If you do it this way, theres only one PUBLIC way for a genre to get added
to a show - by calling AddGenre on a show. Once that method completes, your
domain model is consistent - the show has its new genre and the genre has
its new show. If you ensure your domain model is consistent, its easier to
get the logical behavior for how objects are added and removed (you would
use a similar pattern for doing a remove).

IMPORTANT: You will also want to make sure you map the Genre.Shows side of
the relationship as inverse, as NHibernate needs to know which side of the
relationship to actually pay attention to. NH will ignore the inverse side.
See: http://www.nhforge.org/doc/nh/en/index.html#collections-bidirectional

If you are going this route, you'll still have to look at your cascades to
make sure you have the behaviour you want when a Genre or Show is deleted. I
can't remember what the default behaviour is exactly so I'll leave that to
be addressed later if necessary.

Hope it helps,

Paul.

On Wed, Jul 7, 2010 at 4:58 AM, Buddy Lindsey, Jr. <percen...@gmail.com>wrote:

> No the order doesn't matter so I like the idea of using an ICollection or
> ISet never thought of that.
>
> The problem I am having is I don't know what the proper mapping is for
> these 3 tables. I have tried several compinations so it is kind of hard to
> explain which results are giving me what, but some of the issues I have had
> are:
>
> 1) I can add a show and genres by passing the show object with genres
> inside and running the Save() method. It saves both the show and the genres.
> However, when I try to add genres by getting the show object and adding more
> genre objects to the genres collection and do an update() it doesn't save
> the new genres to the database.
>
> 2) I can add shows to a DB with genres and delete show and the relationship
> and keep the genre, but I can't delete a genre explicitly and the
> relationship of it and the show, without deleting the show too.
>
> I have to delete a relationship itself without deleting the a show or a
> genre. Or if i delete a show I need it to delete the show and the
> relationship with a genre.
>
> If I delete a genre I need it to delete the genre and the relationship with
> a show, but not the show.
>
> I can't seem to get the mapping correct to do this. And from there should I
> even be doing a hasmanytomany mapping or something different altogether
> mapping wise?
>
>
> ---
> Buddy Lindsey
> http://www.buddylindsey.com
> http://www.twitter.com/buddylindsey
>
>
> 2010/7/6 Oskar Berggren <oskar.bergg...@gmail.com>
>
> Is the order of Genres assigned to shows important to you? If it is,
>> you will need a column in the database to store the index. If it
>> isn't, don't use an indexed interface like IList<>, use ICollection<>
>> or ISet<> instead.
>>
>> Apart from that, what problems are you experiencing?
>>
>> /Oskar
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Fluent NHibernate" group.
> To post to this group, send email to fluent-nhibern...@googlegroups.com.
> To unsubscribe from this group, send email to
> fluent-nhibernate+unsubscr...@googlegroups.com<fluent-nhibernate%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/fluent-nhibernate?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibern...@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply via email to