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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:07:43+00:00 2026-05-13T17:07:43+00:00

I have a class that outputs a simple report file. It reads some record

  • 0

I have a class that outputs a simple report file. It reads some record ID numbers from an XML file: each one used for finding a matching record stored in a database. It then writes out each record’s details to a CSV file.

I am wondering – what is the best way to organise it so that it is easy to test, yet follows the principles of encapsulation? I gather that it is best to avoid interacting with the file system unless absolutely necessary so I am dealing with Stream objects. When unit testing I can use partial mock objects to override the bits that read or write to files.

I am also unsure of when/where to dispose the streams without making unit testing tricky. It looks like I might have to expose the streams to the unit test.

My project uses NHibernate for data access, Spring .NET for dependency injection, and Rhino.Mocks for unit testing.

Currently I have something similar to this :

public class ReportBiz
{
    //Access to repository, populated by Spring
    internal ICardRequestDAO CardRequestData { get;set;} 

    //This normally returns a FileStream containing data from the XML file. When testing this is overridden by using a Rhino.Mocks partial mock and returns a MemoryStream
    internal virtual Stream GetSourceStream()
    {
        //Load file and return a Stream
        ...
    }

    //This normally returns a FileStream where CSV data is saved. When testing this is overridden by using a Rhino.Mocks partial mock and returns a MemoryStream
    internal virtual Stream GetOutputStream()
    {
        //Create file where CSV data gets saved and return a Stream
        ...
    }

    public void CreateReportFile()
    {
        Stream sourceStream = GetSourceStream();
        ...

        //Create an XmlDocument instance using the stream
        //For each XML element, get the corresponding record from the database
        //Add record data to CSV stream     
        ...
    }
    }

Would it be better to use some kind of custom factory or something and pass the streams into the constructor? But what about if there is some business logic involved, e.g. the filename is determined based on results of a query?

Or is the whole file access thing not a problem after all?

Apologies if I’m missing something obvious. I would be grateful for any advice.

  • 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-13T17:07:44+00:00Added an answer on May 13, 2026 at 5:07 pm

    The simplest way make the file access mock-able while retaining control over the lifecycle of the disposable resources is to inject a StreamFactory into your class:

    public class ReportBiz {
    
        private IStreamFactory streamFactory;
    
        public ReportBiz(IStreamFactory streamFactory) {
            this.streamFactory = streamFactory
        }
    
        public void CreateReportFile() {
            using(Stream stream = this.streamFactory.CreateStream()) {
                // perform the important work!
            }
        }
    }
    

    When there’s more business logic involved, your factory method may be a bit more elaborate, but not by much:

    public void CreateReportFile() {
        string sourcePath   = this.otherComponent.GetReportPath();
        using(Stream stream = this.streamFactory.CreateStream(sourcePath)) {
            // perform the important work!
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that pulls some information from the database and creates some
I have a class that encapsulates tcp socket communications with a server. For each
I have a class that processes a 2 xml files and produces a text
I have class method that returns a list of employees that I can iterate
I have a class that I want to use to store properties for another
I have a class that map objects to objects, but unlike dictionary it maps
I have a class that I wish to expose as a remote service using
I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have a class that creates several IDisposable objects, all of these objects are
I have a class that utilizes a directory swap method for the Environment.CurrentDirectory. The

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.