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

While trying to learn Play Framework I ran across the following problem: How do
I am working on the domain model for a project. I have a class
I have a problem while trying to render a partial action on my layout.
Let's say I have a set of Django Models: class Article(models.Model): title = models.CharField(max_length=100,
I'm having a problem while trying to call a custom Model method from my
While trying to understand the MVVM pattern I came across this video linked from
I'm trying to convert an ActiveRecord model to JSON in Rails, and while the
While trying to use LINQ to SQL I encountered several problems. I have table
While trying out an experimental UINavigationController-based iPhone application, I ran into a problem when
I'm getting the following errors from MSBuild while trying to build a solution: C:\dev\MySln.sln

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.