<commit_before>====================
Registration Sources
====================

A *registration source* is a way to dynamically feed registrations into an Autofac component context (e.g., container or lifetime scope).

Registration sources are created by implementing the ``IRegistrationSource`` interface. Many of the Autofac features are implemented this way - for example, the :doc:`implicit relationship types <../resolve/relationships>` are added via registration sources. (You didn't think we actually registered every single type of collection manually into the container, did you?) `<NAME> has a great blog article about how this works. <http://nblumhardt.com/2010/01/declarative-context-adapters-autofac2/>`_

Registration sources are great when you don't have a finite set of registrations you can add to a container. Many times, :doc:`assembly scanning <../register/scanning>` and/or :doc:`use of modules <../configuration/modules>` can address dynamic registration concerns... but when all else fails or those means don't accomplish what you're looking to do, a registration source may be the way to go.

"Any Concrete Type Not Already Registered" Source
=================================================
The ``AnyConcreteTypeNotAlreadyRegisteredSource``, or "ACTNARS" as we call it, is an example of a registration source that Autofac ships with that allows you to resolve any concrete type from the Autofac container regardless of whether or not you specifically registered it. People moving from other inversion-of-control containers may be used to this sort of behavior, so ACTNARS is a way to bridge that gap.

You can use the ``Autofac.Features.ResolveAnything.AnyConcreteTypeNotAlreadyRegisteredSource`` by adding it to your container.

.. sourcecode:: csharp

    var builder = new ContainerBuilder();
    builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
    var container = builder.Build();

Contravariant Registration Source
=================================
The ``ContravariantRegistrationSource`` is helpful in registering types that need to be later resolved in a `contravariant context <https://docs.microsoft.com/en-us/dotnet/standard/generics/covariance-and-contravariance>`_ (using a more generic / less derived type than originally specified). This is common in handler patterns:

.. sourcecode:: csharp

    public interface IHandler<in TCommand>
    {
      void Handle(TCommand command);
    }

    public class BaseCommandHandler : IHandler<BaseCommand>
    {
      public void Handle(BaseCommand command)
      {
         Console.WriteLine(command.GetType().Name);
      }
    }

    public class BaseCommand
    {
    }

    public class DerivedCommand : BaseCommand
    {
    }

    var builder = new ContainerBuilder();
    builder.RegisterSource(new ContravariantRegistrationSource());
    builder.RegisterType<BaseCommandHandler>().As<IHandler<BaseCommand>>();
    var container = builder.Build();

    // a and b are both BaseCommandHandler.
    var a = container.Resolve<IHandler<BaseCommand>>();
    a.Handle(new BaseCommand()); // prints BaseCommand
    var b = container.Resolve<IHandler<DerivedCommand>>();
    b.Handle(new DerivedCommand()); // prints DerivedCommand


Implementing a Registration Source
==================================

The best way to show how to implement a registration source is through a simple example.

Let's say you have a factory that is responsible for generating some sort of event handler class. You need to generate them through the factory rather than through Autofac, so you don't have the handlers themselves registered with Autofac. At the same time, there's not a good way to say "when a person asks for any event handler, generate it through this special factory." This is a great example of where a registration source can come in handy.<commit_msg>Register ContravariantRegistrationSource first.<commit_after>====================
Registration Sources
====================

A *registration source* is a way to dynamically feed registrations into an Autofac component context (e.g., container or lifetime scope).

Registration sources are created by implementing the ``IRegistrationSource`` interface. Many of the Autofac features are implemented this way - for example, the :doc:`implicit relationship types <../resolve/relationships>` are added via registration sources. (You didn't think we actually registered every single type of collection manually into the container, did you?) `<NAME> has a great blog article about how this works. <http://nblumhardt.com/2010/01/declarative-context-adapters-autofac2/>`_

Registration sources are great when you don't have a finite set of registrations you can add to a container. Many times, :doc:`assembly scanning <../register/scanning>` and/or :doc:`use of modules <../configuration/modules>` can address dynamic registration concerns... but when all else fails or those means don't accomplish what you're looking to do, a registration source may be the way to go.

"Any Concrete Type Not Already Registered" Source
=================================================
The ``AnyConcreteTypeNotAlreadyRegisteredSource``, or "ACTNARS" as we call it, is an example of a registration source that Autofac ships with that allows you to resolve any concrete type from the Autofac container regardless of whether or not you specifically registered it. People moving from other inversion-of-control containers may be used to this sort of behavior, so ACTNARS is a way to bridge that gap.

You can use the ``Autofac.Features.ResolveAnything.AnyConcreteTypeNotAlreadyRegisteredSource`` by adding it to your container.

.. sourcecode:: csharp

    var builder = new ContainerBuilder();
    builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
    var container = builder.Build();

Contravariant Registration Source
=================================
The ``ContravariantRegistrationSource`` is helpful in registering types that need to be later resolved in a `contravariant context <https://docs.microsoft.com/en-us/dotnet/standard/generics/covariance-and-contravariance>`_ (using a more generic / less derived type than originally specified).

**If you use this source, register it before registering other things.** Just as with standard registrations, registration sources are evaluated in the order of registration. If you register an open generic (which is *also* a registration source under the covers) and *then* register ``ContravariantRegistrationSource``, you will not get the outcome you expect.

Using ``ContravariantRegistrationSource`` is common in handler patterns:

.. sourcecode:: csharp

    public interface IHandler<in TCommand>
    {
      void Handle(TCommand command);
    }

    public class BaseCommandHandler : IHandler<BaseCommand>
    {
      public void Handle(BaseCommand command)
      {
         Console.WriteLine(command.GetType().Name);
      }
    }

    public class BaseCommand
    {
    }

    public class DerivedCommand : BaseCommand
    {
    }

    var builder = new ContainerBuilder();

    // Register this BEFORE registering other things!
    builder.RegisterSource(new ContravariantRegistrationSource());
    builder.RegisterType<BaseCommandHandler>().As<IHandler<BaseCommand>>();

    var container = builder.Build();

    // a and b are both BaseCommandHandler.
    var a = container.Resolve<IHandler<BaseCommand>>();
    a.Handle(new BaseCommand()); // prints BaseCommand
    var b = container.Resolve<IHandler<DerivedCommand>>();
    b.Handle(new DerivedCommand()); // prints DerivedCommand


Implementing a Registration Source
==================================

The best way to show how to implement a registration source is through a simple example.

Let's say you have a factory that is responsible for generating some sort of event handler class. You need to generate them through the factory rather than through Autofac, so you don't have the handlers themselves registered with Autofac. At the same time, there's not a good way to say "when a person asks for any event handler, generate it through this special factory." This is a great example of where a registration source can come in handy.
{'fraction_non_alphanumeric': 0.10525033499817274, 'fraction_numerical': 0.0017054452430259471, 'mean_word_length': 4.689535689535689, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7542767', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1879, 'n_words': 902}