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

  • Home
  • SEARCH
  • 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 236565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:21:19+00:00 2026-05-11T20:21:19+00:00

It is pretty common, especially in applications with an ORM, to have a two

  • 0

It is pretty common, especially in applications with an ORM, to have a two way mapping between classes. Like this:

public class Product
{
    private List<Price> HistoricPrices { get; private set;}
}

public class Price
{
    private Product Product { get; set; }
}

Is there an accepted way of maintaining this relationship in code? So that when I add a price to a product the Product property gets set automatically?

Ideally I am looking for an easily reusable solution. It seems wrong to have to add something to a collection and then set the opposite relations manually.

Please note that this is not a question about how to model products and prices, It is a question of how to model a 2 way relationship. There are plenty of situations where this is perfectly reasonable.

  • 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-11T20:21:19+00:00Added an answer on May 11, 2026 at 8:21 pm

    First, I think the example your present is confusing – it’s uncommon for something like a Price to be modeled as an object or to have reference to the entities that would have a price. But I think the question is legitimate – in the ORM world this is sometimes referred to as graph consistency. To my knowledge there isn’t one definitive way to tackle this problem, there are several ways.

    Let’s start by changing the example slightly:

    public class Product
    {
        private Manufacturer Manufacturer { get; private set; }
    }
    
    public class Manufacturer
    {
        private List<Product> Products { get; set; }
    }
    

    So every Product has one Manufacturer, and each Manufacturer could have a list of products. The challenge with the model is that if the Product class and Manufacturer class maintain disconnected references to one another, updating one can invalidate the other.

    There are several ways to address this issue:

    1. Eliminate the circular reference. This solves the problem but makes the object model less expressive and harder to use.

    2. Change the code so that the Manufacturer reference in Product and Products list in Manufacturer are reflexive. In other words, changing one affects the other. This generally requires some code the setter and the collection to intercept changes and reflect them into one another.

    3. Manage one property in terms of the other. So, rather than storing a reference to a manufacturer within Product, you compute it by search through all Manufacturers until you find the one that owns you. Conversely, you could keep a reference to the Manufacturer in the Product class and build the list of Products dynamically. In this approach, you would generally make one side of the relationship read-only. This, by the way, is the standard relational database approach – entities refer to each other through a foreign key which is managed in one place.

    4. Externalize the relationship from both classes and manage it in a separate object (often called a data context in ORM). When Product wants to return its manufacturer it asks the DataContext. When the Manufacturer want to return a list of Products it does the same. Internally, there are many ways to implement a data context, a set of bi-directional dictionaries is not uncommon.

    Finally, I will mention, that you should consider using an ORM tool (like NHibernate or CSLA) that can help you manage graph consistency. This is generally not an easy problem to solve correctly – and it can easily become very complicated once you start exploring cases like many-to-many relationships, one-to-one relationships, and lazy loading of objects. You are better of using an existing library or product, rather than inventing a mechanism of your own.

    Here are some links that talk about bidirectional associations in NHibernate that you may find useful.

    Here’s a code example of managing the relationships directly yourself using method #2 – which is typically the simplest. Note that only one side of the relationship is editable (in this case, the Manufacturer) – external consumers cannot directly set the Manufacturer of a Product.

    public class Product
    {
        private Manufacturer m_manufacturer;
    
        internal Manufacturer Manufacturer
        {
            get { return m_manufacturer; }
            set { m_manufacturer = value; }
        }
    }
    
    public class Manufacturer
    {
        private List<Product> m_Products = new List<Product>();
    
        public IEnumerable<Product> Products { get { return m_Products.AsReadOnly(); } }
    
        public void AddProduct( Product p )
        {
            if( !m_Products.Contains( p ) )
            {
                m_Products.Add( p );
                p.Manufacturer = this;
            }
        }
    
        public void RemoveProduct( Product p )
        {
            m_Products.Remove( p );
            p.Manufacturer = null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty much what the question says. What's the difference between the two classes of
I feel like this is a pretty common problem but I wasn't really sure
This has to be a pretty common issue, and while we have answers, I'm
I have a pretty common case for nested routes, I feel like, that looks
Pretty common situation, I'd wager. You have a blog or news site and you
I have a pretty common layout issue that I have traditionally used a table
I have included reset.css which is pretty common for most websites that use, on
I have a question which I think should be pretty common but I can't
I guess, the problem is pretty common. Let's say, we have a source file
It seems pretty common to want to let your javascript know a particular dom

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.