HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

IoC component registration

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
componentiocregistration

Problem

How do you list your referenced solution assemblies for IoC registration? I was really tired of typing something strange as new [] { typeof(SomeType).Assembly, … }. What do you think about the following approach?

Use case:

class Program
{
    static void Main(string[] args)
    {
        foreach (var a in Solution.Assemblies)
            Console.WriteLine(a);            
    }
}


Where Solution.Assemblies is:

public static class Solution 
{
    static IEnumerable AssemblyNames => new[] {
        "ValueCache", "ValueCache.Tests", "LazyProxies", "ConsoleApplication1", "ClassLibrary1"
    };

    public static IEnumerable Assemblies => AssemblyNames
        .Select(an => Load(an))
        .Where(a => a != null);

    [DebuggerHidden]
    static Assembly Load(string assemblyName)
    {
        try
        {
            return Assembly.Load(new AssemblyName(assemblyName));
        }
        catch
        {
            return null;
        }
    }
}


… being generated by Solution.tt file:

```





using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Diagnostics;

namespace SolutionConfiguration
{
public static class Solution
{
static IEnumerable AssemblyNames => new[] {

};

public static IEnumerable Assemblies => AssemblyNames
.Select(an => Load(an))
.Where(a => a != null);

[DebuggerHidden]
static Assembly Load(string assemblyName)
{
try
{
return Assembly.Load(new AssemblyName(assemblyName));
}
catch
{
return null;
}
}
}
}

{
public SolutionAssemblyNames(ITextTemplatingEngineHost host)
{
Host = host;
}

public IEnumerator GetEnumerator() => Assemblies.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
IEnumerab

Solution

This is a fascinating solution. I kept looking at it and thinking "There must be an easier way!" but every possible solution I came up with had a fatal flaw.

All I really have, then, is nitpicking:

public static IEnumerable Assemblies => AssemblyNames
        .Select(an => Load(an))


Can be simplified to:

public static IEnumerable Assemblies => AssemblyNames
    .Select(Load)


SolutionAssemblyNames is looking fairly dense. I'd add 1 line of whitespace between each property to make it a bit more readable.

Code Snippets

public static IEnumerable<Assembly> Assemblies => AssemblyNames
        .Select(an => Load(an))
public static IEnumerable<Assembly> Assemblies => AssemblyNames
    .Select(Load)

Context

StackExchange Code Review Q#129342, answer score: 3

Revisions (0)

No revisions yet.