通用类型约束和方差

时间:2014-12-11 08:20:26

标签: c# entity-framework generics

我正在构建一个类似于AutoMapper的简单类型映射器,但具有更动态的行为。从实体框架模型映射时,调用者可以决定过滤RecordStatus == RecordStatus.Deleted个记录。

抽象地图制作者:

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

具体地图:

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

实体框架模型:

public class Foo
{
    public ICollection<Lookup> Lookups { get; set; }
}

public class Lookup : IRecordStatusFilterable
{
    public string Value { get; set; }

    public string RecordStatus { get; set; }
}

的ViewModels:

public class FooViewModel
{
    // ICollection<Lookup> => string
    public string BarLookupValues { get; set; }
}

我收到了编译错误:

  

参数1:无法转换为&#39; System.Collections.Generic.IEnumerable&lt; Lookup&gt;&#39; to&#39; System.Collections.Generic.IEnumerable&lt; TRecordStatusFilterable&gt;&#39;

但我的Lookup类在实现IRecordStatusFilterable时确实满足泛型类型参数约束。任何人都可以对此有所了解吗?

1 个答案:

答案 0 :(得分:1)

实际上很多代码与实际问题无关。这是一个更简单的版本,希望能更好地说明它:

class MyList<T>
    where T : class, IConvertible
{
    private List<T> list = new List<T>();

    public void Add(string s)
    {
        list.Add(s); // error
    }        
}

T受限制,string符合约束条件,但这并不意味着您可以将string添加到任意T的列表中。这不会安全。

如果我定义了

class Bar : IConvertible { /* left out IConvertible impl */ }

并制作了一个var bars = new MyList<Bar>()很明显,向条形图添加字符串是泛型类中该代码的问题。

你刚刚得到了一个更复杂的版本,而且我不能100%确定你究竟要表达的是什么。也许课程FooMapper根本不应该是通用的,只需要采用IMapper<IEnumerable<Lookup>, string>的实例。