0

is there any way to register types (i.e. repositories) for a mvc 6 (vnext) application within the dnx class libraries? I don't want to call AddTransient<,>() within the startup class hundreds of times. For a n-tier application this seems not to be the best approach.

endeffects
  • 431
  • 4
  • 15

2 Answers2

2

Basic IoC vNext use doesn't have scanning abilities/batch registration.

In your case you should use some of "big" ioc containers like Ninject, Unity, StructrureMap...

Marko M.
  • 789
  • 3
  • 9
  • 1
    Well, i tried this too, but i failed to load the dependencies from the ILibraryManager into the ContainerBuilder because the Container has to be build before the LibraryManager is initialized, – endeffects Jul 27 '15 at 13:57
2

You class libraries you can follow the same approach the rest of the framework uses which uses extension methods on IServiceCollection. eg. AddMvc, AddEntityFramework, etc.

For "automatic" registration, nothing exists out of the box, but I've built a library that gives you a [ServiceDescriptor] attribute, that you can use to bind your interfaces to your classes (or just apply to a class).

https://github.com/Blacklite/DependencyInjection/tree/master/src/Blacklite.Framework.DependencyInjection

The AddAssembly(...) extension method just assembly surfs to find all your [ServiceDescriptor] attributes and adds them to the services collection.

Then to use it you just need to do one of the following:

  1. Add the extension method like above, and have the extenstion method simply call services.AddAssembly(typeof(ExtensionClassInAssembly))

    I find this the simplist, also it allows you to remove the AddAssembly method and replace it with something else without having to change your main startup class.

  2. Add a call to services.AddAssembly(typeof(TypeInAssembly)) to your Startup.cs

Community
  • 1
  • 1
David Driscoll
  • 1,399
  • 11
  • 13
  • Nice idea, but it looks a little bit hacky. :D I would like to reuse the build in services. I have provided a not working example here: http://stackoverflow.com/questions/31674104/self-registering-libraries-with-autofac-4-and-vnext – endeffects Jul 28 '15 at 11:51