If a class has one open entry point that accepts everything needed as parameters, should it be static?

I just finished coding a class and realized that all its public functions are encapsulated in one method. It has no properties, no shared resources, and does not require constructor overloading and does not process everything that it uses. It looks something like this:

public class ReportGenerator
{
    public string GenerateReport(List<SomeClass> stuffToReportOn)
    {
        string fileName = String.Empty;
        using(var reportDs = CreateDataSet(stuffToReportOn))
        {
            //do the stuff with the third party tool that 
            //creates the report.
            //Construct the filename.
            //Save the report.
        }
        return fileName;
    }

    private TypedDataSetRequiredByThirdPartyLib CreateDataSet(List<SomeClass> reportItems)
    {
        //create the dataset, calling two other private methods
        //to build the tables/rows
    }
}

After I finished refactoring, I realized that this class can be completely static. My question is, right? Should a class that encapsulates all its functions in one public method be static?

+3
source share
4 answers

No. and what are the expected benefits?

. , , , -. , .

+5

, - , - .

, FakeItEasy, , ( , , TDD), .

UPDATE
, unit test GenerateWidgetReports, ReportGenerator.GenerateReport. , stuffToReportOn Widget1 Widget2. ?

GenerateWidgetReports GetStuffToReportOn, , . GenerateWidgetReports , GetStuffToReportOn ReportGenerator.GenerateReport.

GenerateWidgetReports, .

GenerateWidgetReports IReportGenerator, GenerateReport Widget1 Widget2.

Mocking, GenerateReport .

2
, TypeMock . .

+3

, :

  • ? , ?
  • ?
  • ?

, , :

public static class ReportExtension
{
    public static string GenerateReport(this List<SomeClass> stuffToReportOn)
    {
        string fileName = String.Empty;
        using(var reportDs = CreateDataSet(stuffToReportOn))
        {
            //do the stuff with the third party tool that 
            //creates the report.
            //Construct the filename.
            //Save the report.
        }
        return fileName;
    }

    private static TypedDataSetRequiredByThirdPartyLib CreateDataSet(List<SomeClass> reportItems)
    {
        //create the dataset, calling two other private methods
        //to build the tables/rows
    }
}

, .

:

You still have unit testing problems because the code depends on this static method. Ultimately, you have to decide on the pros and cons and make your own decision. I could be wrong, but if you were unit test dependent code with this as an instance class, you can get some confidence that it should be exact as a static or advanced method.

+1
source

Yes, this will make it easier to use your class.

0
source

Source: https://habr.com/ru/post/1772902/


All Articles