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
@dannief
Copy link

dannief commented Dec 8, 2015

I am enjoying the blog series so far. Please keep them coming.

Here are a few cool libraries that I use frequently that I think are worth mentioning:

ExpressiveAnnotations

Small .NET and JavaScript library which provides annotation-based conditional validation mechanisms

e.g.

[RequiredIf("Details.Email != null")]
[RequiredIf("Details.Phone != null")]
[AssertThat("AgreeToContact == true")]
public bool? AgreeToContact { get; set; }

Humaizer

Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities

e.g.

"PascalCaseInputStringIsTurnedIntoSentence".Humanize()
 // "Pascal case input string is turned into sentence"

GraphDiff

Entity Framework graph merge extension method http://blog.brentmckendrick.com/introducing-graphdiff-for-entity-framework-code-first-allowing-automated-updates-of-a-graph-of-detached-entities/

e.g.

using (var context = new TestDbContext())  
{
    // Update the company and state that the company 'owns' the collection Contacts.
    context.UpdateGraph(company, map => map
        .OwnedCollection(p => p.Contacts, with => with
            .AssociatedCollection(p => p.AdvertisementOptions))
        .OwnedCollection(p => p.Addresses)
    );

    context.SaveChanges();
}

@ivaylokenov
Copy link

Hi, guys, thanks for the blog posts. :)

I would like to share two of my open-source projects, I believe they are quite helpful:

MyTested.WebApi - fluent testing framework for ASP.NET Web API 2

AspNet.Mvc.TypedRouting - typed routing and link generation for ASP.NET MVC 6 (linked on the MVC repository's readme)

@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