question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto add "Default" interface implementation

See original GitHub issue

Hi,

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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
khellangcommented, Sep 4, 2016

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

services.Scan(scan => scan
    .FromAssemblies(assemblies)
    .AddClasses(classes => classes.AssignableTo<ICommandApi>())
    .AsImplementedInterfaces()
    .WithScopedLifetime());

??

0reactions
khellangcommented, Sep 5, 2016

Did I already mention how awesome Scrutor is 😃 Why does this work out of the box, unbelievable!

Thanks

I’m guessing this means I can close this issue? 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

interface methods and default values for Auto Properties - ...
No and Yes. An interface can't have state but you can define a property with {get;set;} . Auto properties aren't a special type...
Read more >
Default interface methods - C# 8.0 draft feature specifications
Default interface methods enable an API author to add methods to an interface in future versions without breaking source or binary compatibility ...
Read more >
C# 8 Interfaces: Properties and Default Implementation
In taking a closer look at C# interfaces, we'll start by exploring default implementation and how it relates to properties.
Read more >
Safely update interfaces using default interface methods in C
This advanced tutorial explores how you can safely add new capabilities to existing interface definitions without breaking all classes and ...
Read more >
Static and Default Methods in Interfaces in Java
Default interface methods are an efficient way to deal with this issue. They allow us to add new methods to an interface that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found