简体   繁体   English

通用接口实现混乱

[英]Generic interface implementation confusion

I'm getting myself in a bit of a mess regarding interface implementations, all my attempted 'fixes' seem to make the whole solution more complex and broken. 我对接口实现有点混乱,所有我尝试过的'修复'似乎都让整个解决方案变得更加复杂和破碎。 I'm sure there is a simple answer, but I can't quite see it at the moment! 我确信有一个简单的答案,但我现在看不到它!

I have these two interfaces (the second is used in a list of the first): 我有这两个接口(第二个在第一个列表中使用):

public interface IUserDefinedListEditViewModel<T> where T : IUserDefinedListEntryEditViewModel<IBaseUserDefinedListModel>
{
    string TypeName { get; set; }
    IList<T> UserDefinedListEntries { get; set; }
}

public interface IUserDefinedListEntryEditViewModel<T> where T : IBaseUserDefinedListModel
{
    string Display { get; set; }
    T UserDefinedListEntry { get; set; }
}

I have a third interface which is implemented by several different classes: 我有第三个接口,由几个不同的类实现:

public interface IBaseUserDefinedListModel
{
    Guid Id { get; set; }
    string Name { get; set; }
    bool IsSystem { get; set; }
}

Below is my (incorrect) implementation attempt: 以下是我的(不正确的)实施尝试:

public class APEditViewModel : IUserDefinedListEditViewModel<APEntryEditViewModel>
{
    public string TypeName { get; set; }
    public IList<APEntryEditViewModel> UserDefinedListEntries { get; set; } = new List<APEntryEditViewModel>();
}

public class APEntryEditViewModel : IUserDefinedListEntryEditViewModel<APModel>
{
    public string Display { get; set; }
    public APModel UserDefinedListEntry { get; set; }
}

public class BaseUserDefinedListModel : IBaseUserDefinedListModel
{
    public Guid Id { get; set; }
    [Required(ErrorMessage = "The Name field is required.")]
    public string Name { get; set; }
    public bool IsSystem { get; set; }
}

public class APModel : BaseUserDefinedListModel
{
    public string NewValue { get; set; }
}

The main error I'm getting at the moment is in the APEditViewModel, here is the (cut down) error: 我目前得到的主要错误是在APEditViewModel中,这是(减少)错误:

The type 'APEntryEditViewModel' cannot be used as type parameter 'T' in the
generic type or method 'IUserDefinedListEditViewModel<T>'. There is no 
implicit reference conversion from 'APEntryEditViewModel' to 
'IUserDefinedListEntryEditViewModel<IBaseUserDefinedListModel>'.

I'm not sure whether I need this level of generic interfaces, but from my research and experiments, I believe I do. 我不确定我是否需要这种级别的通用接口,但是从我的研究和实验中,我相信我做到了。 I'm just not quite getting there and I'm thinking that the IUserDefinedListEditViewModel interface needing a type in the type interface ( IUserDefinedListEntryEditViewModel ) seems wrong. 我只是没有到达那里,我认为IUserDefinedListEditViewModel接口需要类型接口中的类型( IUserDefinedListEntryEditViewModel )似乎是错误的。

Sorry that I'm not making myself that clear, it's quite tricky to explain because I'm not sure where I'm going wrong, so any questions I'll try and answer/update my question. 对不起,我不是那么清楚,解释起来相当棘手,因为我不确定我哪里出错,所以我会尝试回答/更新我的问题。

Change your IUserDefinedListEditViewModel interface to: 将您的IUserDefinedListEditViewModel接口更改为:

public interface IUserDefinedListEditViewModel<T1,T2> 
           where T1 : IUserDefinedListEntryEditViewModel<T2> 
           where T2 : IBaseUserDefinedListModel

After that, and update the implementation in APEditViewModel : 之后,更新APEditViewModel的实现:

public class APEditViewModel : IUserDefinedListEditViewModel<APEntryEditViewModel, APModel>

The error indicates that with the generic constraint 该错误表明具有通用约束

where T : IUserDefinedListEntryEditViewModel<IBaseUserDefinedListModel>

only IUserDefinedListEntryEditViewModel<IBaseUserDefinedListModel> and types which implement this interface will be accepted as T . 只有 IUserDefinedListEntryEditViewModel<IBaseUserDefinedListModel>和实现此接口的类型将被接受为T If you want any IBaseUserDefinedListModel to be accepted in the type parameter of T , you need to make it generic as well. 如果您希望在T的类型参数中接受任何IBaseUserDefinedListModel ,您还需要使其成为通用的。

You need to give us the whole business scenario for expecting the right design suggestions. 您需要为我们提供整个业务场景,以期获得正确的设计建议。

As of above examples the issue is: Your defined interface:: 如上例所示,问题是:您定义的接口::

interface IUserDefinedListEditViewModel<T> where T : IUserDefinedListEntryEditViewModel<IBaseUserDefinedListModel>

But while calling you are passing APEntryEditViewModel as Interface specific type. 但在调用时,您将APEntryEditViewModel作为接口特定类型传递。 Which is wrong with OOPS. OOPS哪个错了。

To successfully compile this your 要成功编译你的

APEntryEditViewModel must be an implimentation of IUserDefinedListEntryEditViewModel APEntryEditViewModel必须是IUserDefinedListEntryEditViewModel的实现

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

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