Use Roslyn to improve the experience for Simple Injector users

Apr 18, 2014 at 9:24 AM
Roslyn is coming and and it will be a game changer, without any doubt. I like to brainstorm with you whether Roslyn could bring anything interesting for Simple Injector and its users. For instance, Simple Injector currently contains the Diagnostic Services that allow verifying the configuration and during runtime (both while debugging and inside unit tests). But would it be possible to integrate this with Roslyn to do this analysis earlier?

And perhaps there are other kind of DI or Simple Injector related validations and refactorings that can be written on top of the Roslyn API that would help Simple Injector users. I like you to post anything you can think of that might be useful that might be doable with Roslyn.
May 30, 2014 at 10:01 AM
While making registrations using the generic overloads of the Register method type checking is automatically done. With Roslyn you could implement this feature also for the non-generic overloads of the Register method. Consider the following code, which currently compiles and offcourse breaks on calling the Verify method.
    public interface ISomeService { }
    public interface ISomeOtherService { }
    public class SomeService : ISomeService { }
    public class SomeOtherService : ISomeOtherService { }

    container.Register(typeof(ISomeService), typeof(SomeOtherService));
It is allready very nice this breaks before execution when the Verify method is used, but with Roslyn we could know this while typing our code and you can supply us with some nice comment or question.
May 30, 2014 at 10:36 AM
Edited May 30, 2014 at 10:58 AM
That's great, but such check would in fact just be the beginning. With Roslyn, we might be able to add the following compile-time checks:
  • Verify whether a supplied type can be applied as decorator.
  • Verify whether a supplied type can be applied as open-generic type.
  • Verify whether a supplied type will be used, or that the registration is useless.
But do note that although the check for Register(typeof(ISomeService), typeof(SomeOtherService)) could be easily made, it becomes pretty hard soon after that, for instance, what about this:
  • container.Register(typeof(ISomeService), GetService())
  • listOfTypes.ForEach(pair => container.Register(pair.Service, pair.Implementation))