That is lovely, thank you.  Exactly what I had in mind, but I had no
idea how to express it in code.

Now I must see how easily this is mappable with Fluent NHibernate.

David


On Mar 10, 5:43 pm, Fabio Maulo <[email protected]> wrote:
> For this
> public interface IAnnotable
> {
> IEnumerable<Note> Notes { get; }
>
> }
>
> public class Note: Entity
> {
> public virtual string Text { get; set; }
> public virtual IAnnotable Annotable { get; internal set; }
>
> }
>
> public class Book : Entity, IAnnotable
> {
> public virtual string Text { get; set; }
> private ISet<Note> notes;
>
> protected Book()
> {
> notes = new HashedSet<Note>();}
>
> public virtual IEnumerable<Note> Notes
> {
> get { return notes; }}
>
> public virtual void Add(Note note)
> {
> notes.Add(note);
> note.Annotable = this;}
>
> public virtual void Remove(Note note)
> {
> notes.Remove(note);
> note.Annotable = null;
>
> }
> }
>
> public class Author : Entity, IAnnotable
> {
> public virtual string Name { get; set; }
> private ISet<Note> notes;
>
> protected Author()
> {
> notes = new HashedSet<Note>();}
>
> public virtual IEnumerable<Note> Notes
> {
> get { return notes; }}
>
> public virtual void Add(Note note)
> {
> notes.Add(note);
> note.Annotable = this;}
>
> public virtual void Remove(Note note)
> {
> notes.Remove(note);
> note.Annotable = null;
>
> }
> }
>
> The mapping is:
> private void ConfOrmMapping(ObjectRelationalMapper orm, Mapper mapper)
> {
> mapper.Customize<Note>(map=> map.Property(x=> x.Text, p=>
> p.Type(NHibernateUtil.StringClob)));
>
> }
>
> On Thu, Mar 10, 2011 at 2:31 PM, José F. Romaniello
> <[email protected]>wrote:
>
>
>
>
>
>
>
> > yes, is that even possible?
> > here is a better code:https://gist.github.com/864517
>
> > 2011/3/10 Fabio Maulo <[email protected]>
>
> >> All Notes in a single table ?
>
> >> On Thu, Mar 10, 2011 at 12:11 PM, José F. Romaniello <
> >> [email protected]> wrote:
>
> >>> woops sorry, you are right.
> >>> in your case I don't think this is right:
>
> >>> public class Note
> >>> {
> >>>  public virtual string Text { get; set; }
> >>>  public virtual List<Book> Books { get; set; }
> >>>  public virtual List<Author> Authors { get; set; }
> >>>  // .. etc
> >>> }
>
> >>> You could, but you don't need to create bidirectional relationships
> >>> always.
>
> >>> you can have:
>
> >>> public class Book
> >>> {
> >>>    IEnumerable<Note> Notes
> >>> }
> >>> public class Author
> >>> {
> >>>    IEnumerable<Note> Notes
> >>> }
>
> >>> public class Publisher
> >>> {
> >>>   IEnumerable<Note> Notes
> >>> }
>
> >>> public class Note
> >>> {
> >>>   string Description ....
> >>> }
>
> >>> All classes will have a <many-to-many with points to three differents
> >>> Tables BookNotes, PublisherNotes, AuthorNotes.
>
> >>> Do you need to know to who belongs the Note? i think no.
>
> >>> Another option is to use <any as described here, to know who belongs the
> >>> note.
> >>> Then you can have an interface named ICommentable o IAnnotable:
>
> >>> public interface IAnnotable
> >>> {
> >>>    public IEnumerable<Notes> {get; }
> >>> }
>
> >>> public class Note
> >>> {
> >>>    public string Description {get;set;}
> >>>    public IAnnotable Annotable {get;set;}
> >>> }
>
> >>> public class Book : IAnnotable
> >>> {
> >>>    public IEnumerable<Note> Notes { get; private set;}
> >>> }
>
> >>> public class Author : IAnnotable
> >>> {
> >>>    public IEnumerable<Note> Notes { get; private set;}
> >>> }
>
> >>> "y ahora quien podra ayudarnos?" /cc Fabio Maulo: paste the generated
> >>> mappings :)
>
> >>> 2011/3/10 David <[email protected]>
>
> >>> Jose, was your reply meant for the thread "Mapping for many-to-many
> >>>> representing user roles" ?
>
> >>>> Any assistance with my previous questions would be greatly
> >>>> appreciated.
>
> >>>> David
>
> >>>> On Mar 9, 10:12 pm, José F. Romaniello <[email protected]> wrote:
> >>>> > In your user mapping:
>
> >>>> > <set name="Roles" table="UserRoles">
> >>>> >     <key column="UserId"/>
> >>>> >     <element column="Role"
> >>>> > type="NHibernate.Type.EnumStringType`1[[YourNamespace.RoleEnum,
> >>>> > YourAssembly]]"/>
> >>>> > </set>
>
> >>>> > your user class:
>
> >>>> > public class User
> >>>> > {
> >>>> >     private ICollection<Role> roles = new HashSet<Role>();
> >>>> > ...
> >>>> >     public ICollection<Role> Roles { get { return roles; } }
>
> >>>> > }
>
> >>>> > You don't need to map roles.
>
> >>>> > 2011/3/9 David <[email protected]>
>
> >>>> > > Thanks, I've had a think about this now and drawn out a few
> >>>> potential
> >>>> > > diagrams.  Imagining my previous scenario of Categories, Books and
> >>>> > > Publishers and the desire to add Notes.
>
> >>>> > > Database wise, there are three ways I see of accomplishing this.
> >>>> > > Option 1 is a notes table for each item, i.e. BookNotes,
> >>>> CategoryNotes
> >>>> > > and the associated C# classes for this.  No difficulty with code,
> >>>> but
> >>>> > > ugly and inextensible.
>
> >>>> > > Option 2 is one notes table and a mapping table for each, e.g.
>
> >>>> > > public class Book
> >>>> > > {
> >>>> > >  public virtual string Title { get; set; }
> >>>> > >  public virtual List<Note> Notes { get; set; }
> >>>> > > }
>
> >>>> > > public class Note
> >>>> > > {
> >>>> > >  public virtual string Text { get; set; }
> >>>> > >  public virtual List<Book> Books { get; set; }
> >>>> > >  public virtual List<Author> Authors { get; set; }
> >>>> > >  // .. etc
> >>>> > > }
>
> >>>> > > I assume from your post that this is what you meant by the HasMany,
> >>>> > > and I assume that this is how you would implement the Note class
> >>>> (with
> >>>> > > multiple lists).  Can you confirm that the example implementation
> >>>> > > above is correct?
>
> >>>> > > Option 3 would be some form of class that any item which supports
> >>>> > > "notes" can derive from, with a mapping table i.e.:
>
> >>>> > > ItemFk | ItemType | NoteFk
> >>>> > > 1 | Book | 2
> >>>> > > 4 | Author | 2
> >>>> > > ...
>
> >>>> > > I suspect this is a many-to-any, but have no idea how to implement
> >>>> > > this in C# code or whether it is supported like this by nhibernate.
> >>>> > > This seems to be the most flexible way of implementing notes/tags/
> >>>> > > categories that I can think of, but obviously requires a bit more
> >>>> > > code.  If this isn't impossible or crazy, can anybody suggest how
> >>>> many-
> >>>> > > to-any would look in a few C# classes/interfaces?
>
> >>>> > > Thanks for your help.
>
> >>>> > > David
>
> >>>> > > On Mar 6, 8:14 pm, "Drew Jones" <[email protected]> wrote:
> >>>> > > > The HasMany convention should help you set this up. Author, Book,
> >>>> and
> >>>> > > > Publisher. If you plan on attaching the SAME note to several of
> >>>> these,
> >>>> > > use
> >>>> > > > HasManyToMany.
>
> >>>> > > > -----Original Message-----
> >>>> > > > From: David
> >>>> > > > Sent: Sunday, March 06, 2011 12:53 PM
> >>>> > > > To: nhusers
> >>>> > > > Subject: [nhusers] Many-to-many different classes
>
> >>>> > > > I am using nhibernate but I seem to be making my life harder and
> >>>> > > > harder as I plan my design.
>
> >>>> > > > I have a bunch of domain objects which are linked together in an
> >>>> > > > entirely normal way.  For the purposes of example, these could be
> >>>> > > > Authors, Books and Publishers.
>
> >>>> > > > However I would like to extend this so that these items can have
> >>>> other
> >>>> > > > attributes linked to them, e.g. Notes.  An individual Book could
> >>>> have
> >>>> > > > multiple notes, as could an Author or Publisher.  The same sort of
> >>>> > > > system could work for categories, tags[1] or a bunch of other
> >>>> > > > attributes.
>
> >>>> > > > Is there a sensible way of designing a domain model (and database
> >>>> > > > structure) around this without having a mapping table for each?  I
> >>>> > > > have not seen any examples where one domain model (e.g. Notes) has
> >>>> > > > multiple lists of other objects that could relate to it.
>
> >>>> > > > I have only seen the following, which could get complicated pretty
> >>>> > > > quickly and introduces a whole unnecessary layer of complexity:
>
> >>>> > > > Author <-> AuthorNotes <-> Notes
> >>>> > > > Book <-> BookNotes <-> Notes
> >>>> > > > Publisher <-> PublisherNotes <-> Notes
>
> >>>> > > > It would be great to hear about alternative ways of defining these
> >>>> > > > sorts of relationships.
>
> >>>> > > > 1 - I know there are alternative methods of implementing this and
> >>>> will
> >>>> > > > be considering them.
>
> >>>> > > > --
> >>>> > > > You received this message because you are subscribed to the Google
> >>>> Groups
> >>>> > > > "nhusers" group.
> >>>> > > > To post to this group, send email to [email protected].
> >>>> > > > To unsubscribe from this group, send email to
> >>>> > > > [email protected].
> >>>> > > > For more options, visit this group athttp://
> >>>> > > groups.google.com/group/nhusers?hl=en.
>
> >>>> > > --
> >>>> > > You received this message because you are subscribed to the Google
> >>>> Groups
> >>>> > > "nhusers" group.
> >>>> > > To post to this group, send email to [email protected].
> >>>> > > To unsubscribe from this group, send email to
> >>>> > > [email protected].
> >>>> > > For more options, visit this group at
> >>>> > >http://groups.google.com/group/nhusers?hl=en.
>
> >>>> --
> >>>> You received this message because you are subscribed to the Google
> >>>> Groups "nhusers" group.
> >>>> To post to this group, send email to [email protected].
> >>>> To unsubscribe from this group, send email to
> >>>> [email protected].
> >>>> For more options, visit this group at
> >>>>http://groups.google.com/group/nhusers?hl=en.
>
> >>>  --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "nhusers" group.
> >>> To post to this group, send email to [email protected].
> >>> To unsubscribe from this group, send email to
> >>> [email protected].
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/nhusers?hl=en.
>
> >> --
> >> Fabio Maulo
>
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "nhusers" group.
> >> To post to this group, send email to [email protected].
> >> To unsubscribe from this group, send email to
> >> [email protected].
> >> For more options, visit this group at
>
> ...
>
> read more »

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to