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

Spot the bug #2 (Code)

Yesterday we stumbled some odd behavior. Totally logical and well defined, but still odd.

I reduced the classes to it’s essentials. Finder is a base class among other things assuring it’s implementor can rely on being called only once per instance.

The particular implementation uses member variables to detect circles in a tree.

Can you spot the problem? Can you tell me how to solve it?

(Update 12.01.2011: While anonymizing I had introduced another bug. Fixed now.)

public abstract class Finder
{
    private bool _used;
    public IEnumerable<int> GetIds()
    {
        if (_used)
            throw new NotSupportedException("Please do not use twice!");

        _used = true;

        return getIds();
    }

    protected abstract IEnumerable<int> getIds();
}

public class FlatHierarchyIdsFinder : Finder
{
    List<int> _visited = new List<int>();
    protected override IEnumerable<int> getIds()
    {
        if (_visited.Contains(1))
            throw new ApplicationException("Cycle detected!");

        _visited.Add(1);
        yield return 1;
    }
}

It might help you to read this post: Behind the scenes of the C# yield-keyword

NHibernate 3.0 GA and Castle Windsor 2.5.2

NHibernate 3.0 has gone GA quietly on December 4. For those that used the Alpha 1 together with Castle Windsor, there might be some obstacles!

NHibernate.ByteCode.Castle was updated to use Castle 2.5.2, which now has changed it’s assembly structure. Castle.DynamicProxy2.dll was merged into Castle.Core.dll and Castle.MicroKernel.dll is now included in Castle.Windsor.dll.

If you have dependencies again taking dependency on MicroKernel or Dynamic Proxy, in my case MvcTurbine, a simple assembly redirect wont help. You will need to recompile third-party libraries or wait for a new release.

Naming is important

Just stumbled over a “bug” in .NET 4.0 DataAnnotations/Validator that was quite easy to “work around”, but still took us a while to figure out.

We want to do simple structural server-side validations.

Data Annotations

/* haven’t installed my Live Writer beautifier-plugin yet */

public class SomeCommand : Command {

    [Required] public string Name { get; set; }

    [Required] public sting City { get; set; }

    [Range(0,99)] public string Age { get; set; }

}

The base class uses System.ComponentModel.DataAnnotations.Validator to run the validations in this simple Method:

private IEnumerable<ValidationResult> AutoValidation()
{
    var results = new List<ValidationResult>();
    Validator.TryValidateObject(this, new ValidationContext(this, null, null), results);
    return results;
}

Problem

It only validated the fields marked as required. The RangeAttribute was ignored.

After a while we had a look at the .NET-Framework-Sources using Reflector, and found this:

if (validateAllProperties)
{
    source.AddRange(GetValidationErrors(pair.Value, pair.Key, propertyValidationAttributes, breakOnFirstError));
}
else
{
    RequiredAttribute validationAttribute = propertyValidationAttributes.FirstOrDefault<ValidationAttribute>(delegate (ValidationAttribute a) {
        return (a is RequiredAttribute);
    }) as RequiredAttribute;
    if (validationAttribute != null)
   {
       // validate the RequiredAttribute

validateAllProperties in this case means: do not only run Required-Validations.

Solution

Actually a different override of Validator.TryValidateObject has a flag named validateAllProperties. But since it did already validate more than one property, I assumed true was the default.

Lesson I learned: do not assume anything!

Lesson for MS: Take time finding the right name! Do refactor!

I filed a bug here: https://connect.microsoft.com/VisualStudio/feedback/details/605635/missleading-parametername-validateallproperties-in-validator-try-validate-componentemodel-dataannotations

Size calculations for image resizing (C#)

I just reused some code I wrote a couple of years ago, and thought I’d share it here.

It’s not about the best ways to resize images. If that is what you search for, have a look at this 🙂

It’s rather some code for calculating the size of the target image from the originals size while applying one of four rules:

  • Fit: Often you want to fit the image into a box – but without stretching it. So if you have a image that is 50×100 and you want to fit it into 50×50, the result is 25×100. If the original is 100×50, the result would be 50×25. Getting it?
  • Stretch: Not to useful, but sometimes needed 🙂
  • FixedHeight: Maintain the ratio, adjust to height.
  • FixedWidth: Maintain the ration, adjust to width.

Usage

Pretty straight-forward, I think.

var _100_50 = new Size(100, 50);
ResizeType.Fit.CalcNewSize(_100_50, new Size(50, 50), true)
    .Satisfy(s => s.Width == 50 && s.Height == 25);

var _50_100 = new Size(50, 100);

ResizeType.Fit.CalcNewSize(_50_100, new Size(50, 50), true)
    .Satisfy(s => s.Width == 25 && s.Height == 50);

ResizeType.Stretched.CalcNewSize(_50_100, new Size(500, 500), false)
    .Satisfy(s => s.Width == 50 && s.Height == 100);

ResizeType.FixedHeight.CalcNewSize(_50_100, new Size(0, 50), false)
    .Satisfy(s => s.Width == 25 && s.Height == 50);

* The Satisfy-Method is from SharpTestEx.

Code

Have fun! 🙂

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace lcorneliussen
{
    public interface IResizeType
    {
        Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller);
    }

    /// <summary>
    /// Offers some helpful methods for image resizing.
    /// </summary>
    public class ResizeType
    {
        /// <summary>
        /// Returns the new size by fixed height.
        /// </summary>
        public static IResizeType FixedHeight = new FixedHeightResizeType();

        /// <summary>
        /// Returns the new size by fixed width.
        /// </summary>
        public static IResizeType FixedWidth = new FixedWidthResizeType();

        /// <summary>
        /// Returns a size wich fits in a target size.
        /// </summary>
        public static IResizeType Fit = new FitResizeType();

        /// <summary>
        /// Stretches a image to the given size.
        /// </summary>
        public static IResizeType Stretched = new StretchedResizeType();

        private static int CalcNewHeight(int currentWidth, int currentHeight, int newWidth)
        {
            float ratio = (float)newWidth / (float)currentWidth;
            float newHeight = currentHeight * ratio;

            return (int)newHeight;
        }

        private static int CalcNewWidth(int currentWidth, int currentHeight, int newHeight)
        {
            float ratio = (float)newHeight / (float)currentHeight;
            float newWidth = currentWidth * ratio;

            return (int)newWidth;
        }

        private class FixedHeightResizeType : IResizeType
        {
            public Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller)
            {
                int height = newSize.Height;

                if (!resizeIfSmaller)
                {
                    if (height > currentSize.Height) height = currentSize.Height;
                }

                int newWidth = CalcNewWidth(currentSize.Width, currentSize.Height, height);
                return new Size(newWidth, height);
            }
        }

        private class FixedWidthResizeType : IResizeType
        {
            public Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller)
            {
                int width = newSize.Width;
                
                if (!resizeIfSmaller)
                {
                    if (width > currentSize.Width) width = currentSize.Width;
                }

                int newHeight = CalcNewHeight(currentSize.Width, currentSize.Height, width);
                return new Size(width, newHeight);
            }
        }

        private class FitResizeType : IResizeType
        {
            public Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller)
            {
                int width = newSize.Width;
                int height = newSize.Height;

                int newWidth = currentSize.Width;
                int newHeight = currentSize.Height;

                if (!resizeIfSmaller) // break, if newSize allready fits in currentSize
                    if (currentSize.Height < height && currentSize.Width < width)
                        return new Size(currentSize.Width, currentSize.Height);

                if(width != newWidth) // shrink or expand to width
                {
                    newHeight = CalcNewHeight(currentSize.Width, currentSize.Height, width);
                    newWidth = width;
                }
                if (newHeight > height) //shrink to fit height, if neccesarry
                {
                    newWidth = CalcNewWidth(newWidth, newHeight, height);
                    newHeight = height;
                }

                return new Size(newWidth, newHeight);
            }
        }

        private class StretchedResizeType : IResizeType
        {
            public Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller)
            {
                int w = newSize.Width;
                int h = newSize.Height;

                if (!resizeIfSmaller)
                {
                    if (w > currentSize.Width) w = currentSize.Width;
                    if (h > currentSize.Height) h = currentSize.Height;
                }

                return new Size(w, h);
            }
        }
    }
}

* this code on gist

Turtles are Reptiles, right?

While writing an article about co- and contravariance in .NET I stumbled over some funny C# behaviors.

Given a simple domain model of animals (Reptile is base of Snake and Turtle), will this actually work?

And if not, what will happen? Will it even compile?

Reptile[] reptiles = new[]
  {
    new Snake("Leo"), 
    new Snake("Lucy")
  };

reptiles[0] = new Turtle("Platschi");

Quite obvious, still odd. 🙂

GREAT! Thanks for the answers!

I think @thinkbeforecoding had the best answer. Justin Chase, Björn Rochel and Stefan Lieser also got it right.

Here is what happens:

  1. It compiles!
  2. The type inference will infer a Snake[] from new[] because all elements are Snake.
  3. The Snake array will then be implicitly casted to the Reptile array reptiles because C# supports covariance for Array element types.
  4. Line 7 also compiles fine. But the covariance support is unsafe, hence read-only. The Snake-Array behind the scenes can’t handle Turtles.
  5. A ArrayTypeMismatchException is thrown at runtime.

In C# 4.0 there will not be safe variance for Arrays either. There can never be safe co- and contra-variance for read-write-interfaces or types, because you need co-variance for reading (out) and contra-variance for writing (in).

PDC 2009 – .NET Developers Short Review and Bleeding Edge Download Links

This years PDC was great. I think the big deal this year was “Integration”. I’ve never seen so many bits working together at such an early state.

I’ll walk through what I’ve seen and provide some detailed information plus the corresponding downloads I’ve found. It is insane, how many CTPs and Betas Microsoft offers right now.

These are the topics I will cover. Click to jump to the corresponding section.

Visual Studio 2010 + .NET Framework 4

I’ll start with Visual Studio, because it spans all of it.

In the last years especially Visual Studio was lagging behind. It didn’t even have good support for debugging LINQ in C# – what could we then expect from integration with other products from Microsoft.

But now Visual Studio comes not only with an improved debugger, but also with many, many new project templates, enhanced deployment features for web and SharePoint, an extension manager bound to an online extension gallery, and many-many more features.

Maybe the reason for the more integrated development environment is the huge improvement in Visual Studio Extensibility. Download the SDK if you plan to write your own plugins.

If you want to know more about the “.NET Framework 4.0”, Google has a good coverage on “C# 4.0”, “Windows Workflow 4.0”, “Windows Communication Foundation 4.0”, “Entity Framework 4.0” …

Downloads

Silverlight 4

Maybe the announcements around Silverlight 4 were the biggest. The Silverlight team has made incredible progress in the last couple years. With Silverlight 4 they seem to eliminate most of the drawbacks it had compared to WPF. I can’t see yet where this goes, but a Silverlight OS could absolutely be a nice answer to the Chrome OS. And then we are back to terminals and mainframes.

Among the new features are a better out-of-the-browser experience including full-trust mode. Also new is webcam and microphone access, a richtext/html editor and a browser control. The browser control can also embed flash. And it can be used as a brush, in other words, it lets you run YouTube videos while applying any Silverlight effects.

Downloads

Entity Framework 4

After the entity framework came out in V1, many of the industry’s thought leaders called it a joke. The team has tried to incorporate most of that Feedback in EF V2 (Called V4 to align with .NET 4).

Together with the .NET Framework 4 Beta 2 and the Futures CTP you get code-only and model-first including SQL schema generation. There are also alot improvements in the designer and the query performance. Haven’t really checked out the details so far.

Downloads

OData / WCF Data Services (neé “Astoria”)

The Open Data Protocol (OData) has been developed to support a convention-oriented REST-style API over data of any kind. It builds on atom/pub and adds optional metadata, querying and projections. Using the .NET OData Client (WCF Data Services) you can use LINQ against any OData source. Sharepoint 2010 will expose all data via OData. Any application that uses the Entity Framework can very simply expose it’s data over the protocol.

Also SQL Server Excel PoverPivot (neé Gemini) supports OData and lets you interact with huge amounts of data the Excel-way.

Downloads

SQL Server Modelling (neé “Oslo”)

In a couple of sessions Doug, Chris, Don and Chris showed the new features in the November CTP. The team has made some progress, although it is easy to see, that they have much homework left. Some of the new features are the better Visual Studio Integration, a ASP.NET MVC project supporting “M” for the M in MVC, debugging support in DSLs, richer right-hand grammar productions, support for views in “Quadrant”. Also the Modeling Services where officially announced to be part of SQL Server in some future release.

I’ll spend a new post on more of the changes soon.

Also, today I think, that Quadrant will not replace the DSL Tools in Visual Studio. Those have made much progress and will rather be improved and merged with “M”.

Downloads

Office and Sharepoint 2010

No matte who I asked, nobody liked SharePoint development so far. From what I’ve seen this might change in the future. There has been much focus on the developer experience for SharePoint customization. Deployment and packaging seems to be much more fluent, and the horrible SharePoint web service interfaces will be superseded by the new generic OData implementation over lists and libraries. There is also better support for embedding Silverlight, and Microsoft finally promises a good user experience even when using Firefox or Safari.

I just like Office 2010, but I haven’t done any customization for Office since the good old VBA times. I think the web-versions of Office built into SharePoint are really cool. They allow collaborative editing like in Google Docs. I’ll check out this stuff soon!

Downloads

SQL Server 2008 R2

In the last couple of weeks I had to do a lot with SQL Server. The thing is, still devs do not care much about new SQL Server versions. They do rather talk about persistence ignorance. Even the Xml/XQuery implementations well the hosting of the CLR inside the SQL Engine did not really touch the developers hearts.

But often the heart of the application is data. And the more you have to do with it with fast response times, the closer you’ll have to operate on it. Another argument is, that Microsoft emerges the brand “SQL” from a relational RDBMS engine to a data platform including Analysis Services, Reporting, Integration Services, and so on …

Some of the new features are StreamInsight, the Excel PoverPivot plugin and more…

Downloads

AppFabric (neé “Dublin” and “Velocity”)

“Dublin” was Microsofts promise to deliver a more manageable Application Server for .NET, WF and WCF solutions. But since it was announced there were no public CTPs or anything that we could have played around with.

At the same time “Velocity”, a memory cache, has had more publicity.

Those two are now merged in the AppFabric, promising a reliable host for the workflow foundation runtime and hopefully in the future a good replacement for NT services.

There have been rumors, that it enters the space of EAI, that BizTalk tried to cover so far. But the promise is, that they have different intents and will play nicely together.

Downloads

Windows Azure

I’m not into it yet. But it looks promising. Let me just provide the links to the downloads I found.

Downloads

.NET Service Bus

The .NET Service Bus is really interesting stuff. Somebody explained it as “Skype knowledge in a box”. This means, the .NET Service Bus helps you to set up secure duplex communication channels through firewalls and routers. It just leads your calls through the NAT jungle.

What you simply do, is creating a public namespace on “the internet”, where you then register your WCF service. Clients may then access your service through the Azure Cloud. Bye, bye DMZ!!

You can also use it to negotiate the connection details and then have it set up a direct TCP/IP connection between your server and the calling client.

In both modes, you’ll also be able to send messages to your clients, through all this expensive infrastructure that is intended to avoid those scenarios 🙂

The .NET Service Bus is part of the .NET Services SDK which also contains the Access Control Service and Workflow Services in the cloud.

Downloads

Windows Identity Foundation (neé “Geneva”)

Federated claims-based security. Everybody is talking about it. After Microsoft tried to reach through with Card Space and Live ID, this finally seems to be their answer to OpenID.

I’ll definitely take some time digging into it. I’m also interested in how it interacts with the SQL Server Modeling Services for System.Identity.

Downloads

    Windows 7

    Windows 7 is great! What I’m interested in most, is multi-touch. I think this will be the default for every monitor and notebook in just a couple of years.

    If you find any nice multitouch enabled applications, please give me a hint at http://multitouch-apps.com.

    Downloads

    Have fun!