简体   繁体   English

使用通用接口创建工厂

[英]Create a factory by using a generic interface

I want to create a mapper factory that returns the related mapper class like below.我想创建一个映射器工厂,它返回相关的映射器类,如下所示。 But I cannot work it because I do not know what should I put to GetRelatedMapper function return ?但是我无法使用它,因为我不知道应该向 GetRelatedMapper 函数返回什么内容?

The GetRelatedMapper class should return related MapperClass by looking at the entity resource type. 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
}

I played with your example, you can try like this and then casting resource to specified resource:我玩过你的例子,你可以尝试这样,然后将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();
        }
    }
}

Here's an another interesting option, but it requires Resource also:这是另一个有趣的选项,但它也需要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();
        }
    }
}

Question's snippet hierarchy should be reviewed though.不过,应该审查问题的片段层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM