bretddog
bretddog

Reputation: 5529

FluentNHibernate, child collection dependencies

I attempt to make a more useful sample of the FluentNHibernate tutorial, but I'm confused about what type of dependencies the objects have when requested from the repository. Basically I want the objects to be:

  1. Bi-directional; so I can traverse up/down object hierarchy
  2. Decoupled from NHibernate, repository, sessions (all the things I don't understand too well yet)
  3. No-lazy load (Since I don't need it, and it helps me with (2), I think)

It's a bit hard for me to understand if, how and why the example I work on actually satisfy those points or not. But when I request a List and make a break in the code, I see that the child collection lists are of a type:

NHibernate.Collection.Generic.PersistentGenericBag<Store>

..which is a bit too complex for my head..

So, my concrete question is:

  1. What changes are required for complete decoupling?, i.e. to retrieve object hierarchies of simple Lists? (What main concepts, in what classes etc. that must be changed)

I believe this is what I need, since; I write a single-physical-tier application, for single user, with no requirement for "undo-logic". I just want things as loose as possible, as I feel it gives much more reliable code when I use kind of a "push/pull" approach.

Product class:

public   class Product
{
    public int Id { get; private set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public IList<Store> StoresStockedIn { get; private set; }

    public Product()
    {
        StoresStockedIn = new List<Store>();

    }
}

Store class:

public class Store
{
    public int Id { get; private set; }
    public string Name { get; set; }
    public IList<Product> Products { get; set; }
    public IList<Employee> Staff { get; set; }

    public Store()
    {
        Products = new List<Product>();
        Staff = new List<Employee>();
    }


    // AddProduct & AddEmployee is required. "NH needs you to set both sides before
    // it will save correctly" ??

    public void AddProduct(Product product)
    {
        product.StoresStockedIn.Add(this);
        Products.Add(product);
    }

    public void AddEmployee(Employee employee)
    {
        employee.Store = this;
        Staff.Add(employee);
    }
}

Employee class:

   public class Employee
{
    public int Id { get;  private set; }
    public string FirstName { get;  set; }
    public string LastName { get;  set; }
    public Store Store { get; set; }
}

Mapping:

public class ProductMap : ClassMap<Product>
{
    public ProductMap() 
    {
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Name).Length(20);
        Map(x => x.Price).CustomSqlType("decimal").Precision(9).Scale(2);
        HasManyToMany(x => x.StoresStockedIn)
        .Cascade.All()
        .Inverse()
        .Table("StoreProduct");
     }
}

public class StoreMap : ClassMap<Store>
{
    public StoreMap()
    {
        Id(x => x.Id);
        Map(x =>  x.Name);
        HasMany(x => x.Staff)    // 1:m
            .Inverse()           // other end of relation is responsible for saving
            .Cascade.All();     // Tells NH to cascade events
        HasManyToMany(x => x.Products).Cascade.All()
            .Table("StoreProduct");   // Set m:m join table
        // ..only required for bi-directional m:m
    }
}

public class EmployeeMap : ClassMap<Employee> 
{
    public EmployeeMap()
    {
        Id(x => x.Id);  // By default an int Id is generated as identity
        Map(x => x.FirstName);
        Map(x => x.LastName);
        References(x => x.Store).Cascade.None().Not.LazyLoad();    
    }
}

Repository:

    public ICollection<Product> GetAll()
    {
        using (ISession session = FNH_Manager.OpenSession())
        {
            var products = session.CreateCriteria(typeof(Product)).List<Product>();

            return products;
        } 
    }

Upvotes: 0

Views: 835

Answers (1)

TheNameless
TheNameless

Reputation: 363

I don't quite understand why you would want to decouple any further. The public interface you are exposing is already in the form of an IList. You could expose that IList as an IEnumerable. That would make it a readonly collection. (you'd have to map it with FluentNHibernate to a field instead of a property)

The PersistentGenericBag is there for change tracking. For example, how would NHibernate know when an entity was added, or removed from the list?

edit: Turning of lazy-loading is considered premature optimization. It's better to leave it out of the mappings and enable it in your repositories if you have to.

Upvotes: 1

Related Questions