簡體   English   中英

Inheritance 與 generics 接口

[英]Inheritance in interface with generics

我正在嘗試遷移一個必須從各種 XML 讀取的應用程序,在將它們持久化到數據庫之前,它們每個都有不同的模型和方法/東西要做。 出於這個原因,我決定創建一個接口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);
}

之后,我有一個工廠方法,它基於 xml 返回正確的 controller:

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

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

我的問題是: BooController實現了IEntityController<Boo>的接口,其中Boo繼承了PersistentEntity ,當嘗試將 controller 轉換為IEntityController<PersistentEntity>我得到一個InvalidCastException ,盡管我沒有問題將 BooController 轉換為IEntityController<Boo> ,就像 ZD7EFA19FBE7D3977244FBE7D39724忽略泛型類型的 inheritance。

關於如何解決這個/更好地實施它的任何想法? 謝謝

您可以做的最簡單的事情是從 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);
}

由於你的方法

private IEntityController<PersistentEntity> GetController(XmlFile file)

總是返回 PersistentEntity 的 IEntityController 而不是 T 的 IEntityController 你根本不需要 generics 。

記住:

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

否則這可能會發生

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