C#接口和泛型协同工作

时间:2019-08-23 21:40:32

标签: c# generics design-patterns interface open-closed-principle

我有一个域对象 Address ,它可以从各种数据源中填充,这需要大量的映射代码。为了实现“ 修改结束”,我希望能够为每个数据源创建单独的“映射器”。然后,我可以将映射器传递到地址和VOILA的实例中!得到一个适当的数据实体作为回应。反之亦然,我还想在该 Address 上实现一个方法,该方法使我可以将实体映射到新的实体或填充 Address 的现有实例。 / p>

我创建我的 Address 对象...

public class Address
{
    public string Street1 { get; set; }
    public string Street2 { get; set; }
    public string Street3 { get; set; }
    public string Street4 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string PostalCode { get; set; }
}

现在,我创建了两个类,这些类将有助于在此 Address 对象之间来回映射特定的数据实体对象。

//
// Maps to and from a Database object (DB1_ADDRESS)
//
public class DB1AddressMapper
{
    property DB1_ADDRESS _entity;

    public DB1AddressMapper()
    {

    } 

    public DB1AddressMapper(DB1_ADDRESS entity)
    {
        _entity = entity;
    }

    public DB1_ADDRESS MapModelToEntity(Address model)
    {
        DB1_ADDRESS ret = new DB1_ADDRESS();

        <... mapping logic goes here>

        return ret;
    }

    public Address MapEntityToModel()
    {
        Address ret = new Address();

        <... mapping logic goes here>

        return ret;
    }
}

//
// Maps to and from a WebService response (WS_ADDRESS)
//
public class WSAddressMapper
{
    property WS_ADDRESS _entity;

    public WSAddressMapper()
    {

    } 

    public WSAddressMapper(WS_ADDRESS entity)
    {
        _entity = entity;
    }

    public WS_ADDRESS MapModelToEntity(Address model)
    {
        WS_ADDRESS ret = new WS_ADDRESS();

        <... mapping logic goes here>

        return ret;
    }

    public Address MapEntityToModel()
    {
        Address ret = new Address();

        <... mapping logic goes here>

        return ret;
    }
}

现在有了映射器,我可以在地址上创建一个方法,可以将其传递到其中,以方便转换数据。因此,您可以在下面的代码中看到我不得不重载方法,因为每个映射器都有自己的类型。这意味着每次我想添加新的数据源来填充Address对象时,都必须重新打开 Address 并添加新的重载方法。 g……不,谢谢(“关闭以进行修改”发生了什么?)

public class Address
{
    public string Street1 { get; set; }
    public string Street2 { get; set; }
    public string Street3 { get; set; }
    public string Street4 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string PostalCode { get; set; }
    //
    // Populate "this" instance of the Address object from data found in the mapper.
    // The "mapper" argument would have to have been instantiated with the entity it expects to map 
    // to the Domain object, Address
    //
    public Address MapToModel(DB1AddressMapper mapper)
    {
        return mapper.MapEntityToModel();
    }

    //
    // Map "this" instance of address to a new DB1_ADDRESS instance
    //
    public DB1_ADDRESS MapToEntity(DB1AddressMapper mapper)
    {
        return mapper.MapModelToEntity(this);
    }


    //
    // And now again for WSAddressMapper
    //
    public Address MapToModel(WSAddressMapper mapper)
    {
        return mapper.MapEntityToModel();
    }

    //
    // Map "this" instance of address to a new WS_ADDRESS instance
    //
    public WS_ADDRESS MapToEntity(WSAddressMapper mapper)
    {
        return mapper.MapModelToEntity(this);
    }

} 

这使我进入了接口和泛型……我已经涉足了多年,但是对它们的缺乏性并没有迫使我加深我对它们的理解(我相信这会使我退缩)。

回到当前的问题...我只想在地址中使用两种映射方法,这些方法将“关闭以进行修改”。他们需要为我遇到的任何数据源提供任何映射器。映射器封装了所有特定的映射逻辑,并且Address并不真正在乎细节。它只想“ MapTo”。

伪代码解决方案看起来像这样...

public class Address 
{
    public Address MapToModel(EntityMapper mapper)
    {
        ...
    }

    public EntityAddress MapToEntity(EntityMapper mapper)
    {
        ...
    }
}

似乎我可以为映射器创建一个接口,以便所有映射器都将实现相同的两种方法...

MapModelToEntity();
MapEntityToModel();

我从那开始...

public interface IEntityAddressMapper
{
    Address MapEntityToModel();
    T MapModelToEntity<T>(Address model);
}

您也许可以看到我在哪里遇到麻烦。由于“ MapModelToEntity”的返回类型因数据源而异,因此我不知道该怎么做。我选择使其通用。这些在其他领域为我工作。我希望通过在制图仪中实施该程序来继续下去,希望答案能够揭晓。

public class DB1AddressMapper : IEntityAddressMapper
{
    Address MapEntityToModel()
    {
        Address ret = new Address();

        <... mapping logic goes here>

        return ret;
    }

    //
    // This is what I want but, does NOT satisfy interface
    //
    DB1_ADDRESS MapModelToEntity(Address model)  <!-- DOES NOT SATISFY INTERFACE
    {
        DB1_ADDRESS ret = new DB1_ADDRESS();

        <... mapping logic goes here>

        return ret;
    }
    //
    // This satisfies interface but is silly. The mapper already KNOWS the TYPE, that's the point.
    // Besides this means that the consumer will have to pass in the types, which is EXACTLY what 
    // I am trying to avoid.
    //
    T MapModelToEntity<T>(Address model)  
    {
        DB1_ADDRESS ret = new DB1_ADDRESS();

        <... mapping logic goes here>

        return ret;
    }   
}

I've tried a million different permutations so it's impractical to list them all here but suffice to say the closest I have come so far is the following ...



public interface IEntityAddressMapper<EntityType>
{

    EntityType MapModelToEntity(Address mode);

    void MapModelToEntity(Address model, ref EntityType entity);

    Address MapEntityToModel(EntityType entity);

    void MapEntityToModel(EntityType entity, ref Address model);
}



public class DB1AddressMapper : IEntityAddressMapper<DB1_ADDRESS>
{
    Address MapEntityToModel()
    {
        Address ret = new Address();

        <... mapping logic goes here>

        return ret;
    }

    T MapModelToEntity(Address model)  
    {
        DB1_ADDRESS ret = new DB1_ADDRESS();

        <... mapping logic goes here>

        return ret;
    }   
}

这似乎使我现在可以毫无问题地实现接口,但是我似乎已经将负担转移到了现在正在中断的方法上……

public class Address 
{
    // *********************************************
    // ERROR - Using generic type 'IEntityAddressMapper<EtityType>' requires one type argument
    // *********************************************
    public Address MapToModel(EntityMapper mapper)
    {
        ...
    }

    // *********************************************
    // ERROR - Using generic type 'IEntityAddressMapper<EtityType>' requires one type argument
    // *********************************************
    public EntityAddress MapToEntity<EntityType>(EntityMapper mapper)
    {
        ...
    }
}

我在圈子里转悠,对此已经有很多年了。我需要解决这个问题!任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:2)

您需要从一个通用接口开始,该接口根据两个类型参数(而不是一个)而变化。这将解决您眼前的问题,但是在解决方案之后,我想提出另一种方法。

解决方案

考虑此界面

public interface IMapper<TModel, TEntity>
{
    TEntity MapModelToEntity(TModel source);

    TModel MapEntityToModel();
}

它允许您创建可以传递给Address类的特定实现:

public class DatabaseAddressMapper : IMapper<Address, DB1_ADDRESS>
{
    public DB1_ADDRESS MapModelToEntity(Address source) { ... }

    Address MapEntityToModel()
}

public class WSAddressMapper : IMapper<Address, WS_ADDRESS>
{
    public WS_ADDRESS MapModelToEntity(Address source) { ... }

    Address MapEntityToModel()
}

并修改Address,使其具有一些可以接受您的映射器的通用方法

// only need TEntity for this generic method because we know we are an Address
public Address MapToModel<TEntity>(IMapper<Address, TEntity> mapper)
{
    return mapper.MapEntityToModel();
}

// only need TEntity for this generic method because we know we are an Address
public TEntity MapToEntity<TEnity>(IMapper<Address, TEntity> mapper)
{
    return mapper.MapModelToEntity(this);
}

通过此设置,您的Address类现在跟随Open/Closed Principal,因为它可以接受支持Address的任何映射器以及任何任意类型。这也可以使您的Address与其他类型完全脱离,这很好。

替代

这里有待改进的地方,这很简单。问自己:为什么Address根本不需要了解任何有关映射的知识?这只是一个地址。映射是其他问题的关注点:也许是调用者本身?

您可以从Address中完全删除方法,也可以在调用者调用这些方法的地方将其删除

var mapper = new WSAddressMapper();
var model = address.MapToModel<WS_ADDRESS>(mapper);
var entity = address.MapToEntity();

您可以直接调用映射器。

var model = mapper.MapEntityToModel<WS_ADDRESS>();
var entity = mapper.MapModelToEntity(address);

这意味着您的Address现在也遵循Single Responsibility Principle!毕竟,启动映射的是您的呼叫者; 是应该承担的责任,而不是地址本身。

让我们继续前进!

为什么一个接口可以双向映射?毕竟,您更有可能将一个方向映射为一小段代码(例如,保存到数据库),而将另一个方向映射为另一小段代码(例如,从数据库中读取)。

让我们将接口分为两个,每个接口只有一种方法。我将把该实现留给您,但是您会得到以下结果:另一个SOLID支柱,Interface Segregation Principle。呜呼!