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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:00:39+00:00 2026-05-16T12:00:39+00:00

If we want to refactor an enum (contained in the domain layer) to a

  • 0

If we want to refactor an enum (contained in the domain layer) to a polymorphic class, using “simple” abstract methods could be a bad idea, if all the switch and if statements we want to refactor are inside the other layers (like the business or the presentation layer), because we could end up to reference these layers inside the domain layer:

public abstract class MyRefactoredEnum
{
    public abstract void DoSomething(IBusinnessObject aBizObject); //dependency to the biz. layer

    public abstract MvcHtmlString GetImgTag(); //dependency to presentation layer
}

(in the example above, we can have a “cross reference” problem too)

I’ve found that the visitor pattern ( http://en.wikipedia.org/wiki/Visitor_pattern ) it’s a valid solution to this problem: in the domain layer we define only the MyRefactoredEnum.IVisitor interface, and all the other layers can implement their own visitors.

The only problem: when we modify the MyRefactoredEnum.IVisitor interface (for example, because we’ve added another MyRefactoredEnum’s subclass) we have to modify and recompile all projects and solutions that reference the domain model. We can solve the problem using reflection ( http://surguy.net/articles/visitor-with-reflection.xml ), but it can be slow…

Is there a better pattern to refactor an enum?

PS: sorry for my awful English 🙂

  • 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-16T12:00:40+00:00Added an answer on May 16, 2026 at 12:00 pm

    You could provide a default implementation for visitors that has a fall-back method:

    abstract class Cheese
    {
        public abstract void Visit(ICheeseVisitor visitor);
    }
    
    class Wensleydale : Cheese { ... }
    
    class Gouda : Cheese { ... }
    
    interface ICheeseVisitor
    {
        void Visit(Wensleydale cheese);
        void Visit(Gouda cheese);
    }
    
    abstract class CheeseVisitor : ICheeseVisitor
    {
        public virtual void Visit(Wensleydale cheese) { Default(cheese); }
        public virtual void Visit(Gouda cheese) { Default(cheese); }
        public virtual void Default(Cheese cheese) { }
    }
    

    When you add new types, libraries built against an older version will use the fall-back method, while newer libraries can override the new overloads:

    class Brie
    {
        public override void Visit(ICheeseVisitor visitor)
        {
            visitor.Visit(this);
        }
    }
    
    interface ICheeseVisitor
    {
        ...
        void Visit(Brie cheese);
    }
    
    abstract class CheeseVisitor : ICheeseVisitor
    {
        ...
        public virtual void Visit(Brie cheese) { Default(cheese); }
        ...
    }
    

    Example:

    class CheeseImgVisitor : CheeseVisitor 
    {
        private string src;
    
        public string Src
        {
            get { return this.src; }
        }
    
        public override void Visit(Wensleydale cheese)
        {
            this.src = "wensleydale.png";
        }
    
        public override void Visit(Gouda cheese)
        {
            this.src = "gouda.png";
        }
    
        public override void Default(Cheese cheese)
        {
            this.src = "generic_cheese.png";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that I want to refactor. I have lots of methods
I want to refactor some code using the Windsor IOC/DI framework, but my issue
I've a old C/C++ class that i want to refactor and access from .net
I want to refactor this mumbo jumbo of a method to make it more
I want to create a Java application bundle for Mac without using Mac. According
Suppose I have the two following Linq-To-SQL queries I want to refactor: var someValue1
I'm doing some work with stats, and want to refactor the following method :
Data binding in ActionScript is really cool. But what if I want to refactor
I want to refactor my model so I can properly write a unit test
I have a controller that I want to refactor. This controller basically renders a

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.