简体   繁体   English

具有接口列表的接口,如何选择由接口实现的一种类型

[英]Interface with a list of interface, how to choose one type implemented by interface

This is my first question on StackOverflow, so please forgive and tell me if I'm doing something wrong. 这是我关于StackOverflow的第一个问题,所以请原谅并告诉我我做错了什么。

Problem: 问题:

I write some kind of dictionary connected to DB and text files etc. nothing commercial, just learning. 我写了一些字典,连接到数据库和文本文件等。没有什么商业意义,只是学习而已。 For better explanation it can be English-French. 为了更好的解释,它可以是英语-法语。

I want to refactor the code to have possibility of use one "general" method to process entrance for English-French and French-English dictionary model. 我想重构代码以使用一种“通用”方法来处理英法词典和法英词典模型的输入的可能性。 On the begining i made separate model for each of them(I will paste if necessary) and now i would like to make everything "universal". 一开始,我为每个模型创建了单独的模型(如有必要,我将粘贴),现在我想使所有模型“通用”。 What I did till i stop: 直到停下脚步,我所做的一切:

public interface IWordModel 
{

    int Id { get; set; }
    string Name { get; set; }
    string Definition { get; set; }
}

class implementing IWordModel: 实现IWordModel的类:

public class EnglishWordModel: IWordModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Definition { get; set; } = null;
}
public class FrenchWordModel : IWordModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Definition { get; set; } = null;
}

Interface implementing IWordModel and problematic List of this interface: 实现IWordModel的接口和该接口的问题列表:

public interface IDictionairyModel<T> where T : IWordModel
{
    int Id { get; set; }
    T BaseWord { get; set; }
    List<T> DerivativeWords { get; set; }
}

Class implementing IDicionairyModel 实现IDicionairyModel的类

public class EnglishFrenchDictionairyModel<T>: IDictionairyModel where T : IWordModel
{
    public int Id { get; set; }
    public IWordModel BaseWord { get; set; } = new EnglishWordModel();
    public List<IWordModel> DerivativeWords { get; set; } =  = new 
    List<IWordModel>(new List<FrenchWordModel>());
}
public class FrenchDictionairyModel: IDictionairyModel<T> where T : IWordModel
{
    public int Id { get; set; }
    public IWordModel BaseWord { get; set; } = new FrenchWordModel();
    public List<IWordModel> DerivativeWords { get; set; } =  = new 
    List<IWordModel>(new List<EnglishWordModel>());
}

And my Question 我的问题

  1. How to make that ie in FrenchDictionairyModel instance we will be able to define BaseWord only as FrenchWordModel and add to DerivativeWords list ONLY EnglishWordModel? 如何做到这一点,即在FrenchDictionairyModel实例中,我们只能将BaseWord定义为FrenchWordModel,并且仅将EnglishWordModel添加到DerivativeWords列表中? I know it have something common with covariance and contrvariance but i dont have idea how to apply this here. 我知道协方差和协方差有一些共同点,但我不知道如何在这里应用。

  2. Is it above code have some sense from experienced coder point of view or it's look like OK only in my head? 从经验丰富的编码人员的角度来看,上面的代码是否具有某种意义,或者仅在我脑海中看起来还可以? If answer is NO then how it should look like, what pattern should i use? 如果答案为“否”,那么它将是什么样子,我应该使用哪种模式?

  3. How to use it properly in other methods? 如何以其他方式正确使用它? As now i was using ie 就像现在我正在使用

     public List<EnglishFrenchDictionairyModel> CreateEnglishFrenchEntrance(List<EnglishFrenchDictionairyModel> model){ 

    ( ... )} (...)}

    but its already showing "Using generic type requires 1 type arguments". 但其已显示“使用泛型类型需要1个类型参数”。

Thanks and have a Great Day! 谢谢,祝你有美好的一天!

It sounds like you need two generic parameters - one to apply to BaseWord and one to apply to DerivativeWords : 听起来您需要两个通用参数-一个适用于BaseWord ,另一个适用于DerivativeWords

public interface IDictionairyModel<T,U> 
    where T : IWordModel, U : IWordModel
{
    int Id { get; set; }
    T BaseWord { get; set; }
    List<U> DerivativeWords { get; set; }
}

Then define your FrenchDictionaryModel as so: 然后这样定义FrenchDictionaryModel

public class FrenchDictionairyModel: 
    IDictionairyModel<FrenchWordModel, EnglishWordModel>
{
    public int Id { get; set; }
    public FrenchWordModel BaseWord { get; set; } = new FrenchWordModel();
    public List<EnglishWordModel> DerivativeWords { get; set; } =  new List<EnglishWordModel>();
}

Thanks D Stanley! 谢谢D史丹利! it works fine, just need to add two where clauses for U and T like: 它工作正常,只需为U和T添加两个where子句即可:

public interface IDictionairyModel<T,U> 
where T : IWordModel,
where U : IWordModel {(...)}

But now i have another issue which i would like to implement here. 但是现在我有另一个问题想在这里实施。 For example i would like to create some method which will be remove duplicates from List but i want to this to be ONE method for all class which implementing IDictionairyModel 例如,我想创建一些方法,该方法将从List中删除重复项,但是我希望这对实现IDictionairyModel的所有类都是一个方法

        public static List<IDictionairyModel<IWordModel, IWordModel>> RemoveDuplicates(this List<IDictionairyModel<IWordModel, IWordModel>> model)
    {
        (...)  return model;
    }

What I need to do to be able to use this extension method on 我需要做些什么才能能够使用此扩展方法

List<FrenchDictionairyModel> model = new List<FrenchDictionairymodel>();
model.RemoveDuplicates();

As for now it return error. 至于现在,它返回错误。 Should I make FrenchDictionairyModel also generic like: 我应该让FrenchDictionairyModel也通用吗?

public class PoznanPolishDictionairyModel<T,U> : IDictionairyModel<PoznanWordModel, PolishWordModel>
where T:IWordModel
where U:IWordModel

??? ??? What is the proper way 什么是正确的方法

Thanks a lot!!! 非常感谢!!!

Have a wonderful Sunday! 祝你有个美好的星期天!

Best Regards 最好的祝福

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

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