Barış Velioğlu
Barış Velioğlu

Reputation: 5817

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 ?

The GetRelatedMapper class should return related MapperClass by looking at the entity resource type.

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
}

Upvotes: 2

Views: 130

Answers (1)

Yurii N.
Yurii N.

Reputation: 5703

I played with your example, you can try like this and then casting resource to specified 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:

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.

Upvotes: 1

Related Questions