Dependency Injection Libraries


The decorator pattern provided by Helpful Libraries can be used to create wrappers around injected dependencies. This is useful if you want to intercept calls to dependencies without completely overriding them (for logging, profiling...). E.g. you can create an ISiteService implementation and make it wrap the already existing ISiteService implementation, and thus intercept every call to GetSiteSettings() as below:

    // Setting up the decorator.
    public class DecoratorsModule : DecoratorsModuleBase
        protected override IEnumerable<DecorationConfiguration> DescribeDecorators()
            // We could set up more than one decorator here.
            return new[]
                DecorationConfiguration.Create<ISiteService, SiteServiceDecorator>()

    // Should be internal so it's not picked up by Orchard automatically and so it doesn't override the existing implementation.
    internal class SiteServiceDecorator : ISiteService
        private readonly ISiteService _decorated;

        public ILogger Logger { get; set; }

        // Decorators are expected to inject at least their decorated dependency.
        public SiteServiceDecorator(ISiteService decorated)
            _decorated = decorated;

            Logger = NullLogger.Instance;
        public ISite GetSiteSettings()
            Logger.Information("ISiteService.GetSiteSettings called.");

            // Forwarding the call to the wrapped type.
            return _decorated.GetSiteSettings();


It consists solely of IResolve<T> and its implementation with a single property, T Value. The purpose is to inject IResolve through the constructor and use its Value instead of injecting a WorkContext and using its Resolve(), which is more or less hidden (calling Value is exactly the same as calling Resolve()). Thus by requesting an IResolve in the constructor dependencies can be lazy-loaded, but still they're visible by looking at the constructor signature.

IResolve works pretty much the same as Orchard's built-in Work class, but it gives a new instance on every call if the dependency is an ITransientDependency.

Last edited Mar 28, 2015 at 12:07 AM by Piedone, version 3


No comments yet.