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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:05:12+00:00 2026-05-13T09:05:12+00:00

I am trying to create a web-based tool for my company that, in essence,

  • 0

I am trying to create a web-based tool for my company that, in essence, uses geographic input to produce tabular results. Currently, three different business areas use my tool and receive three different kinds of output. Luckily, all of the outputs are based on the same idea of Master Table – Child Table, and they even share a common Master Table.

Unfortunately, in each case the related rows of the Child Table contain vastly different data. Because this is the only point of contention I extracted a FetchChildData method into a separate class called DetailFinder. As a result, my code looks like this:

DetailFinder DetailHandler;
if (ReportType == "Planning")
  DetailHandler = new PlanningFinder();
else if (ReportType == "Operations")
  DetailHandler = new OperationsFinder();
else if (ReportType == "Maintenance")
  DetailHandler = new MaintenanceFinder();
DataTable ChildTable = DetailHandler.FetchChildData(Master);

Where PlanningFinder, OperationsFinder, and MaintenanceFinder are all subclasses of DetailFinder.

I have just been asked to add support for another business area and would hate to continue this if block trend. What I would prefer is to have a parse method that would look like this:

DetailFinder DetailHandler = DetailFinder.Parse(ReportType);

However, I am at a loss as to how to have DetailFinder know what subclass handles each string, or even what subclasses exist without just shifting the if block to the Parse method. Is there a way for subclasses to register themselves with the abstract DetailFinder?

  • 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-13T09:05:12+00:00Added an answer on May 13, 2026 at 9:05 am

    You might want to use a map of types to creational methods:

    public class  DetailFinder
    {
        private static Dictionary<string,Func<DetailFinder>> Creators;
    
        static DetailFinder()
        {
             Creators = new Dictionary<string,Func<DetailFinder>>();
             Creators.Add( "Planning", CreatePlanningFinder );
             Creators.Add( "Operations", CreateOperationsFinder );
             ...
        }
    
        public static DetailFinder Create( string type )
        {
             return Creators[type].Invoke();
        }
    
        private static DetailFinder CreatePlanningFinder()
        {
            return new PlanningFinder();
        }
    
        private static DetailFinder CreateOperationsFinder()
        {
            return new OperationsFinder();
        }
    
        ...
    

    }

    Used as:

    DetailFinder detailHandler = DetailFinder.Create( ReportType );
    

    I’m not sure this is much better than your if statement, but it does make it trivially easy to both read and extend. Simply add a creational method and an entry in the Creators map.

    Another alternative would be to store a map of report types and finder types, then use Activator.CreateInstance on the type if you are always simply going to invoke the constructor. The factory method detail above would probably be more appropriate if there were more complexity in the creation of the object.

    public class DetailFinder
    {
          private static Dictionary<string,Type> Creators;
    
          static DetailFinder()
          {
               Creators = new Dictionary<string,Type>();
               Creators.Add( "Planning", typeof(PlanningFinder) );
               ...
          }
    
          public static DetailFinder Create( string type )
          {
               Type t = Creators[type];
               return Activator.CreateInstance(t) as DetailFinder;
          }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a web-based WYSIWYG editor that consists of a canvas and
We are trying to create a web-service that we plan to pass a variable
I'm trying to create a rails web app that does not use ActiveRecord framework
I am trying to create a small web app that will allow users to
I am trying to create an asp.net web form that allows a user to
I'm trying to create a web based welcome display for a building entrance. The
I'm trying to create a web page that will display an appropriate user control
I am trying to create a web application that can respond to post requests
I need to create a Web-based Dashboard tool for a LOB application. Essentially users
I am trying to create a RESTful web service based on WCF Web API

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.