我想创建一个映射器工厂来返回相关的映射器类,如下所示。但是我无法使用它,因为我不知道应该将什么放入 GetRelatedMapper 函数返回?
GetRelatedMapper 类应该通过查看实体资源类型返回相关的 MapperClass。
class Program
{
static void Main(string[] args)
{
var entity1 = new Entity1
{
Id = 1,
Name1 = "Name1 Haha",
Type = ResourceType.Entity1
};
Resource1 resource = MapperFactory.GetRelatedMapper(entity1.Type).MapToResource(entity1);
}
}
public static class MapperFactory
{
public static ***???????????*** GetRelatedMapper(ResourceType type)
{
switch (type)
{
case ResourceType.Entity1:
return new Entity1Mapper();
break;
case ResourceType.Entity2:
return new Entity2Mapper();
break;
default:
throw new NotImplementedException();
break;
}
}
}
public interface IMapper<T, K> where T : BaseEntity
where K : BaseResource
{
K MapToResource(T entity);
CompressedEntity MapToCompressedEntity(T entity);
}
public class Entity2Mapper : IMapper<Entity2, Resource2>
{
public CompressedEntity MapToCompressedEntity(Entity2 entity)
{
return new CompressedEntity()
{
Id = entity.Id
};
}
public Resource2 MapToResource(Entity2 entity)
{
return new Resource2()
{
Id = entity.Id,
Name2 = entity.Name2
};
}
}
public class Entity1Mapper : IMapper<Entity1, Resource1>
{
public CompressedEntity MapToCompressedEntity(Entity1 entity)
{
return new CompressedEntity()
{
Id = entity.Id
};
}
public Resource1 MapToResource(Entity1 entity)
{
return new Resource1()
{
Id = entity.Id,
Name1 = entity.Name1
};
}
}
public class Entity2 : BaseEntity
{
public string Name2 { get; set; }
}
public class Entity1 : BaseEntity
{
public string Name1 { get; set; }
}
public class Resource1 : BaseResource
{
public string Name1 { get; set; }
}
public class Resource2 : BaseResource
{
public string Name2 { get; set; }
}
public class BaseEntity
{
public int Id { get; set; }
public ResourceType Type { get; set; }
}
public class BaseResource
{
public int Id { get; set; }
public ResourceType Type { get; set; }
}
public class CompressedEntity
{
public int Id { get; set; }
}
public enum ResourceType
{
Entity1 = 1,
Entity2 = 2
}
最佳答案
我玩过你的例子,你可以这样尝试,然后将 resource
转换为指定的资源:
Resource1 resource = MapperFactory.GetRelatedMapper(entity1)
.MapToResource(entity1) as Resource1;
public static class MapperFactory
{
public static IMapper<T, BaseResource> GetRelatedMapper<T>(T entity)
where T : BaseEntity
{
switch (entity)
{
case Entity1 _:
return new Entity1Mapper() as IMapper<T, BaseResource>;
case Entity2 _:
return new Entity2Mapper() as IMapper<T, BaseResource>;
default:
throw new NotImplementedException();
}
}
}
这是另一个有趣的选项,但它还需要 Resource
:
public static class MapperFactory
{
public static IMapper<T, K> GetRelatedMapper<T, K>(T entity, K resource)
where T : BaseEntity
where K : BaseResource
{
switch (entity)
{
case Entity1 _ when resource is Resource1:
return new Entity1Mapper() as IMapper<T, K>;
case Entity2 _ when resource is Resource2:
return new Entity2Mapper() as IMapper<T, K>;
default:
throw new NotImplementedException();
}
}
}
不过,应该检查问题的摘要层次结构。
关于c# - 使用通用接口(interface)创建工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54093948/