Skip to content

dvabuzyarov/Autofac.Extras.RegistrationAttributes

Repository files navigation

Autofac.Extras.Attribute

Component registering into Autocaf container using attributes

Simple steps

  1. Mark implementation with attribute
    [As(typeof (INowProvider))]
    [SingleInstance]
    class NowProvider : INowProvider
    {
        public DateTime UtcNow()
        {
            return DateTime.UtcNow;
        }
    }
  1. Create assembly registration
    
    namespace AssemblyNameSpace
    {
      public class AutoRegistrationModule : Module
      {
          protected override void Load(ContainerBuilder builder)
          {
              base.Load(builder);
              builder.AutoRegistration(GetType().Assembly);
          }
      }
  1. Call all assemblies specific Ninject modules in the main assembly
    using Autofac.Extras.RegistrationAttributes.RegistrationAttributes;
    using Autofac.Modules;
    
    namespace MainAssemblyNameSpace
    {
        public class AutoRegistrationModule : Module
        {
            protected override void Load(ContainerBuilder builder)
            {
                base.Load(builder);
                builder.AutoRegistration(GetType().Assembly);
                //---either---
                builder.AutoRegistration(typeof(TypeFromOtherAssembly).Assembly);
                //---or---
                builder.RegisterModule<OtherAssemblyNamespace.AutoRegistrationModule>();
            }
        }
    }

Supported attributes

  1. AsAttribute - The implementation class will be registred as specified type
    [As(typeof (ITemplateUserProvider))]
    class ExampleClass : ITemplateUserProvider
    {
        private readonly INowProvider _nowProvider;
    
        public ExampleClass(INowProvider nowProvider) 
        {
            _nowProvider = nowProvider;
        }
    
        public string CurrentDateTimeAsString()
        {
            _nowProvider.UtcNow().ToString();
        }
    }
  1. SingleInstanceAttribute - The single instance of the implementation class will be shared across all consumers
  2. NamedAttribute - It creates a named registration
    [Named(typeof(IDownloadParams), "Download1")]
    class DownloadParams : IDownloadParams
    {
        public string PageUrl { get; private set; }

        public DownloadParams()
        {
            PageUrl = "http://google.com";
        }
    }
    
    
    [As(typeof (IDownloadSetvice))]
    [SingleInstance]
    class DownloadSetvice : IDownloadSetvice
    {
        private readonly IContainer _container;

        public DownloadParamsFactory(IContainer container)
        {
            _container = container;
        }

        public string Get(string paramsName)
        {
            return _container.ResolveNamed<IDownloadParams>('Download1');
            // --- some action here
        }
    }
  1. ExternallyOwnedAttribute - setup a registration as externally owned.
  2. AsCustomAtrribute - you can specify your custom registration, here is an expample how I am using it
    [As(typeof(ISomeService))]
    [SingleInstance]
    [AsCustom(typeof (DebugRegistration))]
    class SomeServiceFake : ISomeService
    {
        public SomeServiecFake()
        {
        }

        public IEnumerable<string> Payload()
        {
            yield return "concept 1";
            yield return "concept 2";
            yield return "concept 3";
        }
    }


    [As(typeof(ISomeService))]
    [SingleInstance]
    [AsCustom(typeof(ProductionRegistration))]
    [AsCustom(typeof(DevelopmentRegistration))]
    [AsCustom(typeof(ConsolidationRegistration))]
    class SomeService : ISomeService
    {
        public SomeServiec()
        {
        }

        public IEnumerable<string> Payload()
        {
            //--- read data from remote source
        }
    }
    
  So if DEBUG constans is defined the first implementation will be resolved from the DI.
  If DEBUG constant is not defined then the first implementation will not be registred.
  
  You just need to implement IAutofacCustomRegistration interface in your custom registration.

About

Autofac extras library for component registration via attributes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published