simpleinjector / SimpleInjector

An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
https://simpleinjector.org
MIT License
1.21k stars 155 forks source link

Add debug information for Scopes #993

Closed dotnetjunkie closed 3 weeks ago

dotnetjunkie commented 6 months ago

Currently, it's hard to see if multiple Scope references point to the same scope, or to different instances. It would, therefore, be useful, for debugging purposes to see a unique instance number, just like Container instances already have an unique internal id. For instance something like:

private readonly long id;

public override ToString()
{
    if (this.ParentScope is null && this.Container is null)
    {
         return "Scope #" + this.id;
    }

    if (this.ParentScope is null)
    {
         return "Scope #" + this.id + " for Container #" + this.Container.Id;
    }

    return "Scope #" + this.id + " for Parent #" + this.ParentScope.Id + 
        " for Container #" + this.Container.Id;
}
wleader commented 5 months ago

I like this, but you may want to reserve overriding .ToString() for something that is an actual string representation of the object (although I am having trouble imagining what that would be.) A slightly more formal way of providing this kind of debug time information is to use a DebuggerDisplay attribute.

https://learn.microsoft.com/en-us/visualstudio/debugger/using-the-debuggerdisplay-attribute?view=vs-2022

dotnetjunkie commented 5 months ago

As a matter of fact, Simple Injector already uses [DebuggerDisplay] extensively. But I do agree with you, that a DebuggerDisplayAttribute would be a better fit here.