gpt4 book ai didi

c# - StructureMap 通用 Ctor 命名实例

转载 作者:太空狗 更新时间:2023-10-30 01:00:19 26 4
gpt4 key购买 nike

更新

我用下面的代码解决了这个问题,但这不是我要找的解决方案。 这仍然是一个更通用的解决方案的悬赏。如果我们有一个不是 int 的表或 string对于关键值,我们必须手动添加到此以使其工作。

c.For(typeof(ILogDifferencesCommand<,>)).Use(typeof(LogDifferencesCommand<,>))
.Ctor<ILogDifferencesLogger<int>>()
.Named(AppSettingsManager.Get("logDifferences:Target"))
.Ctor<string>()
.Named(AppSettingsManager.Get("logDifferences:Target"));

原始问题

我有三种类型的记录器,我在我的容器中为它们定义了命名实例:

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");

LogDifferencesCommand收到 ILogDifferencesLogger<>作为它唯一的参数:

public LogDifferencesCommand(ILogDifferencesLogger<TKey> logDifferencesLogger)
{
this.logDifferencesLogger = logDifferencesLogger;
}

如何正确配置 ILogDifferencesCommand<>根据应用程序设置获取正确的命名实例?现在我有这样的东西:

c.For(typeof(ILogDifferencesCommand<,>))
.Use(typeof(LogDifferencesCommand<,>));

我遇到的问题是我无法拉入 Ctor<>因为我不能使用带有该签名的未绑定(bind)泛型,所以我不能使用 Named关闭方法 Ctor为此。

例如,我可以做这样的事情,但这不会影响所有可能的类型:

c.For(typeof(ILogDifferencesCommand<,>)).Use(typeof(LogDifferencesCommand<,>))
.Ctor<ILogDifferencesLogger<int>>()
.Named(AppSettingsManager.Get("logDifferences:Target"));

但问题是我必须处理每个 TKey系统使用的类型。

类和接口(interface)定义

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界面。

最佳答案

想到的一个选项:

var loggers = new Dictionary<string, ConfiguredInstance>();
loggers.Add("all", c.For(typeof(ILogDifferencesLogger<>))
.Use(typeof(LogDifferencesAllLogger<>)));
loggers.Add("nlog", c.For(typeof(ILogDifferencesLogger<>))
.Use(typeof(LogDifferencesNLogLogger<>)));
loggers.Add("database", c.For(typeof(ILogDifferencesLogger<>))
.Use(typeof(LogDifferencesDatabaseLogger<>)));
foreach (var kv in loggers) {
// if you still need them named
// if you only used names for this concrete scenario - you probably don't
// so can remove it
kv.Value.Named(kv.Key);
}
c.For(typeof(LogDifferencesCommand<>))
.Use(typeof(LogDifferencesCommand<>))
// add explicit instance as dependency
.Dependencies.Add(typeof(ILogDifferencesLogger<>), loggers[AppSettingsManager.Get("logDifferences:Target")]);

更新。正如我们在评论中发现的那样,这不适用于您在 LogDifferencesCommand 时的特定情况。有多个类型参数。出于某种原因(我认为这是一个错误)- 结构图试图创建封闭的泛型类型 ILogDifferencesLogger<> , 但这样做时 - 从 LogDifferencesCommand 传递通用类型参数.也许值得在他们的 github 上提出一个问题。您可以像这样解决它:

public class GenericTypesWorkaroundInstance : Instance
{
private readonly Instance _target;
private readonly Func<Type[], Type[]> _chooseTypes;
public GenericTypesWorkaroundInstance(Instance target, Func<Type[], Type[]> chooseTypes) {
_target = target;
_chooseTypes = chooseTypes;
ReturnedType = _target.ReturnedType;
}

public override Instance CloseType(Type[] types) {
// close type correctly by ignoring wrong type arguments
return _target.CloseType(_chooseTypes(types));
}

public override IDependencySource ToDependencySource(Type pluginType) {
throw new NotSupportedException();
}

public override string Description => "Correctly close types over open generic instance";
public override Type ReturnedType { get; }
}

然后做

commandReg.Dependencies.Add(
commandReg.Constructor.GetParameters().First(
p => p.ParameterType.IsGenericType && p.ParameterType.GetGenericTypeDefinition() == typeof(ILogDifferencesLogger<>)).Name,
new GenericTypesWorkaroundInstance(
loggers[AppSettingsManager.Get("logDifferences:Target")],
// specify which types are correct
types => types.Skip(1).ToArray()));

它有效,但我不能说我喜欢它。

关于c# - StructureMap 通用 Ctor 命名实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com