Skip to content

Instantly share code, notes, and snippets.

@bleroy
Last active December 14, 2015 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bleroy/857ffb9796295606734e to your computer and use it in GitHub Desktop.
Save bleroy/857ffb9796295606734e to your computer and use it in GitHub Desktop.
Tips for the Week in .NET
@dadhi
Copy link

dadhi commented Dec 9, 2015

Hello @bleroy,

Packages I want more people to know:

BenchmarkDotNet

Performance optimization is hard but micro-optimizations are harder. So we need a reliable benchmarks to see the difference, not for 1 000 000 times loop. Especially when we target different platforms and architectures, and jitters.

BenchmarkDotNet is initially developed by @AndreyAkinshin, then with help of @mattwarren.

The project allows you to focus on What to benchmark, instead of How. And provides results in a way you can put on presentation slides for your management (who is on the tech side though ;).

DryIoc

disclosure: I am an Owner.

Top performer in IoC for .NET at the moment.

The version 2.0 was in a preview for almost a year and released in November. V2.0 adds to performance the full spec of features that you'd expect from modern IoC.

It was initially designed to be on par with Autofac, StructureMap, Unity. But provide faster resolutions (even for the first time) and faster bootstrap/registrations as well. So you do not hesitate to use it.

Plus:

  • The support for MEF Attributed Model was here from the start.
  • DryIoc was designed to be unobtrusive for easy integration with existing solutions and libraries.
  • The last but not least the goal was to minimize noise associated with container usage.

An elaborated example (full code):

    [Test]
    public void Test()
    {
        var container = new Container().WithMefAttributedModel();
        container.RegisterExports(typeof(Foo<,>), typeof(FooDecorator), typeof(X), typeof(Y));
        var foo = container.Resolve<IFoo<X, Y>>();
        StringAssert.Contains("decorated", foo.Message);
    }

    // The SUT
    public interface IFoo<A, B>
    {
        string Message { get; set; }
    }

    [Export]
    public class X { }

    [Export]
    public class Y { }

    [Export(typeof(IFoo<,>))]
    class Foo<A, B> : IFoo<A, B>
    {
        public Foo(A a, B b) {}

        public string Message { get; set; }
    }

    // AsFactory says that class has factory method(s) to Export 
    [Export, AsFactory]
    public class FooDecorator
    {
        // Decorator or kind of instantiation middleware implemented as normal method
        // Method parameters are injected by container. Lazy, Func, etc are supported
        // AsDecorator instructs to use result of this method for IFoo<> service, decoratee foo is injected by container. 
        [Export, AsDecorator]
        public IFoo<A, B> AddMessage<A, B>(IFoo<A, B> foo, Func<A> a, Lazy<B> b)
        {
            foo.Message = $"decorated with {a()} and {b.Value}";
            return foo;
        }
    }

Note: That the same setup may be done without attributes or MEF model. It illustrates how easy it is to work with generics, decorators, wrappers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment