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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:59:21+00:00 2026-05-13T19:59:21+00:00

I wanted to avoid the switch statement. I have over 30 document types. There

  • 0

I wanted to avoid the switch statement. I have over 30 document types. There is also a possibility I will need to add more document types moving forward. I would rather pass IDocument and have the type specified in the implementation of IDocument. Something else I forgot to mention was ProgressNoteViewModel, LabViewModel … all inherit from WorkspaceViewModel and all of the concrete implementation constructors take a type IPatient as parameter. I am also using Castle as my IoC container

I would want to refactor the code to something like

viewModel = new TreeViewModel(repository.GetPatientDocumentListing(IDocumentType);
this.DocTreeViewModel = viewModel;
//How would I then be able to instantiate the right ViewModel
//based on IDocumentType and also pass a object into the
//constructor that is not know at compile time

I have the following code:

switch (docType)
{
    case "ProgressNotes":
        viewModel = new TreeViewModel(repository.GetPatientProgressNotes());
        this.DocTreeViewModel = viewModel;
        ProgressNoteViewModel workspace = ProgressNoteViewModel.NewProgressNoteViewModel(_patient);
        break;
    case "Labs":
        viewModel = new TreeViewModel(repository.GetPatientLabs());
        this.DocTreeViewModel = viewModel;
        LabViewModel workspace = LabViewModel.NewLabViewModel(_patient);
        break;
}
this.Workspaces.Add(workspace);
this.SetActiveWorkspace(workspace);
  • 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-13T19:59:21+00:00Added an answer on May 13, 2026 at 7:59 pm

    Entirely untested:

    public class ViewModelBuilderFactory
    {
        public IViewModelBuilder GetViewModelBuilder (string docType, IRepository repository)
        {
            switch (docType)
            {
                case "ProgressNotes":
                    return new ProgressNotesViewModelBuilder(repository);
                case "Labs":
                    return new LabsViewModelBuilder(repository);
                default:
                    throw new ArgumentException(
                        string.Format("docType \"{0}\" Invalid", docType);
            }
        }
    }
    
    public interface IViewModelBuilder
    {
        TreeViewModel GetDocTreeViewModel();
        WorkSpace GetWorkSpace(Patient patient);
    }
    
    public class LabsViewModelBuilder : IViewModelBuilder
    {
        private IRepository _repository;
        public LabsViewModelBuilder(IRepository repository)
        {
            _repository = repository;
        }
    
        public TreeViewModel GetDocTreeViewModel()
        {
            return new TreeViewModel(_repository.GetPatientLabs());
        }
    
        public Workspace GetWorkspace(Patient patient)
        {
            return LabViewModel.NewLabViewModel(patient);
        }
    }
    
    public class ProgressNotesViewModelBuilder : IViewModelBuilder
    {
        private IRepository _repository;
        public ProgressNotesViewModelBuilder(IRepository repository)
        {
            _repository = repository;
        }
    
        public TreeViewModel GetDocTreeViewModel()
        {
            return new TreeViewModel(_repository.GetPatientProgressNotes());
        }
    
        public Workspace GetWorkspace(Patient patient)
        {
            return ProgressNoteViewModel.NewProgressNoteViewModel(patient);
        }
    }
    

    Now your calling code is:

    ViewModelBuilderFactory factory = new ViewModelBuilderFactory();
    IViewModelBuilder modelBuilder = factory.GetViewModelBuilder(docType, repository);
    this.DocTreeViewModel = modelBuilder.GetDocTreeViewModel();
    Workspace workspace = modelBuilder.GetWorkspace(patient);
    this.Workspaces.Add(workspace);
    this.SetActiveWorkspace(workspace);
    

    [4 edits since first post; keep seeing mistakes]

    [Further Edit noting that you’re using Castle IOC]

    In your Castle xml configuration, you could add (and I’m working on only a vague knowledge of Castle here)

    <component id="ProgressNotesViewModelBuilder"
               type="MyNamespace.ProgressNotesViewModelBuilder, MyAssembly">
        <parameters>
            <!-- reference to repository here -->
        </parameters>
    </component>
    <component id="LabsViewModelBuilder"
               type="MyNamespace.LabsViewModelBuilder, MyAssembly">
        <parameters>
            <!-- reference to repository here -->
        </parameters>
    </component>
    

    Then you don’t need the ViewModelBuilderFactory, you can just replace

    IViewModelBuilder modelBuilder = factory.GetViewModelBuilder(docType, repository);
    

    with

    IViewModelBuilder modelBuilder = (IViewModelBuilder)
        container.Resolve(docType + "ViewModelBuilder");
    

    Now you don’t need your switch statement at all.

    However, it’s worth noting that switches aren’t evil, they just smell bad and like all bad smells should be isolated from everything that smells good; that’s what the Factory pattern is intended to achieve.

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

Sidebar

Ask A Question

Stats

  • Questions 374k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The error message is pretty clear: Your client_encoding setting is… May 14, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer It looks like there is a random element here, since… May 14, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer Postgresql prohibits that query because it is ambiguous: there's actually… May 14, 2026 at 8:04 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.