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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:49:46+00:00 2026-06-03T15:49:46+00:00

I am writing an object that must always have certain values. Most notably, it

  • 0

I am writing an object that must always have certain values. Most notably, it must always have a value for Name property.

public class User
{
    public string Name { get; set; }

    public User(string name)
    {
        Name = name;
    }
}

Now, there are a couple of business rules that I need to implement in this class. One of which is that the Name property must be a unique name. So, I would think that the initializer for this object would look something this:

    public User(string name, IQueryable<User> allUsers)
    {
        var matches = allUsers.Where(q => q.Name == name).ToList();
        if(matches.Any())
        {
            // abort object initialization
        }
        Name = name;
    }

But I’m not sure how I would abort the object initialization. In fact, is that even possible?

Is there a way to abort an object initialization (ie: set object to null) or is there a better way of accomplishing this?

  • 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-06-03T15:49:49+00:00Added an answer on June 3, 2026 at 3:49 pm

    Aborting initialization of an object is done by throwing an exception in the constructor, and is recommended to reject invalid input.

    public class User
    {
        public User(String name) {
            if (String.IsNullOrWhiteSpace(name)) {
                if (name == null) {
                    throw new System.ArgumentNullException("Cannot be null.", "name");
                }
                else {
                    throw new System.ArgumentException("Cannot be empty.", "name");
                }
            }
        }
    }
    

    The business logic you wish to define in the constructor doesn’t fit there. Constructors should be lightweight, and instantiation only. Querying some data source is too expensive for a constructor. Because of this, you should use the factory pattern instead. With the factory pattern, a caller might expect there to be some heavy lifting involved with object creation.

    public class User
    {
        private User(String name) {
            if (String.IsNullOrWhiteSpace(name)) {
                if (name == null) {
                    throw new System.ArgumentNullException("Cannot be null.", "name");
                }
                else {
                    throw new System.ArgumentException("Cannot be empty.", "name");
                }
            }
        }
    
        public static User CreateUser(String name) {
            User user = new User(name); // Lightweight instantiation, basic validation
    
            var matches = allUsers.Where(q => q.Name == name).ToList();
    
            if(matches.Any())           
            {           
                throw new System.ArgumentException("User with the specified name already exists.", "name");         
            }     
    
            Name = name;
        }
    
        public String Name {
            get;
            private set; // Optionally public if needed
        }
    }
    

    You can see that the factory pattern fits better, and because it’s a method a caller might expect there to be some work going on by invoking it. Whereas with a constructor one would expect it to be lightweight.

    If you wanted to go the constructor route, then you would want to try some other method of enforcing your business rules such as when the actual insert into the data source is attempted.

    public class User
    {
        public User(String name) {
            if (String.IsNullOrWhiteSpace(name)) {
                if (name == null) {
                    throw new System.ArgumentNullException("Cannot be null.", "name");
                }
                else {
                    throw new System.ArgumentException("Cannot be empty.", "name");
                }
            }
        }
    }
    
    public class SomeDataSource {
        public void AddUser(User user) {
            // Do your business validation here, and either throw or possibly return a value
            // If business rules pass, then add the user
            Users.Add(user);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a .NET object that does a lot of reading/writing with the database.
I'm writing an app that monitors the user's location. I have a CLLocationManager object
I'm writing a COM object that provides access to a service that must be
In a simulation I'm writing I have a class that represents an Agent that
I'm writing an object in PHP that displays files on an FTP server. I
I am writing a deserialization method that converts xml to a Java object. I
I'm writing some code that will serialize a C# object to JSON, send it
I'm writing a simple jQuery plugin that will interface with a JSON object/array. I
A requirement for a program I am writing is that it must be able
I am writing a class in JSP to retrieve a bunch of config values

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.