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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:17:17+00:00 2026-05-18T04:17:17+00:00

I wrote a non-static Generic class instantiator for my Abstract Factory design, and use

  • 0

I wrote a non-static Generic class instantiator for my Abstract Factory design, and use Singleton approach to make sure that only 1 instance of the instantiator will be initialized for every client request.

public sealed class FactoryInstantiator<T> where T: class
{
    private static readonly FactoryInstantiator<T> _instance = new Instantiator<T>();
    public static FactoryInstantiator<T> Instance
    {
        get
        {
            _client = HttpContext.Current.Session["ClientCode"].ToString();
            return _instance;
        }
    }

    private static string _client;
    private string _className;
    private string _fullyQualifiedClassName;
    private string _assemblyName;

    private FactoryInstantiator() { }

    public T CreateInstance()
    {
        string fullClassName = typeof(T).ToString();
        string[] splitClassName = fullClassName.Split('.');
        _className = splitClassName[2];
        _assemblyName = splitClassName[0] + "." + _client + "." + splitClassName[1];
        _fullyQualifiedClassName = _assemblyName + "." + _className;

        return (T)Activator.CreateInstance(Type.GetType(_fullyQualifiedClassName + "," + _assemblyName));
    }
}

I abstracted the the whole namespace for each Client

namespace InventorySuite.Factory.BusinessLogic
{
    // abstract factory
    public abstract class InvoiceFactory
    {
        public abstract void Set() { }
    }
}

namespace InventorySuite.Client1.BusinessLogic
{
    // concrete invoice class for Client1
    public class Invoice : InvoiceFactory
    {
        public override void Set() { }
    }
}

namespace InventorySuite.Client2.BusinessLogic
{
    // concrete invoice class for Client2
    public class Invoice : InvoiceFactory
    {
        public override void Set() { }
    }
}


protected void Page_Load(object sender, EventArgs e)
{       
    InvoiceFactory clientInvoice;

    Session.Add("ClientCode", "Client1");
    clientInvoice = FactoryInstantiator<InvoiceFactory>.Instance.CreateInstance();
    clientInvoice.Set();

    Session["ClientCode"] = "Client2";
    clientInvoice = FactoryInstantiator<InvoiceFactory>.Instance.CreateInstance();
    clientInvoice.Set();

}

It works well and already tested it, but my question is about its efficiency/performance hit, since I use reflection here, and for the Singleton approach if it has multi-threading issues (afaik, the singleton instance will be shared in all clients). I will also appreciate any other approach on this. thanks

  • 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-18T04:17:17+00:00Added an answer on May 18, 2026 at 4:17 am

    You will not have any multi-threading issue since you’re creating a new instance every time.

    About the performance. You can measure the time creating 100 instances:

    long ini = Environment.TickCount;
    for (int i = 0; i < 100; i++)
    {
        Session["ClientCode"] = "Client2";
        clientInvoice = FactoryInstantiator<InvoiceFactory>.Instance.CreateInstance();
        clientInvoice.Set();
    }
    long timeCreate100Instances = Environment.TickCount - ini;
    

    Using reflection, the performance hit resides in loading the assembly. I think that in your case, the class you’re loading is in the same dll, you you also will not experiment any performance issue.

    In other case, you can cache the Assembly obejcts in a Hastable/Dictionary in your CreateInstance() method.

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

Sidebar

Related Questions

I want to use a generic class inside a nested static interface. My objective
I just wrote some new utility methods within a non-page class for an existing
I have created a Static Class and used that in Reflection. But when i
I wrote a generic extension method that takes the pain out of parsing a
I wrote an abstract class to contain all rules of the application because I
I need to write a non-recursive version of the function sum-squares and Use a
I'm trying to write a method that removes all non alphabetic characters from a
I wrote a JSP code that needs to run some mysql INSERT queries that
I'm writing a library in C# that I will later use for an application,
Why is it considered bad practice to lock non-static fields? And, if I am

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.