I'm using the LINQ Zip()
method do create a Dictionary<TAbstraction,TImplementation>
in order to register all my types in a DI Container.
The TAbstraction are the exposed Types with names that ends with Repository from an Assembly , and the TImplementation are the implementations (with name that ends with Repository) of the abstraction located in another assembly.
So far, I've ordered the two IEnumerable
by Type.Name
, and the registration can be performed but is very fragile.
I've kept the types in my assembly named after a convention (the abstraction has to be called [objectname]Repository
, and the implementation Sql[objectname]Repository
).
The result lists are ordered alphabetically, so if I add an interface called I[objectname]Repository
(or more likely if a developer after me will do that) the list order will be messed up.
The dictionary is created like this:
var dictionary = repositories.Zip(repositoriesImp, (r, rImp) => new { r, rImp })
.ToDictionary(x => x.r, x => x.rImp);
I'd like to introduce a where clause in or after the Zip()
method in order to avoid unexpected errors, something like this
var dictionary = (from r in repositories
join rImp in repositoriesImp
on //?
where rImp.Name.Contains(r.Name)
select new{
Key = r,
Value = rImp
}).ToDictionary(x=>x.Key,x=>x.Value);
From the MSDN I've seen that the delegate Func<TFirst, TSecond, TResult>
you can perform some operations (they use a ternary operator), so I'm wondering if you can check the Type.Name
from there.