Is it possible to eliminate duplicate type parameters in this example?

I have an MVP implementation for a list of items from a domain model.

My question in general is how (or can I) get rid of duplicate type parameters in a particular view and presenter?

I tend to think that I stumbled upon a limitation and should either live with duplication or redesign the classes. Another option: I am missing something obvious. Anyway, that's why I am asking here.

In particular, if you have:

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

      

Can I get compiled to deduce parameters like T2 and T3, resulting in something like:

interface IB : IA<int, string>
class D : C<IB> { } // can't do this

      

This compiles. Duplicate parameters are Note, string

and Attachment, string

.

// 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; }
}

      

enter image description here

+3


source to share


1 answer


Minor change for your host. Print your opinion instead. Since your concrete views are implementing the correct interface, it works. See code below.



// 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; }
}

      

0


source







All Articles