Auto add "Default" interface implementation
See original GitHub issueHi,
I use Scrutor every day and I love it. Until now I used it in that style:
services.Scan(scan => scan
.FromAssemblies(assemblies)
.AddClasses(classes => classes.AssignableToAny(typeof(IHandleCommand<>), typeof(IHandleQuery<,>)))
.AsImplementedInterfaces()
.WithScopedLifetime());
And it worked like charme. But now I want to add a new scenario, maybe I overlooked it, but I think it’s currently not supported?! I think I saw what I want to do in some other DI container, maybe Ninject, but I’m not sure.
Let’s assume we have the following interfaces and classes:
public interface ITest1 : ICommandApi {}
public interface ITest2 : ICommandApi {}
public interface ITest3 : ICommandApi {}
public class Test1 : ITest1 {}
public class Test2 : ITest2 {}
public class Test3 : ITest3 {}
We all know what we would do:
services.AddScoped<ITest1, Test1>();
services.AddScoped<ITest2, Test2>();
services.AddScoped<ITest3, Test3>();
Issue is, let’s say we have 100 of this classes and every time you add new one you have not to forget to add it to the ServiceCollection. In my scenario, I can ensure that there is always just one class that inherits the corresponding interface.
I bet you can already imagine what I want to do 😃
Because the interfaces can be distributed all over the place and I don’t know where (Because it’s an framework used by multiple applications) I had to mark the interfaces somehow, that’s the only purpose of ICommandApi.
I want now with scrutor do something like:
services.Scan(scan => scan
.FromAssemblies(assemblies)
.AddClasses(classes => classes.ToDefaultImplementation().Where(interface => interface.InheritFrom<ICommandApi>()))
.AsImplementedInterfaces()
.WithScopedLifetime());
What does nothing else in the background than this:
services.AddScoped<ITest1, Test1>();
services.AddScoped<ITest2, Test2>();
services.AddScoped<ITest3, Test3>();
Do you think it’s possible?
Kind regards
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Oh, now I think I see what you’re trying to do. You just want to register all classes that is assignable to the marker interface,
ICommandApi
?Can’t you just do
??
I’m guessing this means I can close this issue? 😄