在接口实现的接口属性

在接口实现的接口属性

本文介绍了在接口实现的接口属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code碱基我想用这两个一个ASP.NET MVC和WPF / MVVM前端。业务对象实现为接口和业务逻辑使用这些接口传递数据。

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.

说我有实现IEnumerable接口,我在一个属性上。是否有可能有不同版本的界面使用的IEnumerable的不同实现?什么,我试图完成一个例子:

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

可以这样做?

编辑:我也应该问怎么了,因为当我尝试这一点,我得到的错误与此类似:

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

MvcReportParams不实现接口成员'IReportParams.SelectedStatuses。 MvcReportParams.SelectedStatuses无法实现IReportParams.SelectedStatuses',因为它不具有匹配的返回类型System.Collections.Generic.IEnumerable

'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和列表两种完全实现IEnumerable,所以这似乎没有道理给我。

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

最后修改

嗯,有人贴出了答案,但他们删除它由于某种原因,所以我不能将它标记为解决方案。下面是我落得这样做:

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

这是样板code多行来写,但我喜欢更明确的反正。

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

推荐答案

我可能会用只读属性实现这一点。它更好地初步实践为客户端添加/删除/更新项目现有的集合中,而不是完全取代的集合。

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

这篇关于在接口实现的接口属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:04