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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:46:36+00:00 2026-06-13T18:46:36+00:00

I am in the process of refactoring a rather large portion of spaghetti code.

  • 0

I am in the process of refactoring a rather large portion of spaghetti code. In a nutshell it is a big “God-like” class that branches into two different processes depending in some condition. Both processes are lengthy and have lots of duplicated code.

So my first effort has been to extract those two processes into their own classes and putting the common code in a parent they both inherit from.

It looks something like this:

public class ExportProcess
{
   public ExportClass(IExportDataProvider dataProvider, IExporterFactory exporterFactory)
   {
       _dataProvider = dataProvider;
       _exporterFactory = exporterFactory;
   }

   public void DoExport(SomeDataStructure someDataStructure)
   {
      _dataProvider.Load(someDataStructure.Id);

      var exporter = _exporterFactory.Create(_dataProvider, someDataStructure);

      exporter.Export();
   }
}

I am an avid reader of Mark Seemann’s blog and in this entry he explains that this code has a temporal coupling smell since it is necessary to call the Load method on the data provider before it is in a usable state.

Based on that, and since the object is being injected to the ones returned by the factory anyway, I am thinking of changing the factory to do this:

public IExporter Create(IExportDataProvider dataProvider, SomeDataStructure someDataStructure)
{
   dataProvider.Load(someDataStructure.Id);

   if(dataProvider.IsNewExport)
   {
      return new NewExportExporter(dataProvider, someDataStructure);
   }
   return new UpdateExportExporter(dataProvider, someDataStructure);
}

Because of the name “DataProvider” you probably guessed that the Load method is actually doing a database access.

Something tells me an object doing a database access inside the create method of an abstract factory is not a good design.

Are there any guidelines, best practices or something that say this is effectively a bad idea?

Thanks for your help.

  • 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-06-13T18:46:37+00:00Added an answer on June 13, 2026 at 6:46 pm

    Typically, a factory is used to resolve concrete types of a requested interface or abstract type, so you can decouple consumers from implementation. So usually a factory is just going to discover or specify the concrete type, help resolve dependencies, and instantiate the concrete type and return it. However, there’s no hard or fast rule as to what it can or can’t do, but it is sensible to give it enough access to only to resources that it needs to resolve and instantiate concrete types.

    Another good use of a factory is to hide from consumers types dependencies that are not relevant to the consumer. For example, it seems IExportDataProvider is only relevant internally, and can be abstracted away from consumers (such as ExportProcess).

    One code smell in your example, however, is how IExportDataProvider is used. The way it currently seems to work, you get an instance of it once, but it’s possible to change its state in subsequent usages (by calling Load). This can lead to issues with concurrency and corrupted state. Since I don’t know what that type does or how it’s actually used by your IExporter, it’s hard to make a recommendation. In my example below, I make an adjustment so that we can assume that the provider is stateless, and instead Load returns some sort of state object that the factory can use to resolve the concrete type of exporter, and then provide data to it. You can adjust that as you see fit. On the other hand, if the provider has to be stateful, you’ll want to create an IExportDataProviderFactory, use it in your exporter factory, and create a new instance of the provider from the factory for each call to exporter factory’s Create.

    public interface IExporterFactory
    {
        IExporter Create(SomeDataStructure someData);
    }
    
    public class MyConcreteExporterFactory : IExporterFactory
    {
         public MyConcreteExporterFactory(IExportDataProvider provider) 
         {
              if (provider == null) throw new ArgumentNullException();
    
              Provider = provider;
         }
    
         public IExportDataProvider Provider { get; private set; }      
    
         public IExporter Create(SomeDataStructure someData)
         {
             var providerData = Provider.Load(someData.Id);
    
             // do whatever. for example...
             return providerData.IsNewExport ? new NewExportExporter(providerData, someData) : new UpdateExportExporter(providerData, someData);
         }
    }
    

    And then consume:

    public class ExportProcess
    {
        public ExportProcess(IExporterFactory exporterFactory)
        {
            if (exporterFactory == null) throw new ArgumentNullException();
    
            _exporterFactory = factory;
        }
    
        private IExporterFactory _exporterFactory;
    
        public void DoExport(SomeDataStructure someData)
        {
            var exporter = _exporterFactory.Create(someData);
            // etc.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are in the process of refactoring some code. There is a feature that
I'm in the process of refactoring a very large amount of code, mostly C++,
In the process of refactoring some code, I encountered several build errors such as
I'm in the process of refactoring a Flex application into a library project, and
I'm in the process of refactoring some logic built into a Rails application into
Greetings. I am struggling to reduce a code segment that looks rather lengthy, leaving
Hey, I am in the process of refactoring a program that requires some amount
I'm in the process of refactoring some code which includes moving folders around, and
I have made a major refactoring of some code, and in the process I
I am in the process of refactoring our BI layers to make our code

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.