I am using SimpleInjector for dependency injection and I've just discovered that I can register the same decorator multiple times within a single call chain and the decorator will be applied as per the requested configuration. For example I can insert my RepositoryTraceDecorator as the first and last decorator of my repository class:
container.RegisterDecorator(typeof(IRepository<>),
typeof(RepositoryTraceDecorator<>));
container.RegisterDecorator(typeof(IRepository<>),
typeof(RepositoryDetectChangesDecorator<>));
container.RegisterDecorator(typeof(IRepository<>),
typeof(CrossRepositoryTransactionScopeDecorator<>));
container.RegisterDecorator(typeof(IRepository<>),
typeof(RepositoryTraceDecorator<>));
container.RegisterDecorator(typeof(IRepository<>),
typeof(RepositoryExceptionDecorator<>));
Is this by design and therefore can I depend on this feature?
Is this by design and therefore can I depend on this feature?
As you just shown, it can be useful to apply decorators multiple times in the chain and changing this behavior in a future release would be a breaking change. I can ensure you that you can safely depend on this and this behavior won't change.
What might happen in the future is that the new Diagnostics feature gets improved to detect this and warns you about having multiple identical decorators.