jeudi 14 mars 2019

How to avoid code duplication in c# project

I have a situation where I am using specflow custom plugin. Using specflow custom plugin I have two different projects let's say project_A and project_B. Both the project has their own repository.

Project_A has below class (please find the code snippet) :

public class CustomTestGeneratorProvider : IUnitTestGeneratorProvider
{
    public CustomTestGeneratorProvider (CodeDomHelper codeDomHelper)
    {
        _unitTestGeneratorProvider = new 
        NUnit3TestGeneratorProvider(codeDomHelper);
        CodeDomHelper = codeDomHelper;
    }

    public UnitTestGeneratorTraits GetTraits()
    {
        return _unitTestGeneratorProvider.GetTraits();
    }

    public void SetTestClass(TestClassGenerationContext generationContext, string featureTitle,
        string featureDescription)
    {
        _unitTestGeneratorProvider.SetTestClass(generationContext, featureTitle, featureDescription);
        generationContext.Namespace.Imports.Add(new CodeNamespaceImport("Com.MyOrg.Custom.Core.Feature"));
        generationContext.TestClass.BaseTypes.Add("MyOrgTest");
     }

    public void SetTestClassCategories(TestClassGenerationContext generationContext,
        IEnumerable<string> featureCategories)
    {
        _unitTestGeneratorProvider.SetTestClassCategories(generationContext, featureCategories);
    }

Project_B has below class (please find the code snippet) :

public class CustomTestGeneratorProvider : IUnitTestGeneratorProvider
{
    public CustomTestGeneratorProvider (CodeDomHelper codeDomHelper)
    {
        _unitTestGeneratorProvider = new 
        NUnit3TestGeneratorProvider(codeDomHelper);
        CodeDomHelper = codeDomHelper;
    }

    public UnitTestGeneratorTraits GetTraits()
    {
        return _unitTestGeneratorProvider.GetTraits();
    }

    public void SetTestClass(TestClassGenerationContext generationContext, string featureTitle,
        string featureDescription)
    {
        _unitTestGeneratorProvider.SetTestClass(generationContext, featureTitle, featureDescription);
    }

    public void SetTestClassCategories(TestClassGenerationContext generationContext,
        IEnumerable<string> featureCategories)
    {
        _unitTestGeneratorProvider.SetTestClassCategories(generationContext, featureCategories);
    }

If you look at SetTestClass method in the class has different implementation of logic. This is the only method is becoming maintence for me. Note that both the project do same thing but the user for them are different. At the time of release If I change in project_A I should take care or think about project_B also.

I would like to minimize this maintenance. What should I do to reduce the maintenance?. How to avoid code duplication? in such scenario.

Aucun commentaire:

Enregistrer un commentaire