New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Lifestyle.Length public. #245

Closed
dotnetjunkie opened this Issue Jun 5, 2016 · 0 comments

Comments

1 participant
@dotnetjunkie
Collaborator

dotnetjunkie commented Jun 5, 2016

The Lifestyle.Length property is currently defined as protected, but this makes it harder for custom Lifestyle implementations to reuse the length of existing Lifestyle implementations, without having to revert to magic numbers.

Examples:

public class DisposableTransientLifestyle : Lifestyle {
    public DisposableTransientLifestyle() : base("Disposable Transient") { }
    protected override int Length => 1; // Same length as Transient.
    ...
}

public class PerGraphLifestyle : ScopedLifestyle {
    public PerGraphLifestyle() : base("Per Graph", disposeInstances: false) { }
    protected override int Length => 2; // one above transient.
    ...
}

public class ThreadLifestyle : Lifestyle {
    public ThreadLifestyle() : base("Thread") { }
    protected override int Length => 999; // smaller than Singleton.
    ...
}

These custom implementations have to know what the value is of the built-in lifestyles (1 for Transient, 1000 for Singleton, 500 for Scoped). Instead, it would be much nicer if we were able to do this:

``` c#
public class DisposableTransientLifestyle : Lifestyle {
    public DisposableTransientLifestyle() : base("Disposable Transient") { }
    protected override int Length => Lifestyle.Transient.Length;
    ...
}

public class PerGraphLifestyle : ScopedLifestyle {
    public PerGraphLifestyle() : base("Per Graph", disposeInstances: false) { }
    protected override int Length => Lifestyle.Transient.Length + 1;
    ...
}

public class ThreadLifestyle : Lifestyle {
    public ThreadLifestyle() : base("Thread") { }
    protected override int Length => Lifestyle.Singleton.Length - 1;
    ...
}

For this to work, the only thing that has to be changed is making the Lifestyle.Length property public instead of protected.

Although this change won't effect many users (since it's quite rare for users to implement a custom lifestyle), it's still both a code and binary breaking change that can especially be annoying if users are depending on NuGet packages that implement a custom lifestyle).

@dotnetjunkie dotnetjunkie added this to the v4 milestone Jun 5, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment