Using a list to centrally manage type mappings?
<p>I have a bunch of type mappings I need to register in an Install method and remove in an Uninstall method. At the moment my code looks like this:</p>

<p>Install:</p>

<pre><code>var serviceLocatorConfig = new ServiceLocatorConfig();
serviceLocatorConfig.RegisterTypeMapping&lt;IListItemRepository, ListItemRepository&gt;();
serviceLocatorConfig.RegisterTypeMapping&lt;ITaskRepository, TaskRepository&gt;();
serviceLocatorConfig.RegisterTypeMapping&lt;IIssueRepository, IssueRepository&gt;();
...
</code></pre>

<p>Uninstall:</p>

<pre><code>var serviceLocatorConfig = new ServiceLocatorConfig();
serviceLocatorConfig.RemoveTypeMapping&lt;IListItemRepository&gt;(null);
serviceLocatorConfig.RemoveTypeMapping&lt;ITaskRepository&gt;(null);
serviceLocatorConfig.RemoveTypeMapping&lt;IIssueRepository&gt;(null);
...
</code></pre>

<p>and these continue for several more mappings.</p>

<p>The thing I don't like here is as I add a new repository I have to add a new line to both the install method and the uninstall method. What I want is something like this</p>

<pre><code>    private readonly Dictionary&lt;Type, Type&gt; _typeMappings = new Dictionary&lt;Type, Type&gt;
                                             {
                                                 {typeof(IListItemRepository), typeof(ListItemRepository)},
                                                 {typeof(ITaskRepository), typeof(TaskRepository)},
                                                 {typeof(IIssueRepository), typeof(IssueRepository)},
                                                 ...
                                             };
</code></pre>

<p>and then for my install and uninstall methods I could just iterate the collection...</p>

<pre><code>foreach (KeyValuePair&lt;Type, Type&gt; mapping in _typeMappings)
{
    serviceLocatorConfig.RegisterTypeMapping&lt;mapping.Key, mapping.Value&gt;();                
}
</code></pre>

<p>and</p>

<pre><code>foreach (KeyValuePair&lt;Type, Type&gt; mapping in _typeMappings)
{
    serviceLocatorConfig.RemoveTypeMapping&lt;mapping.Key&gt;(null);                
}
</code></pre>

<p>As I add more repositories I would only have the <code>_typeMappings</code> collection to update, and not have to worry about updating both methods.</p>

<p>Problem is, the <code>RegisterTypeMapping</code> method in the body of the foreach complain that it is expecting either the name of a namespace or type. I also tried</p>

<pre><code>serviceLocatorConfig.RegisterTypeMapping&lt;typeof(mapping.Key), typeof(mapping.Value)&gt;();
</code></pre>

<p>too but it didn't like that either.</p>

<p>Any ideas?</p>

<p>[EDIT] The <code>RegisterTypeMapping</code> method signature is defined below</p>

<pre><code>public void RegisterTypeMapping&lt;TFrom, TTo&gt;() where TTo : TFrom, new()
{
   ...
}
</code></pre>
