The SimpleInjector verification feature from time to time has highlighted issues that I have needed to fix. I do however have a couple of "potential" issues that I am comfortable with and I would like some way of telling SimpleInjector that these are ok, just tell me about the rest ...
- Configuration Warnings Warnings in multiple groups have been detected.
- Potential Lifestyle Mismatches 1 possible mismatch for 1 service.
IObjectMaterializedSubscriber ObjectMaterializedSubscriber
(Lifetime Scope) depends onIEventPublisher
(Transient).- Potential Single Responsibility Violations 2 possible violations.
ILetterGenerator<A>
LetterGenerator<A>
has 9 dependencies which might indicate a SRP violation.ILetterGenerator<B>
LetterGenerator<B>
has 9 dependencies which might indicate a SRP violation.
The first warning IEventPublisher
as transient is fine.
The other two warnings (I am assuming) are caused by me using the SimpleInjector Decorator facility to build Chains of Responsibility.
I'd like to be able to mark these specific warnings as accepted so that the Container can report everything appears to be ok!
Is this possible?
I'd like to be able to mark these specific warnings as accepted [...] Is this possible?
No, there is no way to do that with the current (2.2.3) release. The whole diagnostic API is internal. It is possible to trigger the diagnostic validation using reflection in a unit test and filter out the warnings you're not interested in. If there are any warnings left, you can let the test fail.
This is possible, but does require some work, and in most cases you wouldn't want to have any Potential Lifestyle Mismatch violations. The documentation about the Potential Lifestyle Mismatches states:
Do not ignore these warnings. False positives for this warning are rare and even when they occur, the registration or the application design can always be changed in a way that the warning disappears.
In your case your Lifetime Scoped instance refers to a transient and you seem to be aware of the possible risk of this and you seem to have checked that this is not problem. Still, this could become a problem in the future when the class is changed, and other developers could be mislead if you don't document this explicitly.
There are often multiple ways to fix this. For instance, you can promote the transient dependency to lifetime scope, but you have probably already considered this. Another option is to let the ObjectMaterializedSubscriber
not depend on a transient IEventPublisher
, but on a singleton Func<IEventPublisher>
, which can be registered as follows:
container.RegisterSingle<Func<IEventPublisher>>(
() => container.GetInstance<IEventPublisher>());
This makes the code of the ObjectMaterializedSubscriber
communicate clearly that it expects to get an object with a shorter lifestyle, even if that IEventPublisher
is requested once.
The other two warnings (I am assuming) are caused by me using the SimpleInjector Decorator facility to build Chains of Responsibility.
Unfortunately, you stumbled upon a bug in the 2.2.1 release. The problem wasn't caused by adding decorators, but by the use of unregistered type resolution. This bug has been fixed in 2.2.3.
UPDATE
Since Simple Injector 2.4 there's an Diagnostics API that allows you to interact programmatically with the Diagnostic Services. This allows you to write unit tests and filter out any unwanted warnings before letting the unit test verify if you are free of any warnings. Example:
[TestMethod]
public void Container_Always_ContainsNoDiagnosticWarnings()
{
// Arrange
var container = Bootstrapper.GetInitializedContainer();
container.Verify();
// Assert
var results = Analyzer.Analyze(container);
Assert.IsFalse(results.Any(), Environment.NewLine +
string.Join(Environment.NewLine,
from result in results
select result.Description));
}