[英]C# covariant/contravariant types utilizing generics with Dictionary
i need to help with solving Covariant situation in my Code.我需要帮助解决我的代码中的协变情况。
Let me show the situation on example:让我举例说明情况:
abstract class BaseSource { }
abstract class BaseTarget { }
class SourceEntityA : BaseSource { }
class TargetEntityB : BaseTarget { }
interface IEntityMapper { }
interface IEntityMapper<in TSource, out TTarget>: IEntityMapper
{
TTarget Map(TSource entity);
}
abstract class BaseEntityMap<TSource, TTarget> : IEntityMapper<TSource, TTarget>, IEntityMapper
{
public abstract TTarget Map(TSource entity);
}
class OrderEntityMapper : BaseEntityMap<SourceEntityA, TargetEntityB>
{
public override TargetEntityB Map(SourceEntityA entity)
{
//do some map stuff
return null;
}
}
class GoodsEntityMapper : BaseEntityMap<SourceEntityC, TargetEntityD>
{
public override TargetEntityD Map(SourceEntityC entity)
{
//do some map stuff
return null;
}
}
class Workflow
{
IDictionary<Type, IEntityMapper> MappersMap = new Dictionary<Type, IEntityMapper>();
public MappingArchestrator()
{
MappersMap.Add(typeof(SourceEntityA), new OrderEntityMapper());
MappersMap.Add(typeof(SourceEntityC), new GoodsEntityMapper());
//....more mappers
}
private IEnumerable<BaseSource> GetSourceEntities()
{
//return source data
return null;
}
public void Execute()
{
foreach (var m in MappersMap.Keys)
{
var mapper = MappersMap[m];
var entities = GetSourceEntities();
foreach (var entity in entities)
{
//Need to handle this situations with correct covariations.
var target = (IEntityMapper<BaseSource, BaseTarget>mapper).Map(entity);
//.... code continues
}
}
}
}
Shortly i need Dictionary of different types of mappers and use them Execute method in common code to handle workflow.不久我需要不同类型映射器的字典,并在公共代码中使用它们的 Execute 方法来处理工作流。
Could you guide me to a better code or fix an existing solution?你能指导我使用更好的代码或修复现有的解决方案吗? Is it even possible?
甚至有可能吗?
Or i have to forget generics here and use BaseClasses in Map method and cast them to type what i need.或者我必须在这里忘记泛型并在 Map 方法中使用 BaseClasses 并将它们转换为我需要的类型。
Something like this像这样的东西
abstract class BaseEntityMap : IEntityMapper
{
public abstract BaseTarget Map(BaseSource entity);
}
Thank you谢谢
You can apply generic constraints in BaseEntityMap<TSource, TTarget>
class to your BaseSource
and BaseTarget
classes (or apply constraints in IEntityMapper<in TSource, out TTarget>
interface and get rid of base abstract class)您可以将
BaseEntityMap<TSource, TTarget>
类中的通用约束应用于BaseSource
和BaseTarget
类(或在IEntityMapper<in TSource, out TTarget>
接口中应用约束并摆脱基础抽象类)
abstract class BaseEntityMap<TSource, TTarget> : IEntityMapper<TSource, TTarget>
where TSource : BaseSource
where TTarget : BaseTarget
{
public abstract TTarget Map(TSource entity);
}
Then declare mappers dictionary like below, using Func<BaseSource, BaseTarget>
delegate.然后使用
Func<BaseSource, BaseTarget>
委托声明映射器字典,如下所示。 It should be fine, since you've constrained your BaseEntityMap<TSource, TTarget>
and Func<in T, out TResult>
declaration supports contravariance for the input parameter and covariance for the result应该没问题,因为您已经约束了
BaseEntityMap<TSource, TTarget>
和Func<in T, out TResult>
声明支持输入参数的逆变和结果的协方差
IDictionary<Type, Func<BaseSource, BaseTarget>> MappersMap = new Dictionary<Type, Func<BaseSource, BaseTarget>>();
public Workflow()
{
var orderMapper = new OrderEntityMapper();
MappersMap.Add(typeof(SourceEntityA), source => orderMapper.Map((SourceEntityA)source));
var goodsMapper = new GoodsEntityMapper();
MappersMap.Add(typeof(SourceEntityC), source => goodsMapper.Map((SourceEntityC)source));
//....more mappers
}
The usage example使用示例
foreach (var entity in entities)
{
var target = mapper(entity);
//.... code continues
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.