jeudi 24 décembre 2020

Separate logic from data in c#

In the first place let me explain what is in my mind :

A few days ago while I was reading an article regarding Builder design pattern , The point that drew my attention was the idea of separating logic from data that Builder pattern founded based on, Now its kind of confusing how to answer following questions:

1.What are exactly Logic and Data and how to differentiate between the two?

For example, you could consider my below pieces of codes that validate some specific classes:

  //Part 1
   var FailureAttr = step.GetType().GetCustomAttributes(typeof(KeepOperationInFailureAttribute), true).FirstOrDefault() as KeepOperationInFailureAttribute;
            var InitialStepAttr = step.GetType().GetCustomAttributes(typeof(InitialStepAttribute), true).FirstOrDefault() as InitialStepAttribute;
            var stepInfo = new StepInfoProps();
                stepInfo = new StepInfoProps()
                {
                    IsInitialStep = InitialStepAttr != null,
                    KeepOperationInFailure = FailureAttr != null
                };

            StepInfoList.Add(stepInfo);

        }
        var noInitialStep = !StepInfoList.Any(x => x.IsInitialStep);
        var moreThanOneInitialStep = StepInfoList.Where(x => x.IsInitialStep).Count() > 1;
        var DuplicateStepNames = string.Join(",", steps.GroupBy(x => x.PiplineStep).Where(x => x.Count() > 1).Select(x => x.Key).ToList());

//Part 2
        if (noInitialStep)
        {
            exceptions.Add(new Exception(Resources.InitialStepNotDefined));
        }
        if (moreThanOneInitialStep)
        {
            exceptions.Add(new Exception(Resources.MoreThanOneInitialStep));
        }
        if (!string.IsNullOrEmpty(DuplicateStepNames))
        {
            exceptions.Add(new Exception(string.Concat(Resources.RepeatedSteps, DuplicateStepNames)));
        }
        if (exceptions.Count > 0)
        {
            throw new AggregateException("Pipeline validation error , for more detail see inner exception.", exceptions);
        }
        return true;

Is it true to consider part 1 as data part and part 2 as logic part?

2.does a good structure should always separate these two parts?

Aucun commentaire:

Enregistrer un commentaire