Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2011
    Location
    Australia
    Posts
    39

    Easy RegisterBundles expansion

    When Nelson was talking about creating the bundles I thought you could quickly get to a point where your RegisterBundles function was full of very similar code repeated endlessly to add more style bundles. I also wanted to be able to use the navbar-collapse class for my navbar which uses jquery and a few bootstrap Javascripts. After a few minutes I had a nice little function to create my style bundles and also my javascript bundles.

    Code:
    public static void RegisterBundles(BundleCollection bundles)
    {
        var styles = new Dictionary<string, string[]>();
        var scripts = new Dictionary<string, string[]>();
    
        styles.Add("~/styles/frontend", new[] { "~/content/styles/application.less" });
        styles.Add("~/styles/backend", new[] { "~/content/styles/application.less" });
    
        scripts.Add("~/scripts/jquery", new[] { "~/content/scripts/jquery-1.11.1.js" });
        scripts.Add("~/scripts/collapse", new[] { "~/content/scripts/bootstrap/collapse.js" });
        scripts.Add("~/scripts/forms", new[] { "~/content/scripts/forms.js" });
    
        foreach (var style in styles)
        {
            var bundle = new StyleBundle(style.Key);
            bundle.Transforms.Add(new CssTransformer(new List<ITranslator> { new LessTranslator() }));
            bundle.Transforms.Add(new CssMinify());
            bundle.Include(style.Value);
            bundles.Add(bundle);
        }
    
        foreach (var script in scripts)
        {
            var bundle = new ScriptBundle(script.Key);
            bundle.Transforms.Add(new JsMinify());
            bundle.Include(script.Value);
            bundles.Add(bundle);
        }
    }
    My original code would only accept a single script or style into each bundle, but when Nelson implemented the Javascript bundles I saw you could add an array of strings, so I changed the code to implement that also. To add more style bundles or script bundles to my BundleCollection is now a matter of doing:

    Code:
    styles.Add("~/styles/BUNDLENAME", new [] { "~/content/styles/STYLENAME.less" });
    scripts.Add("~/scripts/BUNDLENAME", new [] { "~/content/scripts/SCRIPT1.js", "~/content/scripts/SCRIPT2.js" });
    Very simple to expand them and also stops you writing the same code a billion times to implement more styles or scripts.

    If you are wondering, I put my scripts in the content folder to keep my layout consistent.

  2. #2
    Join Date
    May 2014
    Posts
    1
    Well, just so you know, the Include function on either ScriptBundle or StyleBundle is implemented as an array because the function definition is
    Code:
    public virtual Bundle Include(
    	params string[] virtualPaths
    )
    Taken from http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    What this means is you can instead of chaining, simply call the function as if it has an unlimited* amount of paramaters. C# will on it's end see all the parameters it has received as a single array. You don't need to provide an array yourself.

    example:
    Code:
    bundles.Add(net ScriptBundle("~/scripts").Include("~/content/scripts/jquery-1.11.1.js", "~/content/scripts/bootstrap/collapse.js", "~/content/scripts/forms.js"));
    It's similar to chaining. I have not tested this, but I imagine the scripts are included in order as they appear as parameters. So again first adding your dependencies before adding the rest. But this code should create one script file, minified from the three above.

    While your code is valid, it just adds a level of abstraction that is already possible with the bundles as they exist. So I'd recommend just using it as is. Everyone has their own coding style, and I see what you are doing as valid, but perhaps a bit of a workaround.

    While nelson's videos on MVC were excellent and a sharp base, they sometimes don't delve too deep into every subject. They'll give a baseline. The rabbit hole of MVC is far deeper. Especially considering MVC has been updated several times since and has added tonnes of new features.

    But the key thing is, sometimes abstraction is not necessary, you can just already do it.

    Anyway, Good luck on learning more MVC.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •