Implementing interface properties in interfaces?

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

I have a code base I want to use for both an ASP.NET MVC and a WPF/MVVM front end. The business objects are implement as interfaces and the business logic uses these interfaces for passing data.

Say I have a property on my interface that implements IEnumerable. Is it possible to have different versions of this interface use different implementations of IEnumerable? An example of what I am trying to accomplish:

class Reporting {     public bool RunReport(IReportParams Params); }  interface IReportParams {     IEnumerable<Guid> SelectedItems { get; }     IEnumerable<StatusEnum> SelectedStatuses { get; } }  class MvcReportParams : IReportParams {     public List<Guid> SelectedItems { get; set; }     public List<StatusEnum> SelectedStatuses { get; set; } }  class WpfReportParams : IReportParams {     public ObservableCollection<Guid> SelectedItems { get; set; }     public ObservableCollection<StatusEnum> SelectedStatuses { get; set; } } 

Can this be done?

Edit: I should also have asked "how", because when I try this, I get errors similar to this:

'MvcReportParams' does not implement interface member 'IReportParams.SelectedStatuses'. 'MvcReportParams.SelectedStatuses' cannot implement 'IReportParams.SelectedStatuses' because it does not have the matching return type of 'System.Collections.Generic.IEnumerable'

ObservableCollection and List both absolutely implement IEnumerable, and so this does not seem to make sense to me.

Last Edit

Well, someone posted the answer, but they deleted it for some reason, so I can't mark it as the solution. Here's what I ended up doing:

interface IReportParams {     IEnumerable<Guid> SelectedItems { get; }     IEnumerable<StatusEnum> SelectedStatuses { get; } }  class MvcReportParams : IReportParams {     // Properties that the modelbinder dims and sets up     public List<Guid> SelectedItems { get; set; }     public List<StatusEnum> SelectedStatuses { get; set; }      // Explicityly implement my interface     IEnumerable<Guid> IReportParams.SelectedItems     {         get { return SelectedItems; }     }      IEnumerable<StatusEnum> IReportParams.SelectedStatuses     {         get { return SelectedStatuses; }     } }  class WpfReportParams : IReportParams {     // Properties that my view dims and modifies     public ObservableCollection<Guid> SelectedItems { get; set; }     public ObservableCollection<StatusEnum> SelectedStatuses { get; set; }      // Explicityly implement my interface     IEnumerable<Guid> IReportParams.SelectedItems     {         get { return SelectedItems; }     }      IEnumerable<StatusEnum> IReportParams.SelectedStatuses     {         get { return SelectedStatuses; }     } } 

It's more lines of boilerplate code to write, but I like being more explicit anyways.

回答1:

I would probably implement this with read-only properties. It better pratice for the client to add/remove/update items in an existing collection rather than replace the collection altogether.

interface IReportParams {     IEnumerable<Guid> SelectedItems { get; }     IEnumerable<StatusEnum> SelectedStatuses { get; } }  class MvcReportParams : IReportParams {     public MvcReportParams()     {         SelectedItems = new Collection<Guid>();         SelectedStatuses = new Collection<StatusEnum>();     }      public IEnumerable<Guid> SelectedItems { get; private set; }     public IEnumerable<StatusEnum> SelectedStatuses { get; private set; } }  class WpfReportParams : IReportParams {     public WpfReportParams()     {         SelectedItems = new ObservableCollection<Guid>();         SelectedStatuses = new ObservableCollection<StatusEnum>();     }      public IEnumerable<Guid> SelectedItems { get; private set; }     public IEnumerable<StatusEnum> SelectedStatuses { get; private set; } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!