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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:20:31+00:00 2026-05-30T14:20:31+00:00

I found that my design is wrong and asking you how solve my problem.

  • 0

I found that my design is wrong and asking you how solve my problem.

So my case:

I’m writing factory method, for classes, witch was derived from my special base class. So I wrote

public T MyFactory<T>() where T:MyBaseClass

But my the main work of my factory method is getting some special parameters and passing it to constructor of new object. MyBaseClass has this constructor:

public MyBaseClass(MySpecParam param){...}

But there is no guarantee that type T, derived from MyBaseClass, has such a constructor.

The only solution I see, is to add new() constraint and virtual Init method to MyBaseClass, so that the factory can safely create a new object of type T, and then init it with MySpecParam object.

But, MyBaseClass has such design, so it’s completely unusable if it is not inited with MySpecParam. And user can create MyBaseClass with parameterless constructor and get completely invalid, not initialized, object. I think it’s not good.

There is no way to add new(MySpecParam) constraint.

How must I design my objects, constructors and factory method?

  • 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-30T14:20:33+00:00Added an answer on May 30, 2026 at 2:20 pm

    If each class has only one public constructor, you can find that by reflection, determine what parameters it takes, and supply the appropriate values when you call the constructor (still through reflection).

    Otherwise, I presume you have a finite set of constructor signatures. In that case, you’ll need some way of determining which constructor to call; the decision may be based in part on the identity of the type argument T.

    EDIT

    If you’re not willing to use reflection for the reasons outlined in your comment, then the answer is more or less “no, you can’t do that.” Michael Yoon suggests an IoC framework, which of course uses reflection and is subject to run-time errors as well. In my experience (with Castle Windsor), performance has never been a problem, and run-time errors as the result of misconfiguration are caught almost immediately in the development cycle.

    Another thought; this may not be helpful, but it might be worth considering. You could use a Func<T> to create the instance, or even have overloads of the factory method for different types of Func<T, TOut>, Func<T1, T2, TOut>, etc, where you call

    var obj = FactoryMethod<SomeType>(() => new SomeType(23));
    

    Alternatively, consider the abstract factory pattern.

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

Sidebar

Related Questions

I found that you can't read from some sites using Python's urllib2(or urllib). An
While working with SilverLight using Visual Studio 10, I found that in design mode
I'm trying to implement a design that I found on another site. It's designed
Suppose I am trying to design an algorithm to solve a problem. How should
I just found that our current database design is a little inefficient according to
I found that it is not allowed to call shared_from_this in the destructor from
I found that Wix v3 uses a tool (heat.exe) to harvest information into WiX
I found that SO put the search input inside a form tag, and i
I found that using Smarty with PHP, sometimes extra time will need to be
I found that I have to perform a swap in python and I write

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.