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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:07:36+00:00 2026-06-15T06:07:36+00:00

I have this class, which creates a document and saves it: public class DocCreator

  • 0

I have this class, which creates a document and saves it:

public class DocCreator
{
  private IDocumentStore _documentStore;

  public DocCreator(IDocumentStore documentStore)
  {
    _documentStore = documentStore;
  }

  public void CreateAndSave()
  {
    var doc = new Document();
    doc.Title = "this is a title";
    doc.Content = whateverStream;
    doc.Hash = CalculateHash(doc.Content);
    //[do more things to create a doc]

    _documentStore.PersistToDisk(doc);
  }
}

I think it’s decent, as the code to save things is hidden in DocumentStore. But we can take it one step further, and remove the call _documentStore.PersistToDisk(doc); to another class, like this:

public class DocCreatorWorkflow
{
  private IDocumentStore _documentStore;

  public DocCreatorWorkflow(IDocumentStore documentStore)
  {
    _documentStore = documentStore;
  }

  public void CreateAndSave()
  {
    var docCreator = new DocCreator();
    var doc = docCreator.Create();

    _documentStore.PersistToDisk(doc);
  }
}

In the example above I’ve created another class, which calls two lower classes, and so becomes responsible for the ‘workflow’. It might be cleaner, but it also complicates things more. Doesn’t it?

Or should I always go for the second option?

  • 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-15T06:07:37+00:00Added an answer on June 15, 2026 at 6:07 am

    I would go with Option 2. You would need to modify the DocCreatorClass, though, since it is no longer responsible for saving it to disk:

    public static class DocCreatorClass
    {
        public static Document Create()
        {
            Document doc = new Document();
            // Property assignment code here.
    
            return doc;
        }
    }
    

    It would be static so that you would not need to instantiate a DocCreatorClass. I would also create separate functions for Create and Save in the DocCreatorWorkflow class:

    public class DocCreatorWorkflow
    {
        public IDocumentStore _documentStore;
    
        public DocCreateWorkflow(IDocumentStore documentStore)
        {
        }
    
        public void Document Create()
        {
            return DocCreatorClass.Create();
        }
    
        public void Save(Document doc)
        {
            _documentStore.PersistToDisk(doc);  
        }
    
        public void CreateAndSave()
        {
            Save(Create());
        }
    }
    

    This way, you don’t always have to immediately save to disk the newly-created document. CreateAndSave() would be a convenience function that calls both Save() and Create() inside it, in case your program wants to immediately save a new document often enough.

    This type of design is definitely more coding which may come across as more complicated. In the long run, it’s easier to look at and maintain because each function does only one thing.

    I personally stick with (most of the time, since there may be exceptions) the one class, one responsibility rule. This makes it easier to locate a part of your project when you notice that a functionality doesn’t work. When you work on fixing it, you can be rest assured that the rest of your application (the other tasks, thus classes) is untouched. For functions, I like to create them in such a way that within a class, no code blocks will be repeated in two or more different places. This way, you won’t have to hunt down all of those identical code blocks to update.

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

Sidebar

Related Questions

I have this class which i would like to map: public class Contract {
I have this class which I want to pass around Windows as LPARAM parameter.
So I have this class which extends an activity. But I want to draw
I have this class in which I run a for loop 10 times. This
I have a class which has implemented INotifyPropertyChanged . This class UserInfo has a
I'm trying to validate an ID. I have this class called ManejadorTickets in which
I have class A which extends the Activity class. This class is in package
have written this little class, which generates a UUID every time an object of
I have this script which basically toggles a bgColor class on and off so
I have this middleware which is the first: class RedirectIt require net/https require uri

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.