Update
I solved this problem with the following code, but this is not the solution I'm looking for. This is still open generosity for a more general solution. If we ever have a table that is not intor stringfor a key value, we will have to add to this manually for it to work.
c.For(typeof(ILogDifferencesCommand<,>)).Use(typeof(LogDifferencesCommand<,>))
.Ctor<ILogDifferencesLogger<int>>()
.Named(AppSettingsManager.Get("logDifferences:Target"))
.Ctor<string>()
.Named(AppSettingsManager.Get("logDifferences:Target"));
Original question
I have three types of registrars, and I defined them with instance names in my container:
c.For(typeof(ILogDifferencesLogger<>))
.Use(typeof(LogDifferencesAllLogger<>))
.Named("all");
c.For(typeof(ILogDifferencesLogger<>))
.Use(typeof(LogDifferencesNLogLogger<>))
.Named("nlog");
c.For(typeof(ILogDifferencesLogger<>))
.Use(typeof(LogDifferencesDatabaseLogger<>))
.Named("database");
LogDifferencesCommandaccepts ILogDifferencesLogger<>as a single argument:
public LogDifferencesCommand(ILogDifferencesLogger<TKey> logDifferencesLogger)
{
this.logDifferencesLogger = logDifferencesLogger;
}
How to configure correctly ILogDifferencesCommand<>to capture the right named instance based on application configuration? Right now I have something like this:
c.For(typeof(ILogDifferencesCommand<,>))
.Use(typeof(LogDifferencesCommand<,>));
, , , Ctor<>, , Named Ctor .
, - , :
c.For(typeof(ILogDifferencesCommand<,>)).Use(typeof(LogDifferencesCommand<,>))
.Ctor<ILogDifferencesLogger<int>>()
.Named(AppSettingsManager.Get("logDifferences:Target"));
, TKey, .
public class LogDifferencesCommand<TModel, TKey> : ILogDifferencesCommand<TModel, TKey>
where TModel : class, IIdModel<TKey>
{
public LogDifferencesCommand(ILogDifferencesLogger<TKey> logDifferencesLogger)
{
this.logDifferencesLogger = logDifferencesLogger;
}
}
public interface ILogDifferencesCommand<TModel, TKey>
where TModel : class, IIdModel<TKey>
{
List<LogDifference> CalculateDifferences(TModel x, TModel y);
void LogDifferences(TModel x, TModel y, string tableName, string keyField, string userId, int? clientId);
void RegisterCustomDisplayNameObserver(WeakReference<ICustomDisplayNameObserver<TModel>> observer);
void RegisterCustomChangeDateObserver(WeakReference<ICustomChangeDateObserver<TModel>> observer);
}
public interface ILogDifferencesLogger<TKey>
{
void LogDifferences(string tableName, string keyField, string userId, TKey id, List<LogDifference> differences, int? clientId);
}
TKey - IIdModel.