[英]Generic type constraint and variance
I am building a simple type mapper similar to AutoMapper but with a more dynamic behaviour. 我正在构建一个类似于AutoMapper的简单类型映射器,但具有更动态的行为。 The caller can decide to filter RecordStatus == RecordStatus.Deleted
records when mapping from entity framework models. 从实体框架模型映射时,调用者可以决定过滤RecordStatus == RecordStatus.Deleted
记录。
Abstract mappers: 抽象映射器:
public interface IMapper<in TIn, out TOut>
{
TOut Map(TIn input);
}
public interface IRecordStatusFilterable
{
string RecordStatus { get; }
}
public abstract class RecordStatusFilterableMapperBase<TIn, TOut> : IMapper<TIn, TOut>
{
private readonly bool _filterDeletedRecords;
protected RecordStatusFilterableMapperBase(bool filterDeletedRecords)
{
_filterDeletedRecords = filterDeletedRecords;
}
protected bool FilterDeletedRecords
{
get { return _filterDeletedRecords; }
}
public abstract TOut Map(TIn input);
}
public class MultiLookupValuesMapper : RecordStatusFilterableMapperBase<IEnumerable<Lookup>, string>
{
private static readonly Func<Lookup, bool> _predicate =
filterable => filterable.RecordStatus == RecordStatus.Active;
protected MultiLookupValuesMapper(bool filterDeletedRecords) : base(filterDeletedRecords)
{
}
public override string Map(IEnumerable<Lookup> input)
{
var inputList = input as IList<Lookup> ?? input.ToList();
if (!inputList.Any())
{
return string.Empty;
}
if (FilterDeletedRecords)
{
inputList = (IList<Lookup>)inputList.Where(_predicate);
}
return string.Join(", ", inputList.Select(l => l.Value));
}
}
Concrete Mappers: 具体的映射器:
public class FooMapper<TRecordStatusFilterable> : RecordStatusFilterableMapperBase<Foo, FooViewModel>
where TRecordStatusFilterable : class, IRecordStatusFilterable
{
private readonly IMapper<IEnumerable<TRecordStatusFilterable>, string> _multiLookupValueMapper;
public FooMapper(IMapper<IEnumerable<TRecordStatusFilterable>, string> multiLookupValueMapper,
bool filterDeletedRecords) : base(filterDeletedRecords)
{
_multiLookupValueMapper = multiLookupValueMapper;
}
public override FooViewModel Map(Foo input)
{
return new FooViewModel
{
// Error here
BarLookupValues = _multiLookupValueMapper.Map(input.Lookups)
};
}
}
Entity Framework model: 实体框架模型:
public class Foo
{
public ICollection<Lookup> Lookups { get; set; }
}
public class Lookup : IRecordStatusFilterable
{
public string Value { get; set; }
public string RecordStatus { get; set; }
}
ViewModels: 的ViewModels:
public class FooViewModel
{
// ICollection<Lookup> => string
public string BarLookupValues { get; set; }
}
I got a compile error: 我收到一个编译错误:
Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<Lookup>' to 'System.Collections.Generic.IEnumerable<TRecordStatusFilterable>' 参数1:无法从“ System.Collections.Generic.IEnumerable <查找>”转换为“ System.Collections.Generic.IEnumerable <TRecordStatusFilterable>”
But my Lookup
class does fulfill the generic type parameter constraint as it implements IRecordStatusFilterable
. 但是我的Lookup
类确实实现了通用类型参数约束,因为它实现了IRecordStatusFilterable
。 Can anyone shed some light on this? 谁能对此有所启发?
Actually a lot of the code is irrelevant to the actual problem. 实际上,许多代码与实际问题无关。 Here is a simpler version that, hopefully, illustrates it better: 这是一个更简单的版本,希望可以更好地说明它:
class MyList<T>
where T : class, IConvertible
{
private List<T> list = new List<T>();
public void Add(string s)
{
list.Add(s); // error
}
}
Yes T
is constrained, and string
fits the constraints, but that doesn't mean you can go and add string
to a List of arbitrary T's. 是的, T
是受约束的,并且string
符合约束,但这并不意味着您可以将string
添加到任意T的列表中。 That wouldn't type safe. 那不会输入安全。
If I defined 如果我定义
class Bar : IConvertible { /* left out IConvertible impl */ }
and made a var bars = new MyList<Bar>()
it is obvious that adding a string to bars is a problem for that code in the generic class. 并使var bars = new MyList<Bar>()
很明显,将字符串添加到bar是泛型类中该代码的问题。
You've just got a more complex version of this and I'm not 100% sure what exactly you are trying to express. 您刚刚得到了一个更复杂的版本,而我不确定100%确实要表达什么。 Perhaps the class FooMapper
shouldn't be generic at all and should just take an instance of IMapper<IEnumerable<Lookup>, string>
. 也许类FooMapper
根本不应该是泛型的,而应该仅接受IMapper<IEnumerable<Lookup>, string>
的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.