【发布时间】:2017-04-12 13:34:39
【问题描述】:
我有一个用于将实体对象映射到域对象的接口
public interface IDataEntity<in T1, out T2> where T1 : new() where T2 : new()
{
T2 Map(T1 obj);
}
实施
public class MyEntityObj : IDataEntity<MyEntityObj, MyDomainObj>
{
//props
public MyDomainObj Map(MyEntityObj obj){
// mapping here
return new MyDomainObj();
}
}
如何编写接口以允许我编写这样的实现
public class MyEntity : IDataEntity<MyDomainObj>{
}
谢谢!
【问题讨论】:
-
唯一的方法是返回
IDataEntity而不是T2,但是你基本上失去了你想要的强类型,因为第二种类型是通用的。
标签: c# generics mapping domain-model