mardi 30 mai 2017

Design pattern for refactoring out class methods with many shared variables

I have the class DataManipulation with the 3 functions ExtractData, Validate, and FinishForms.
All of the methods need access to the private variable of the class (there are quite a few more, example shorter for readability).

The actual class is very long and I would like to separate it into 3 classes, one for each of the methods I have shown.
I use dependency injection to instantiate the class and simply separating the 3 methods into 3 classes would mean passing around quite a few variables, which I think does not help readability.

TL;DR. I have a very long class where methods manipulate a lot of internal variables. Methods can be grouped into 3 distinct areas of responsibilities, but dividing the class into 3 would mean passing around a lot of the internal variables.

I am looking for the right design pattern to use for my problem.

public class DataManipulation
    {
        private readonly IUnityContainer unity, 

        private ImportModel VarUsedEverywhere;

        private List<string> _errorMessages = new List<string>();

        public DataManipulation(IUnityContainer unity)
        {
            _unity = unity;
        }


        public async Task<ImportLogModel> Process(Stream importFile)
        {

            ExtractData(importFile);

            await Validate();

            await FinishForms();

            return VarUsedEverywhere;
        }
    }

.

        private bool ExtractData(Stream importFile)
        {
            //Extract data into VarUsedEverywhere
            //Add to _errorMessages if operations do not succeed.

        }

.

        private bool Validate()
        {
            //validate data in VarUsedEverywhere
            //Add to _errorMessages if operations do not succeed.
        }

.

        private bool FinishForms()
        {
           //Order all the values in VarUsedEverywhere
           //Add to _errorMessages if operations do not succeed.
        }

Aucun commentaire:

Enregistrer un commentaire