Reuse, Reuse, Reuse – do we need utility libraries? If not, what’s next? Minimods!

We love being productive. And there are tons of generic problems that we solve far too often. The .NET Base Class Library has plenty of functionality (compared to Java w/o Apache.Commons). But still there is much missing.

So, every developer has tons of snippets in his backyard, from where he digs up and copy-pastes class by class, method by method. Then, from time to time, in the name of reuse, we package those utilities in libraries called *.Common or *.Utils, and put them in our toolbox. Some of us (as a species) then publish their util-libraries as open source: Among them Rhino.Commons, Umbrella, Magnum and many more.

The cost of dependencies

The cost of an dependency is high. If you just need that little thing, but introduce a dependency to a big ball of (unrelated) mud – you’re better off copy-pasting exactly what you need. But event that is hard, because if you start writing utilities, then your utilities tend to depend on each other.

Good reads on this topic:

But still…

But still we do not want to reinvent the wheel on every project. So what to do?

Options

Lets just shortly list the options we have for reusing generic code – besides creating huge utility libraries:

  • Copy-paste
  • Creating very specific libraries: but who want’s the overhead of a DLL?
  • … IL-merge those very specific libraries
  • Utilize .NET modules; those are smaller than an assembly and can be linked into any assembly – that is what ILmerge does. But there is no tooling support for .NET modules in Visual Studio…

I think there is just one option left: Single-file distributions with utility-classes, that can be included as source.

But how to distribute them? How to version? How to upgrade?

Then, how to maintain them? Manual download, copy-paste? In times of NPanday and Nuget? Not really.

Minimods

With this in mind I started off something I’ll call Minimod for now. It is a rather small module, containing of source code only, that can be installed into any of your C# assemblies.

I’ll write more on how to create and publish a Minimod later (hopefully in combination with NPanday).

Definition

A Minimod is defined as follos:

  • it must consist of one file and be very specific (no StringExtensionsMinimo, LinqExtensionsMinimod, or MyUtilMinimod please!!)
  • Each Minimod must be in it’s own namespace in order to avoid extension-method conflicts.
  • A Minimod should only depend on .NET BCL or other Minimods

I also want tool-support for integrating it, as well as upgrade/remove.

Proof of Concept

I just started bundling some utilities in classes and created simple Nuget-Packages. This makes it very easy to install and upgrade the files.

image

Then I published those to the Nuget Gallery.

image

When installing, a file will be created in the subdirectory Minimods. I named it *.Generated.cs, because that will prevent ReSharper from complaining about things.

image

Preview of the imported file. Namespace does not fit (see issues).

image

Now we use them:

using Minimod.NestedDictionary;
using Minimod.PrettyText;
using Minimod.PrettyTypeSignatures;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var width = 22;
            Console.WindowWidth = width;

            var minimods = "Minimods are great! Go get them at Nuget.org, just by searching for 'minimod'.";

            Console.WriteLine(
                minimods
                .ShortenTo(width)
                );

            Console.WriteLine();

            Console.WriteLine(
                minimods
                .WrapAt(width)
                .JoinLines());

            var listPerKey =
                new Dictionary<string, List<string>>();

            Console.WriteLine();

            Console.WriteLine(
                listPerKey.GetType()
                .GetPrettyName()
                .ShortenTo(width));

            listPerKey.EnsureValueFor("key1");
            listPerKey.AddToCollection("key2", "one");
            listPerKey.AddToCollection("key2", "two");

            Console.WriteLine();

            Console.WriteLine("key1: " +
                String.Join(", ", listPerKey["key1"]));

            Console.WriteLine("key2: " +
                String.Join(", ", listPerKey["key2"]));

            Console.ReadKey();
        }
    }
}

The result

image

Still to be considered

  • Namespace should maybe be adjusted to the target-projects namespace + “.Minimods”? Or is it great having *.Minimod for all of them?
  • Id, description and versioning is all done manually now – want to automate this.
  • Haven’t tried with dependencies yet, but that should work.
  • Planning to use NPanday to automate the process of releasing and publishing Minimods ([#NPANDAY-372] Integrate with NuGet and NuGet Gallery (nuget.org))

What do you think? I’d be happy to get Feedback on the process!

I’ll also hopefully keep posting more Minimods.

Current source code is here: moved to minimod/minimods at GitHub (from Braindrops.MiniMods at GitHub)

Advertisements

6 thoughts on “Reuse, Reuse, Reuse – do we need utility libraries? If not, what’s next? Minimods!

  1. Hi Lars,
    a nice post and an interesting mind opener to think about NuGet range of application.
    That we have the need for managing small pieces of code over several projects is clear – wether as source or binary.
    I played a little bit with your minimods over NuGet approach. From the viewpoint of managing i see no drastic difference between source and binary distribution style.
    But i have the fear, that over the time we loose the overview if we had too many setup ways. So i would prefer the binary variant with the drawback of binding overhead in particular by usage of tons of small assemblies – because we need this anyway for other packages.
    Than also the adjusting of improper namespaces in minimods is obsolete.
    But i will follow your experiences with minimods over NuGet.
    Sunny regards, Marko

    • Hi Marko,

      good comment. The thing is, I had never created “real” OSS projects with binary distributions for each of the minimods. And even thinking of one DLL for all of them is a nightmare…

      I would have used copypaste. Minimods is copy-paste on steroids; more than a replacement for real libraries.

      _
      Lars

  2. Interesting approach, i was already doing that…however in a somewhat…”old fashioned” way ^^

    I actually simply have a folder with a lot…yep a loooot ….cs files
    Stuff i tend to use often and stuff i used up to stuff i never used but found somewhere on the net.
    Your “Minimod” approach seems like a very cool idea.

    Related to the Namespace…

    One should add the area to the namespace …for example “minimod.ui.renderhelper”

    If this goes further and people publish things…you need a ordered list of minimod´s.

    having implementations like:

    minimod.ui.prettytext
    minimod.dictionaries.NestedDictionary

    seems like a better approach…

    The one question is…. what can a minimod be?
    – only one extension function?
    – a set of 10 or more functions that all extend a specific element (textbox for example)
    – anything you want it to be?

    regards,
    TC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s