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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:20:02+00:00 2026-05-17T21:20:02+00:00

While trying to model my domain, I came across the following problem. Let’s image

  • 0

While trying to model my domain, I came across the following problem. Let’s image we have a Thing:

class Thing
{
    public int X { get; set; }
}

Things have a property X. Then, there are Packs, which aggregate Things. But the domain requires that there is some restriction on the Things the Packs can hold. Let it be for example that the cumulative value of Xes can’t be higher then some specific limit:

class Pack
{
    private readonly List<Thing> myThings = new List<Thing>();
    private const int MaxValue = 5;

    public void Add(Thing thing)
    {
        if (myThings.Sum(t => t.X) + thing.X > MaxValue)
            throw new Exception("this thing doesn't fit here");
        myThings.Add(thing);
    }

    public int Count
    {
        get { return myThings.Count; }
    }

    public Thing this[int index]
    {
        get { return myThings[index]; }
    }
}

So I check before adding a Thing to a Pack for the condition, but it’s still so easy to get into troubles:

var pack = new Pack();
pack.Add(new Thing { X = 2 });
pack.Add(new Thing { X = 1 });

var thingOne = new Thing { X = 1 };
var thingTwo = new Thing { X = 3 };

//pack.Add(thingTwo); // exception
pack.Add(thingOne);   // OK

thingOne.X = 5;       // trouble
pack[0].X = 10;       // more trouble

In C++ the solution would be to make a copy upon insertion and return const reference in the indexer. How to design around this problem in C# (and probably Java)? I just can’t seem to think of a good solution:

  1. make Thing immutable – but what if it needs to be mutable?
  2. watch the Things in Pack with event/observer – but that means that Pack imposes the design of Thing; what if Things have more properties? Then I’ll end up with just one event due to the need for Pack to watch for changes – that seems awkward to me.

Any ideas or preferred solutions?

EDIT:

Coming back to this question… I’ve accepted the reply by Itay. He’s right.
The original issue was that from one context you would want a Thing object to be immutable, and from a different context, you would want it to be mutable. And that calls for a seperate interface… maybe. I said “maybe”, because most of the time, Pack would be an Aggregate of Things (in the DDD sense) and therefore be the owner of the objects – which means it should not give you the ability to change the owned object (either return a copy or return an immutable interface).

It’s nice that in C++ this particular thing can be handled so easily by the const modifier. Seems like a lot less coding if you want to keep things in a consistent state.

  • 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-17T21:20:03+00:00Added an answer on May 17, 2026 at 9:20 pm

    Make Thing immutable.

    class Thing
    {
        public Thing (int x)
        {
           X = x;
        }
        public int X { get; private set; }
    }
    

    In addition, instead of if (myThings.Sum(t => t.X) + thing.X > MaxValue) I think it is better to hold a sum field in pack so you don’t have to recalculate the sum each time.

    EDIT
    Sorry – I missed the fact that you stated that you need it mutable.
    But… How would your c++ solution work? I don’t know much c++ but doesn’t c++ constant reference will prevent the change on instances that on the Pack?

    EDIT2
    Use an interface

    public interface IThing
    {
      int X { get; }
    }
    
    public class Thing : IThing
    {
      int X { get; set; }
    }
    
    class Pack
    {
        private readonly List<IThing> myThings = new List<IThing>();
        private const int MaxValue = 5;
    
        public void Add(IThing thing)
        {
            if (myThings.Sum(t => t.X) + thing.X > MaxValue)
                throw new Exception("this thing doesn't fit here");
            myThings.Add(new InnerThing(thing));
        }
    
        public int Count
        {
            get { return myThings.Count; }
        }
    
        public IThing this[int index]
        {
            get { return myThings[index]; }
        }
    
        private class InnerThing : IThing
        {
          public InnerThing(IThing thing)
          {
            X = thing.X;
          }
          int X { get; private set; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've ran into a problem while trying to test following IRepository based on NHibernate:
While trying to use LINQ to SQL I encountered several problems. I have table
I have been trying for quite a while to work out how to accomplish
While trying to generate classes from a xsd, i got this error: java.lang.IllegalArgumentException: Illegal
While trying to answer a question in the vicinity ' Unit Testing WPF Bindings
While trying to connect to a database that's set to read only, the connection
While trying to integrate Yahoo Media Player into my own website, I want to
I'm getting an Exception while trying to insert a row in oracle table. I'm
I got an error today while trying to do some formatting to existing code.
I'm trying to test the DB2 adapter for BizTalk 2006 (not R2). While trying

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.