简体   繁体   English

Inheritance 与 generics 接口

[英]Inheritance in interface with generics

I'm trying to migrate an application which has to read from various XML, each of them having different models and methods/stuff to do before persisting them into db.我正在尝试迁移一个必须从各种 XML 读取的应用程序,在将它们持久化到数据库之前,它们每个都有不同的模型和方法/东西要做。 For that reason, I've decided to create an interface IEntityController<T> where T: PersistentEntity and a controller implementing it for each entity (XML), this way they all can have different implementations...出于这个原因,我决定创建一个接口IEntityController<T> where T: PersistentEntity和一个 controller 为每个实体(XML)实现它,这样它们都可以有不同的实现......

    public interface IEntityController<T> where T : PersistentEntity
{
    /// <summary>
    /// Check if the data inserted into the xml is valid and all the requirements are met
    /// </summary>
    /// <exception cref="FooValidationException">When the instance is not valid and can't be saved</exception>
    void Validate(T entity);

    /// <summary>
    /// Implement changes in data before its saved
    /// </summary>
    void PreProcess(T entity);

    /// <summary>
    /// Save or Update the entity
    /// </summary>
    /// <exception cref="System.Data.SqlClient.SqlException"></exception>
    void Persist(T entity);

    /// <summary>
    /// Implement changes in data after the entity is saved
    /// </summary>
    void PostProcess(T entity);
}

After that, I've got a factory method which returns the proper controller based on the xml:之后,我有一个工厂方法,它基于 xml 返回正确的 controller:

        private IEntityController<PersistentEntity> GetController(XmlFile file)
    {
        Type fileType = file.GetType();

        if(fileType == typeof(FileBoo))
            return (IEntityController<PersistentEntity>) new BooController(DC);
      ......
    }

My problem is: BooController implements the interface of IEntityController<Boo> where Boo inherits PersistentEntity , when trying to cast the controller to IEntityController<PersistentEntity> I get an InvalidCastException , though I have no problems casting BooController to IEntityController<Boo> , It's like C# ignores inheritance of the generic types.我的问题是: BooController实现了IEntityController<Boo>的接口,其中Boo继承了PersistentEntity ,当尝试将 controller 转换为IEntityController<PersistentEntity>我得到一个InvalidCastException ,尽管我没有问题将 BooController 转换为IEntityController<Boo> ,就像 ZD7EFA19FBE7D3977244FBE7D39724忽略泛型类型的 inheritance。

Any ideas on how to solve this/implement it better?关于如何解决这个/更好地实施它的任何想法? Thanks谢谢

The simplest thing you can do is to remove the generic type from IEntityController so that it looks like this:您可以做的最简单的事情是从 IEntityController 中删除泛型类型,使其看起来像这样:

 public interface IEntityController
{
    /// <summary>
    /// Check if the data inserted into the xml is valid and all the requirements are met
    /// </summary>
    /// <exception cref="FooValidationException">When the instance is not valid and can't be saved</exception>
    void Validate(PersistentEntity entity);

    /// <summary>
    /// Implement changes in data before its saved
    /// </summary>
    void PreProcess(PersistentEntity  entity);

    /// <summary>
    /// Save or Update the entity
    /// </summary>
    /// <exception cref="System.Data.SqlClient.SqlException"></exception>
    void Persist(PersistentEntity entity);

    /// <summary>
    /// Implement changes in data after the entity is saved
    /// </summary>
    void PostProcess(PersistentEntity  entity);
}

Since your method由于你的方法

private IEntityController<PersistentEntity> GetController(XmlFile file)

always returns an IEntityController of PersistentEntity and not an IEntityController of T you don't need generics at all.总是返回 PersistentEntity 的 IEntityController 而不是 T 的 IEntityController 你根本不需要 generics 。

Remember:记住:

List<string> doesn't inherit from List<object>

otherwise this could happen否则这可能会发生

public static void DestroyTheWorld(List<object> list) {
    list.add(new object());
}

public static void Main() {
    var list = new List<string>();
    DestroyTheWorld(list); //possible if list<string> inherits from list<object>
    //an object has been added to a list of string
}

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

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