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

  • Home
  • SEARCH
  • 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 818447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:09:15+00:00 2026-05-15T02:09:15+00:00

I have factory that looks something like the following snippet. Foo is a wrapper

  • 0

I have factory that looks something like the following snippet. Foo is a wrapper class for Bar and in most cases (but not all), there is a 1:1 mapping. As a rule, Bar cannot know anything about Foo, yet Foo takes an instance of Bar. Is there a better/cleaner approach to doing this?

public Foo Make( Bar obj )
{
    if( obj is Bar1 )
        return new Foo1( obj as Bar1 );
    if( obj is Bar2 )
        return new Foo2( obj as Bar2 );
    if( obj is Bar3 )
        return new Foo3( obj as Bar3 );
    if( obj is Bar4 )
        return new Foo3( obj as Bar4 ); // same wrapper as Bar3
    throw new ArgumentException();
}

At first glance, this question might look like a duplicate (maybe it is), but I haven’t seen one exactly like it. Here is one that is close, but not quite:

Factory based on Typeof or is a

  • 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-15T02:09:15+00:00Added an answer on May 15, 2026 at 2:09 am

    I’m not sure what you actually want to achieve. I would probably try to make it more generic.

    You could use attributes on Foo, which Bar it supports, then you create a list in a initializing phase. We are doing quite a lot of stuff like this, it make adding and connecting new classes very easy.

    private Dictionary<Type, Type> fooOfBar = new Dictionary<Type, Type>();
    public initialize()
    {
      // you could scan all types in the assembly of a certain base class
      // (fooType) and read the attribute
    
      fooOfBar.Add(attribute.BarType, fooType);
    }
    
    public Foo Make( Bar obj )
    {
      return (Foo)Activator.CreateInstance(fooOfBar(obj.GetType()), obj);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data in a table that looks like the following sample data: varchar(20)
With generics on languages like C# or Java, you can have a factory that
I have a factory method that generates django form classes like so: def get_indicator_form(indicator,
I have a script with a factory method that I would like to return
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have the following factory class: public class MessagePresenterCreator implements IPresenterFactory{ @Override public MessagePresenter
I have an abstract class where source code looks like this: /* * @assert
I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
I have code that looks like follows: public interface BaseDAO{ // marker interface }
I have a data set which looks something like data<-c(0,1,2,3,4,2,3,1,4,3,2,4,0,1,2,0,2,1,2,0,4) frame<-as.data.frame(data) I now want

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.