2
votes

I'm a newbie to Fluent Nhibernate (FNH) or NHibernate (or even ORMs) in general. I have a pet project that I'm using to learn FNH and I'm stuck with, what looks like a design issue. Its a basic Library Management System and I have objects like books, users, booksize(!) etc. For instance, I have a BookSize class and its manager BookSizesManager which hold a list of BookSize objects. Could please anyone advise me how to go about creating ClassMap for both of them such that my database (for testing purpose, say a SQLite database) would have only one table called 'BookSizes' and would list all the BookSize objects in BookSizeManager?

My current implementation is as followed and flawed as it produces two tables 1. BookSize 2. BookSizes (from BookSizeManager Map).

My BookSize Class

public class BookSize
{
    public virtual string ID { get; set; }
    public virtual string Name { get; set; }
    public virtual double Length { get; set; }
    public virtual double Width { get; set; }
}

Corresponding ClassMap

public class BookSizeMap : ClassMap<BookSize>
{
    public BookSizeMap()
    {
        Id(x => x.ID);
        Map(x => x.Name);
        Map(x => x.Length);
        Map(x => x.Width);
    }
}

My BookSizesManager Class

public class BookSizesManager
{
    public virtual string Id { get; set; }

    private IList<BookSize> m_bookSizes = new List<BookSize>();
    public virtual IList<BookSize> Items
    {
        get { return new ReadOnlyCollection<BookSize>(m_bookSizes); } 
        set { if(value != null) m_bookSizes = value; }
    }

    public virtual void Add(BookSize size)
    {
        if (size != null)
        {
            m_bookSizes.Add(size); 
        }
    }// Also contains other unimplemented CRUD methods, but haven't listed them here to reduce 'noise'
}

Corresponding ClassMap

public class BookSizesManagerMap : ClassMap<BookSizesManager>
{
    public BookSizesManagerMap()
    {
        Id(x => x.Id);
        HasMany(x => x.Items)
            .Cascade.All();
        Table("BookSizes");
    }
}

Any help is greatly appreciated. Thanks in advance.

1
what does BookSizesManager do except CRUD? i mean when using NHibernate you already have ISession to do the CRUDFiro
Thanks for replying Firo. Yes I intend to make BookSizesManager to have CRUD operations implemented. Hence why the need for two classes. I understand that one can perform CRUD operation using FNH itself.Kunal

1 Answers

0
votes

i would get rid of BookSizesManager completly and use the session directly and specify the tablename explicitly

public class BookSizeMap : ClassMap<BookSize>
{
    public BookSizeMap()
    {
        Table("BookSizes");
        ...
    }
}

BookSizesManager.Add(booksize); becomes session.Save(booksize); BookSizesManager.Get(booksizeId); becomes session.Get(booksizeId);