Why is a type registered twice when lifetime manager is specified? : <p>I'm using Unity's <em>Register by convention</em> mechanism in the following scenario:</p>

<pre><code>public interface IInterface { }

public class Implementation : IInterface { }
</code></pre>

<p>Given <code>Implementation</code> class and its interface I'm running <code>RegisterTypes</code> in the following way:</p>

<pre><code>unityContainer.RegisterTypes(
    new[] { typeof(Implementation) },
    WithMappings.FromAllInterfaces,
    WithName.Default,
    WithLifetime.ContainerControlled);
</code></pre>

<p>After this call, <code>unitContainer</code> contains three registrations:</p>

<ul>
<li><code>IUnityContainer</code> -> <code>IUnityContainer</code>  (ok)</li>
<li><code>IInterface</code> -> <code>Implementation</code> (ok)</li>
<li><code>Implementation</code> -> <code>Implementation</code> (???)</li>
</ul>

<p>When I change the call as follows:</p>

<pre><code>unityContainer.RegisterTypes(
    new[] { typeof(Implementation) },
    WithMappings.FromAllInterfaces,
    WithName.Default);
</code></pre>

<p>The container contains only two registrations:</p>

<ul>
<li><code>IUnityContainer</code> -> <code>IUnityContainer</code> (ok)</li>
<li><code>IInterface</code> -> <code>Implementation</code> (ok)</li>
</ul>

<p>(this is the desired behaviour).</p>

<p>After peeking into <a href="https://github.com/unitycontainer/unity/blob/master/source/Unity/Src/RegistrationByConvention/UnityContainerRegistrationByConventionExtensions.cs">Unity's source code</a>, I've noticed that there is some misunderstanding about how <code>IUnityContainer.RegisterType</code> should work.</p>

<p>The <code>RegisterTypes</code> method works as follows (the comments indicate what are the values in the scenarios presented above):</p>

<pre><code>foreach (var type in types)
{
    var fromTypes = getFromTypes(type); // { IInterface }
    var name = getName(type);           // null
    var lifetimeManager = getLifetimeManager(type); // null or ContainerControlled
    var injectionMembers = getInjectionMembers(type).ToArray(); // null

    RegisterTypeMappings(container, overwriteExistingMappings, type, name, fromTypes, mappings);

    if (lifetimeManager != null || injectionMembers.Length &gt; 0)
    {
        container.RegisterType(type, name, lifetimeManager, injectionMembers);   // !
    }
}
</code></pre>

<p>Because <code>fromTypes</code> is not empty, the <code>RegisterTypeMappings</code> adds one type mapping: <code>IInterface</code> -> <code>Implementation</code> (correct).</p>

<p>Then, in case when <code>lifetimeManager</code> is not null, the code attempts to change the lifetime manager with the following call:</p>

<pre><code>container.RegisterType(type, name, lifetimeManager, injectionMembers);
</code></pre>

<p>This function's name is completely misleading, because <a href="https://msdn.microsoft.com/en-us/library/ee650597.aspx">the documentation</a> clearly states that:</p>

<blockquote>
  <p>RegisterType a LifetimeManager for the given type and name with the container. No type mapping is performed for this type.</p>
</blockquote>

<p>Unfortunately, not only the name is misleading but the documentation is wrong. When debugging this code, I've noticed, that when there is no mapping from <code>type</code> (<code>Implementation</code> in the scenarios presented above), it is added (as <code>type</code> -> <code>type</code>) and that's why we end up with three registrations in the first scenario.</p>

<p>I've downloaded Unity's sources to fix the problem, but I've found the following unit test:</p>

<pre><code>[TestMethod]
public void RegistersMappingAndImplementationTypeWithLifetimeAndMixedInjectionMembers()
{
    var container = new UnityContainer();
    container.RegisterTypes(new[] { typeof(MockLogger) }, getName: t =&gt; "name", getFromTypes: t =&gt; t.GetTypeInfo().ImplementedInterfaces, getLifetimeManager: t =&gt; new ContainerControlledLifetimeManager());

    var registrations = container.Registrations.Where(r =&gt; r.MappedToType == typeof(MockLogger)).ToArray();

    Assert.AreEqual(2, registrations.Length);

    // ...
</code></pre>

<p>- which is almost exactly my case, and leads to my question:</p>

<p>Why is this expected? Is it a conceptual mistake, a unit test created to match existing behaviour but not necessarily correct, or am I missing something important?</p>

<p>I'm using Unity v4.0.30319.</p>
