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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:36:53+00:00 2026-05-22T23:36:53+00:00

I have a simple Factory implementation in my code. I’m aiming to have the

  • 0

I have a simple Factory implementation in my code. I’m aiming to have the constructed objects retain a reference to the factory that created them, so I have them wired up similar to this:

public class Factory {

    public T CreateObject<T>() where T : Foo, new() 
    {
        return new T() {
            parent = this
        };
    }
}

public class Foo {

    internal Factory parent;
    internal Foo() { }
}

This works as it reads, but I’ve been thinking that I probably want that parent variable so that it cannot be changed once it’s set by the factory. However, if I declare it like internal readonly Factory parent;, then the factory can’t set it’s value anymore on construction.

I’d normally remedy this by providing a parametered constructor, but that kills the generic implementation, because AFAIK where T : new() implies a parameter-less constructor.

I may just be missing a few cogs with my C# chops, but what would be the best way to go about implementing something like this? (Or would it be better to just ditch the readonly and trust that the code won’t be modifying parent in an unsafe manner — NullReferenceExceptions come to mind — ?)

  • 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-22T23:36:53+00:00Added an answer on May 22, 2026 at 11:36 pm

    I do not think you can obtain exactly what you want, but you could make a property that is only assignable once and let the factory overwrite it. But I guess the user can always force an override with new CreatingFactory, but I think this makes it difficult and at least clearly shows your intention.

    You could do something like below.

        class Foo
        {
            private Factory factory;
    
            public Factory CreatingFactory
            {
                get { return factory; }
                set
                {
                    if (factory != null)
                    {
                        throw new InvalidOperationException("the factory can only be set once");
                    }
                    factory = value;
                }
            }
        }
    
        class Factory
        {
            public T Create<T>()
                where T : Foo, new()
            {
                T t = new T()
                {
                    CreatingFactory = this
                };
                return t;
            }
        }
    

    After having created it i searched and found an answer that is probably better than mine: Is there a way of setting a property once only in C#

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

Sidebar

Related Questions

I have created simple factory: public interface ICommandFactory { ICommand CreateCommand(string componentName); } Implemented
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have simple win service, that executes few tasks periodically. How should I pass
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have simple SSIS package in which On Error event handler I have created
I have this abstract factory pattern that I use in as3 that I am
I have a simple Spring MVC application that looks up some user details from
I have a simple class ExcelStringBuilder. Its used to build a string that can
Okay - so I know it's simple to build a factory method that provides
i'm just learning java, and i meet some problems. Here we have simple factory

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.