I want to define different DB for different repositories (I use more than one datasources for different datatypes). Like builder.RegisterType().As().Where(t=>t.Name.Contains("someName")); and builder.RegisterType().As().Where(t=>t.Name.Not.Contains("someName"));
It might be better to remove the ambiguity from your design. Your IDbDataContext
is ambiguous, since FirstDbDataContext
and SecondDbDataContext
are not really compatible implementations of the same contract, since they aren't interchangeable; a repository requires a certain database and it will fail when an IDbDataContext
for the wrong database is passed in.
Try giving each context its own abstraction, such as IFirstDbDataContext
and ISecondDbDataContext
. By letting a repository depend explicitly on either of those interfaces it becomes clear to anyone who looks at the constructor what this repository depends on.
But it not only becomes much easier for anyone maintaining the code, it becomes significantly easier to wire everything together using a DI container, simply because you removed the ambiguity.
This is the registration you'll end up with:
builder.RegisterType<FirstDbDataContext>().As<IFirstDbDataContext>();
builder.RegisterType<SecondDbDataContext>().As<ISecondDbDataContext>();