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 7074857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:06:30+00:00 2026-05-28T06:06:30+00:00

I have three classes called Broker , Instrument and BrokerInstrument . using Iesi.Collections.Generic; public

  • 0

I have three classes called Broker, Instrument and BrokerInstrument.

using Iesi.Collections.Generic;

public class Broker : ActiveDefaultEntity
{
    public virtual string Name { get; set; }
    public virtual ISet<BrokerInstrument> BrokerInstruments { get; set; }
}

public class Instrument : Entity
{
    public virtual string Name { get;  set; }
    public virtual string Symbol {get;  set;}
    public virtual ISet<BrokerInstrument> BrokerInstruments { get; set; }
    public virtual bool IsActive { get; set; }        
}

public class BrokerInstrument : Entity
{
    public virtual Broker Broker { get; set; }
    public virtual Instrument Instrument { get; set; }
    public virtual decimal MinIncrement { get; set; }
}

If I create three new objects (one of each type) using those classes how to I associate them? For example:

Instrument instrument = new Instrument { 
    Name = "Test Instrument", 
    Symbol = "Test", 
    IsActive = true 
};
Broker broker = new Broker { 
    Name = "My Test Broker", 
    IsActive = true, 
    IsDefault = false 
};
BrokerInstrument brokerInstrument = new BrokerInstrument { 
    Broker = broker, 
    Instrument = instrument, 
    MinIncrement = 0.01M 
};

How does the instrument “know” that a new brokerInstrument is now associated with it? If I now run if (instrument.Brokerinstruments == null) I get true. Do I have to associate the objects in both the BrokerInstrument declaration and then go back and add it to the instrument.BrokerInstruments ISet?

If I try and do: instrument.BrokerInstruments.Add(instrument) I get an error because its null. Confused. What am I missing? What is the best way to model relationships like this? these objects will get persisted to a database using NHibernate.

  • 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-28T06:06:30+00:00Added an answer on May 28, 2026 at 6:06 am

    You get an exception because you are not initializing the BrokerInstruments property of the Instrument class (meaning the value of that property is null). To fix that, you need a constructor on Instrument:

    public Instrument() {
        BrokerInstruments = new HashSet<BrokerInstrument>();
    }
    

    Now if you want notification of an Instrument being added, that’s a different problem. The easiest and safest way is to make the BrokerInstruments property getter return an IEnumerable, remove the setter, and add a AddBrokerInstrument method:

    // With this, you don't need the constructor above.
    private ISet<BrokerInstrument> _brokerInstruments = new HashSet<BrokerInstrument>();
    
    public virtual IEnumerable<BrokerInstrument> BrokerInstruments { 
        get { return _brokerInstruments; } 
    
        // This setter should allow NHibernate to set the property while still hiding it from external callers
        protected set { _brokerInstruments = new HashSet<BrokerInstrument>(value); } 
    }
    
    public void AddBrokerInstrument(BrokerInstrument brokerInstrument) { 
         // Any other logic that needs to happen before an instrument is added
         _brokerInstruments.Add(brokerInstrument); 
         // Any other logic that needs to happen after an instrument is added
    }
    

    I use an IEnumerable above because you want to indicate to the users of this function that they are not allowed to add instruments directly to the set- they need to call your method instead.

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

Sidebar

Related Questions

I have three classes: Generic, CFG, and Evaluator. Here's Generic: class Generic: public virtual
I have three different base classes: class BaseA { public: virtual int foo() =
I have an abstract base class called Curve . There are three classes that
I have been using three classes. Two classes extends the third class db. But
I have three classes: public class A { public A(){ System.out.println(in A); } }
I have three classes that all have a static function called 'create'. I would
I have three classes; Classes A and B both reference class C . How
If I have three classes class A class B extends A class C extends
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
Let's say I have three classes set up as follows: abstract Class A {

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.