c# - 使用通用接口(interface)创建工厂

标签 c#

我想创建一个映射器工厂来返回相关的映射器类,如下所示。但是我无法使用它,因为我不知道应该将什么放入 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/

相关文章:

c# - Linq-to-Entities,比较不同字段/列中的 int 值并返回最大值

c# - 绑定(bind)数据模板文本 block 标签

c# - 以逗号分隔的整数的正则表达式

c# - 我怎样才能停止我的 while 循环?

c# - 从 DataGrid 中删除无效行后,我无法更新行

c# - 在 asp.net 中将电子邮件作为模板发送

c# - 如何在 .NET 中获取打印机信息?

c# - Xamarin C# 有趣的代码标题

c# - 在 edmx 中生成的类中带有外键的表缺少成员

c# - 命名空间问题