Skip to content

Instantly share code, notes, and snippets.

@adamjez
Last active January 30, 2018 12:29
Show Gist options
  • Select an option

  • Save adamjez/78b037690ed36d44d6656032f01db037 to your computer and use it in GitHub Desktop.

Select an option

Save adamjez/78b037690ed36d44d6656032f01db037 to your computer and use it in GitHub Desktop.
Fluent API Interface prototype for resource registration in DotVVM
public static class Program
{
    public static void Main(DotvvmResourceRepository resources)
    {
        // -----------------------------------------
        // ------------------ OLD ------------------
        // -----------------------------------------
        resources.Register("FeatureSamples_Resources_CdnScriptPriority", new ScriptResource {
            Location = new FileResourceLocation("~/Scripts/testResource.js"),
            LocationFallback = new ResourceLocationFallback("window.dotvvmTestResource", new FileResourceLocation("~/Scripts/testResource2.js"))
        });

        resources.Register("extenders", new ScriptResource {
            Location = new FileResourceLocation("Scripts/ClientExtenders.js")
        });

        resources.Register(ResourceConstants.KnockoutJSResourceName,
            new ScriptResource(new EmbeddedResourceLocation(
                typeof(DotvvmConfiguration).GetTypeInfo().Assembly,
                "DotVVM.Framework.Resources.Scripts.knockout-latest.js")));

        resources.Register(ResourceConstants.DotvvmResourceName + ".internal",
        new ScriptResource(new EmbeddedResourceLocation(
            typeof(DotvvmConfiguration).GetTypeInfo().Assembly,
            "DotVVM.Framework.Resources.Scripts.DotVVM.min.js")) {
            Dependencies = new[] { ResourceConstants.KnockoutJSResourceName, ResourceConstants.PolyfillResourceName },
            VerifyResourceIntegrity = true,
            IntegrityHash = "sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
        });

        // -----------------------------------------
        // ------------------ NEW ------------------
        // -----------------------------------------
        resources.RegisterScript("FeatureSamples_Resources_CdnScriptPriority")
            .WithFileLocation("~/Scripts/testResource.js")
            .WithFallback("window.dotvvmTestResource")
                .WithFileLocation("~/Scripts/testResource2.js");

        resources.RegisterScript("extenders")
            .WithFileLocation("Scripts/ClientExtenders.js");

        resources.RegisterScript(ResourceConstants.KnockoutJSResourceName)
            .WithEmbeddedResourceLocation(typeof(DotvvmConfiguration).GetTypeInfo().Assembly, "DotVVM.Framework.Resources.Scripts.knockout-latest.js");

        resources.RegisterScript(ResourceConstants.DotvvmResourceName + ".internal")
            .WithEmbeddedResourceLocation(typeof(DotvvmConfiguration).GetTypeInfo().Assembly, "DotVVM.Framework.Resources.Scripts.DotVVM.min.js")
            .WithDependencies(ResourceConstants.KnockoutJSResourceName, ResourceConstants.PolyfillResourceName)
            .VerifyResourceIntegrity("sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=");
    }
}
);
    }
}

public static class ResourcesExt
{
    public static IScriptResourceBuilder RegisterScript(this DotvvmResourceRepository repository, string resourceName)
    {
        var scriptResource = new ScriptResource();
        repository.Register(resourceName, scriptResource);

        return new DefaultScriptResourceBuilder(scriptResource);
    }

    public static IScriptResourceBuilderWithLocation WithUrlLocation(this IScriptResourceBuilder builder, string url)
        => builder.WithLocation(new UrlResourceLocation(url));

    public static IScriptResourceBuilderWithLocation WithFileLocation(this IScriptResourceBuilder builder, string filePath)
        => builder.WithLocation(new FileResourceLocation(filePath));

    public static IScriptResourceBuilderWithLocation WithEmbeddedResourceLocation(this IScriptResourceBuilder builder, Assembly assembly, string name, string debugFileName = null)
       => builder.WithLocation(new EmbeddedResourceLocation(assembly, name, debugFileName));
}

public class DefaultScriptResourceBuilder : IScriptResourceBuilder
{
public static class ResourcesExt
{
    public static IScriptResourceBuilder RegisterScript(this DotvvmResourceRepository repository, string resourceName)
    {
        var scriptResource = new ScriptResource();
        repository.Register(resourceName, scriptResource);

        return new DefaultScriptResourceBuilder(scriptResource);
    }

    public static IScriptResourceBuilderWithLocation WithUrlLocation(this IScriptResourceBuilder builder, string url)
        => builder.WithLocation(new UrlResourceLocation(url));

    public static IScriptResourceBuilderWithLocation WithFileLocation(this IScriptResourceBuilder builder, string filePath)
        => builder.WithLocation(new FileResourceLocation(filePath));

    public static IScriptResourceBuilderWithLocation WithEmbeddedResourceLocation(this IScriptResourceBuilder builder, Assembly assembly, string name, string debugFileName = null)
       => builder.WithLocation(new EmbeddedResourceLocation(assembly, name, debugFileName));
}

public class DefaultScriptResourceBuilder : IScriptResourceBuilder
{
    private readonly ScriptResource scriptResource;

    public DefaultScriptResourceBuilder(ScriptResource scriptResource)
    {
        this.scriptResource = scriptResource;
    }

    public IScriptResourceBuilderWithLocation WithLocation(IResourceLocation location)
    {
        scriptResource.Location = location;

        return new DefaultScriptResourceBuilderWithLocation(scriptResource);
    }
}

public class DefaultScriptResourceBuilderWithLocation : IScriptResourceBuilderWithLocation
{
    private readonly ScriptResource scriptResource;

    public DefaultScriptResourceBuilderWithLocation(ScriptResource scriptResource)
    {
        this.scriptResource = scriptResource;
    }

    public IScriptResourceBuilderWithLocation VerifyResourceIntegrity(string integrityHash = null)
    {
        scriptResource.VerifyResourceIntegrity = true;
        scriptResource.IntegrityHash = integrityHash;

        return this;
    }

    public IScriptResourceBuilderWithLocation WithDependencies(params string[] dependencies)
    {
        scriptResource.Dependencies = dependencies;
        return this;
    }

    public IScriptResourceBuilderWithFallbackLocation WithFallback(string javascriptCondition)
    {
        return new DefaultScriptResourceBuilderWithFallbackLocation(this.scriptResource, javascriptCondition);
    }
}

public class DefaultScriptResourceBuilderWithFallbackLocation : IScriptResourceBuilderWithFallbackLocation
{
    private readonly string javascriptCondition;
    private readonly ScriptResource scriptResource;

    public DefaultScriptResourceBuilderWithFallbackLocation(ScriptResource scriptResource, string javascriptCondition)
    {
        this.scriptResource = scriptResource;
        this.javascriptCondition = javascriptCondition;
    }

    public IScriptResourceBuilderWithLocation WithLocation(IResourceLocation location)
    {
        return WithLocations(location);
    }

    public IScriptResourceBuilderWithLocation WithLocations(params IResourceLocation[] locations)
    {
        scriptResource.LocationFallback = new ResourceLocationFallback(javascriptCondition, locations);
        return new DefaultScriptResourceBuilderWithLocation(scriptResource);
    }
}

public interface IScriptResourceBuilder
{
    IScriptResourceBuilderWithLocation WithLocation(IResourceLocation location);
}

public interface IScriptResourceBuilderWithLocation
{
    IScriptResourceBuilderWithLocation WithDependencies(params string[] dependencies);
    IScriptResourceBuilderWithFallbackLocation WithFallback(string javascriptCondition);
    IScriptResourceBuilderWithLocation VerifyResourceIntegrity(string integrityHash = null);
}

public interface IScriptResourceBuilderWithFallbackLocation : IScriptResourceBuilder
{
    IScriptResourceBuilderWithLocation WithLocations(params IResourceLocation[] locations);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment