Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3845280
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:13:12+00:00 2026-05-19T16:13:12+00:00

I attempt to make a more useful sample of the FluentNHibernate tutorial , but

  • 0

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;
        } 
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:13:13+00:00Added an answer on May 19, 2026 at 4:13 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read a bit about a previous attempt to make a C++ standard for
In a attempt to make the current application I'm developing more secure, I've been
First attempt It's difficult to make this question pithy, but to provide a minimal
I have 2 tables (there are more but un related to question) optionValue and
I'm still learning basics of ruby so I thought I'd attempt to make a
How do I make it retry the send attempt if user data is null.
Here in stackoverflow, if you started to make changes then you attempt to navigate
EDIT Here is an attempt to make my question simpler. return this.someFunc(); == return
I'm new to Java and as an attempt to learn more I tried making
I've been attempting to use [UIColor clearColor] with UIToolbar in an attempt to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.