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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:22:42+00:00 2026-05-23T10:22:42+00:00

I have designed a class containing some information about a given object which will

  • 0

I have designed a class containing some information about a given object which will be registered in an SQL Server database. I would like to make this object (deeply) immutable, but I also must assure that it gets registered only once. If this object implements the following pattern, can it still be considered immutable?

public class NewClass
{
    private bool registered;

    public string SomeProperty { get; private set; }

    public NewClass Register()
    {
        if (registered)
        {
            throw new NotImplementedException(/*arguments*/);
        }

        /* Register on DB here... */
        registered = true;
        return new NewClass(somePropertyFromDB);
    }

    public NewClass(string someProperty)
    {
        registered = false;
        SomePropery = someproperty;
    }
}

I would say that except for the boolean field registered the object is immutable, but this fields leaves me some doubts because it will actually change on the first time the Register method is executed… Can anyone please tell me how can I solve this problem and still make the object be immutable?

  • 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-23T10:22:42+00:00Added an answer on May 23, 2026 at 10:22 am

    No, it’s definitely not immutable. The registered value changes over the lifetime of the object.

    To make it immutable, you mustn’t allow the registered field to change over the lifetime of the object – instead, make Register return a new object which has a true value for registered

    I’d also remove the private setter for SomeProperty, just having a getter and a readonly variable.

    So something like this:

    public class NewClass
    {
        private readonly bool registered;
        private readonly string someProperty;
    
        public bool Registered { get { return registered; } }
        public string SomeProperty { get { return someProperty; } }
    
        public NewClass Register()
        {
            // Note the change of exception here
            if (registered)
            {
                throw new InvalidOperationException("Already registered");
            }
    
            return new NewClass(somePropertyFromDB, true);
        }
    
        // You may want to have a public constructor with just someProperty
        // which calls this one, which you could make private
        public NewClass(string someProperty, bool registered)
        {
            this.registered = registered.;
            this.someProperty = someproperty;
        }
    }
    

    Some notes:

    • Another option would be to have two separate classes, RegisteredFoo and UnregisteredFoo; that might make it easier to understand code using this class
    • There’s nothing to stop someone calling Register twice, so making this immutable doesn’t really help in terms of idempotency. As there’s a natural side-effect (talking to the database) it’s hard to make this truly functional.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have designed database tables (normalised, on an MS SQL server) and created a
I have designed a class which is basically nothing but an object which stores
I have some C# class libraries, that were designed without taking into account things
I have designed an ASP.NET page which create graphs. I have written a class
I have designed some classes using Visual Studio class diagramming. Now I would like
I am working on a class library using C#. I have designed 3 main
I have a class (class A) that is designed to be inherited by other
I have designed this gui in netBeans which has a canvas , a couple
I have designed a class with sqlClient.SqlCommand wrappers to implement such functionality as automatic
I have designed a Class for Parent Child relationship class Category { public string

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.