lundi 27 juin 2016

c# Builder Pattern

I have below class which builds reference data for my app, do you think it's a good implementation of builder pattern. What can I change to improve it?

public class ReferenceDataBuilder
    {
        private readonly List<Task> _builderTasks;
        private IDataProvider _dataAccess;
        private ReferenceData _referenceData;
        private bool _withPersistence;
        private string _persistenceDirectory;

        public ReferenceDataBuilder(IDataProvider dataAccess)
        {
            ValidationUtils.ArgumentNotNull(dataAccess, "dataAccess");

            _dataAccess = dataAccess;

            _referenceData = new ReferenceData();
            _builderTasks = new List<Task>();
            createBuilderTasks();
        }

        public virtual ReferenceData BuildAsync()
        {
            Parallel.ForEach(_builderTasks, (task) => { task.Start(); task.Wait(); });
            return _referenceData;
        }


        private void createBuilderTasks()
        {
            _builderTasks.Add(new Task(() => _referenceData.Object1 = _dataAccess.GetObject1()));
            _builderTasks.Add(new Task(() => _referenceData.Object2 = _dataAccess.GetObject2()));
            _builderTasks.Add(new Task(() => _referenceData.Object3 = _dataAccess.GetObject3());
            _builderTasks.Add(new Task(() => _referenceData.Object4 = _dataAccess.GetObject4());
        }
    }

Usage:

new ReferenceDataBuilder(dataAccess).BuildAsync();

Thanks

Aucun commentaire:

Enregistrer un commentaire