C# 在本例中,是否可以消除类型参数的重复?

C# 在本例中,是否可以消除类型参数的重复?,c#,mvp,type-parameter,C#,Mvp,Type Parameter,我有一个MVP实现,用于域模型中的项目列表 我的问题通常是如何(或者我可以)消除具体视图和演示者中类型参数的重复 我倾向于认为我已经达到了一个极限,要么接受复制,要么重新设计类。另一个选择是我遗漏了一些明显的东西。不管怎样,这就是我为什么要问这个问题 具体而言,如果您有: interface IA<T2, T3> { } class C<T1, T2, T3> where T1 : IA<T2, T3> 演示者有轻微变化。而是推断你的观点。因为您的具体视图实

我有一个MVP实现,用于域模型中的项目列表

我的问题通常是如何(或者我可以)消除具体视图和演示者中类型参数的重复

我倾向于认为我已经达到了一个极限,要么接受复制,要么重新设计类。另一个选择是我遗漏了一些明显的东西。不管怎样,这就是我为什么要问这个问题

具体而言,如果您有:

interface IA<T2, T3> { }
class C<T1, T2, T3> where T1 : IA<T2, T3>

演示者有轻微变化。而是推断你的观点。因为您的具体视图实现了正确的接口,所以它可以工作。请参阅下面的代码

// Abstract View
public interface IView<TItem, TKey>
{
    void Fill(TItem[] items);
    event SelectEvent<TKey> SelectionChanged;
    event MessageEvent Add;
}

// Event Delegates
public delegate void SelectEvent<TKey>(TKey selectedValue);
public delegate bool MessageEvent();

// Model Entities
public class Note { }
public class Attachment { }

// Abstract Presenter
public abstract class Presenter<TItem, TKey>
{
    protected TKey SelectedValue { get; private set; }
    protected IView<TItem, TKey> View { get; set; }
    public Presenter(IView<TItem, TKey> view)
    {
        View = view;
        View.SelectionChanged += OnSelectionChangedInternal;
        View.Add += OnAdd;
    }
    void OnSelectionChangedInternal(TKey selectedValue)
    {
        this.SelectedValue = selectedValue;
        OnSelectionChanged(selectedValue);
    }
    protected abstract void OnSelectionChanged(TKey selectedVaule);
    protected abstract bool OnAdd();
}

// Concrete Views
public interface INotes : IView<Note, string> { }
public interface IAttachments : IView<Attachment, string> { }

// Concrete Presenters
public class NotesPresenter : Presenter<Note, string>
{
    public NotesPresenter(INotes view) : base(view) { }
    protected override void OnSelectionChanged(string publisherName) { }
    protected override bool OnAdd() { return false; }
}

public class AttachmentsPresenter : Presenter<Attachment, string>
{
    public AttachmentsPresenter(IAttachments view) : base(view) { }
    protected override void OnSelectionChanged(string publisherName) { }
    protected override bool OnAdd() { return false; }
}
//抽象视图
公共接口IView
{
填空(第[]项);
事件选择事件选择已更改;
事件消息事件添加;
}
//活动代表
公共委托无效SelectEvent(TKey selectedValue);
公共委托bool MessageEvent();
//模型实体
公共类注释{}
公共类附件{}
//摘要演讲者
公共抽象类演示者
{
受保护的TKey SelectedValue{get;private set;}
受保护的IView视图{get;set;}
公共演示者(IView视图)
{
视图=视图;
View.SelectionChanged+=打开选择更改内部;
View.Add+=OnAdd;
}
选择更改内部无效(TKey selectedValue)
{
this.SelectedValue=SelectedValue;
OnSelectionChanged(selectedValue);
}
选择更改时受保护的摘要无效(TKey selectedVaule);
受保护的抽象bool-OnAdd();
}
//具体观点
公共接口INotes:IView{}
公共接口附件:IView{}
//混凝土演示者
公共类注释resenter:演示者
{
PublicNotesPresenter(INotes视图):基本(视图){}
受保护的覆盖无效OnSelectionChanged(字符串publisherName){}
受保护的重写bool OnAdd(){return false;}
}
公共类附件resenter:演示者
{
公共附件资源中心(IAttachments视图):基础(视图){}
受保护的覆盖无效OnSelectionChanged(字符串publisherName){}
受保护的重写bool OnAdd(){return false;}
}

C#中没有“模板”。不,您不能这样做。编译器无法“推断”这些类型,因为在您的情况下,它们是相同的,这只是巧合。他们不必总是这样。TView:IView中的
类演示者是否将第二和第三种类型的
演示者
约束为第一和第二种类型的
IView
?如果没有,那么谢谢你指出我的误解。。。
// Abstract View
public interface IView<TItem, TKey>
{
   void Fill(TItem[] items);
   event SelectEvent<TKey> SelectionChanged;
   event MessageEvent Add;
}

// Event Delegates
public delegate void SelectEvent<TKey>(TKey selectedValue);
public delegate bool MessageEvent();

// Model Entities
public class Note { }
public class Attachment { }

// Abstract Presenter
public abstract class Presenter<TView, TItem, TKey> where TView : IView<TItem, TKey>
{
   protected TKey SelectedValue { get; private set; }
   protected TView View { get; set; }
   public Presenter(TView view)
   {
       View = (TView)view;
       View.SelectionChanged += OnSelectionChangedInternal;
       View.Add += OnAdd;
   }
   void OnSelectionChangedInternal(TKey selectedValue) {
       this.SelectedValue = selectedValue;
       OnSelectionChanged(selectedValue);
   }
   protected abstract void OnSelectionChanged(TKey selectedVaule);
   protected abstract bool OnAdd();
}

// Concrete Views
public interface INotes  : IView<Note, string> {  }
public interface IAttachments : IView<Attachment, string> { }

// Concrete Presenters
public class NotesPresenter : Presenter<INotes, Note, string> { 
   public NotesPresenter(INotes view)  : base(view) { }
   protected override void OnSelectionChanged(string publisherName) { }
   protected override bool OnAdd() { return false; }
}
public class AttachmentsPresenter : Presenter<IAttachments, Attachment, string> {
   public AttachmentsPresenter(IAttachments view) : base(view) { }
   protected override void OnSelectionChanged(string publisherName) { }
   protected override bool OnAdd() { return false; }
}
// Abstract View
public interface IView<TItem, TKey>
{
    void Fill(TItem[] items);
    event SelectEvent<TKey> SelectionChanged;
    event MessageEvent Add;
}

// Event Delegates
public delegate void SelectEvent<TKey>(TKey selectedValue);
public delegate bool MessageEvent();

// Model Entities
public class Note { }
public class Attachment { }

// Abstract Presenter
public abstract class Presenter<TItem, TKey>
{
    protected TKey SelectedValue { get; private set; }
    protected IView<TItem, TKey> View { get; set; }
    public Presenter(IView<TItem, TKey> view)
    {
        View = view;
        View.SelectionChanged += OnSelectionChangedInternal;
        View.Add += OnAdd;
    }
    void OnSelectionChangedInternal(TKey selectedValue)
    {
        this.SelectedValue = selectedValue;
        OnSelectionChanged(selectedValue);
    }
    protected abstract void OnSelectionChanged(TKey selectedVaule);
    protected abstract bool OnAdd();
}

// Concrete Views
public interface INotes : IView<Note, string> { }
public interface IAttachments : IView<Attachment, string> { }

// Concrete Presenters
public class NotesPresenter : Presenter<Note, string>
{
    public NotesPresenter(INotes view) : base(view) { }
    protected override void OnSelectionChanged(string publisherName) { }
    protected override bool OnAdd() { return false; }
}

public class AttachmentsPresenter : Presenter<Attachment, string>
{
    public AttachmentsPresenter(IAttachments view) : base(view) { }
    protected override void OnSelectionChanged(string publisherName) { }
    protected override bool OnAdd() { return false; }
}