1

This piece of code is in many of my methods, in a certain area of the codebase. It's not violating DRY since its a bit different for every method. But it still bothers me that 5-8 lines of code like this is in every method.

So I want to hide it away :) The question is how.

//e.g /configurators/configjohn/defaultfrontend/active/somejsonfile_versionumber.json
var packageFilter = PackagePath.Filter
    .AddPathFilter(StorageConstants.UICONFIGURATION_BASE_CONTAINER)
    .AddPathFilter(configurationName.ToLowerInvariant())
    .AddPathFilter(uiConfigInfo.Name.ToLowerInvariant())
    .AddPathFilter(status.ToLowerInvariant())
    .AddFilter(PackagePath.PackageVersion(uiConfigInfo.Version))
    .Get();

I thought about a factory where I could give the method as a parameter and I would get back a packageFilter for that method.

Any other recommendations ?

Thanks!

UPDATE to solution

Ended up creating a static type like I was advised to do;

public class PackagePathCreator
{
    public static PackagePath CreateFilter(string configurationName, string segment, string status, string uiConfigName)
    {
        var packageFilters = PackagePath.Filter;

        if (!string.IsNullOrEmpty(segment))
            packageFilters.AddPathFilter(segment);

        if (!string.IsNullOrEmpty(configurationName))
            packageFilters.AddPathFilter(configurationName.ToLowerInvariant());

        packageFilters.AddPathFilter(StorageConstants.UICONFIGURATION_BASE_CONTAINER).
        AddPathFilter(uiConfigName.ToLowerInvariant()).
        AddPathFilter(status.ToLowerInvariant());

        return packageFilters;
    }
}
  • what does it do? – Ewan Oct 2 '17 at 15:45
  • It builds up a path string for a blob resource. – frostings Oct 2 '17 at 16:26
3

Create a helper class that contains convenient methods and parameters, so you can call them in different places of your code.

Example of helper class:

class PackagePathHelper {

    public static PackagePath GetFromBaseContainer(string status, string configName, UIConfigInfo uiConfigInfo) {
        var packageFilter = PackagePath.Filter
            .AddPathFilter(StorageConstants.UICONFIGURATION_BASE_CONTAINER)
            .AddPathFilter(configName.ToLowerInvariant())
            .AddPathFilter(uiConfigInfo.Name.ToLowerInvariant())
            .AddPathFilter(status.ToLowerInvariant())
            .AddFilter(PackagePath.PackageVersion(uiConfigInfo.Version))
            .Get();
    }
}

Example of calling code:

var packageFilter = PackagePathHelper.GetFromBaseContainer(status, configurationName, uiConfigInfo);
3

Its hard to say, but you seem to be constructing the final string from configuration settings.

These settings are presumably in a config file. It might be better to store each complete string in the config file rather than storing the parts and then assembling them in code.

You may find this decreases both the number of config variables AND the lines of code. Whilst also allowing more flexibility in the configuration of the app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.