C# 接口返回类型

发布于 2024-12-08 15:10:30 字数 122 浏览 4 评论 0原文

我有几个界面:IOrganizations 和 IPeople。

如果我想返回与某个组织相关的人员列表,我应该在哪个界面中创建它?

结果基于组织,但我返回人员列表

I have a couple of interfaces, IOrganisations and IPeople.

If I want to return a list of people associated with an organisation, which interface do I create this in?

The results are based on an organisation, yet I'm returning a list of people

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜味拾荒者 2024-12-15 15:10:30

IOrganization 上执行此操作(并使用 IPeople 作为返回类型)。

类似这样:

interface IOrganisation {
    IList<IPeople> GetPeople();
}

这是因为组织的人员是组织的财产,而不是人民的财产。

Do it on IOrganisation(and have IPeople as the return type).

Something like this:

interface IOrganisation {
    IList<IPeople> GetPeople();
}

This is because the people of an organisation is a property of the organisation, not of the people.

疯狂的代价 2024-12-15 15:10:30

听起来它应该在 IOrganization 上 - 毕竟,这就是您将调用它的地方。您拥有的是组织,您想要人员列表 - 您无法从人员列表开始,因此它 >必须IOrganization接口的一部分。

我发现以复数接口名称开头很奇怪 - 我希望为单个组织使用IOrganization,为单个人使用IPerson

public interface IOrganization {
    IList<IPerson> GetPeople();
}

That sounds like it should be on IOrganization - that's what you'll call it on, after all. What you have is the organization, you want the list of people - you can't start fromt the list of people, so it must be part of the IOrganization interface.

I find it strange to have plural interface names to start with - I'd expect IOrganization for a single organization, and IPerson for a single person.

public interface IOrganization {
    IList<IPerson> GetPeople();
}
财迷小姐 2024-12-15 15:10:30

首先,您可能不应该有复数的接口名称。因此,将它们称为 IOrganization 和 IPerson,然后在您的 IOrganization 界面上,您可能有一个名为的方法:

IEnumerable<IPerson> GetPeople()

First of all, your should probably not have pluralized interface names. So call them IOrganization and IPerson and then on your IOrganisation interface you might have a method called:

IEnumerable<IPerson> GetPeople()
伴梦长久 2024-12-15 15:10:30

哪个属于哪个?

在我看来,iOrganization.People 将是属于该组织的人员列表(您的“IPeople”界面是代表人员列表还是单个人?)。同样,您可能有一个IPeople.Organizations,其中列出了一个人所属的所有组织。两者可能都有不同的底层机制 - 也许调用相同的查找表 - 这对于调用代码的人来说并不重要。

Which belongs to which?

It seems to me that iOrganisation.People would be a list of people ( is your "IPeople" interface representing a list of people or a single person? ) belonging to the organisation. Likewise you might have an IPeople.Organisations that lists all organisations that a person belongs to. Both will probably have a different underlying mechanism - maybe calling the same lookup table - it doesn't matter to the person calling the code.

当梦初醒 2024-12-15 15:10:30

如果您将组织视为包含人员的集合,那么将其放入 IOrganization 中是很直观的。它看起来像这样:

interface IOrganization
{
  IList<IPeople> Members { get; }
}

If you think of an organization as a collection containing people, then it's intuitive to put that in IOrganization. It would look like this:

interface IOrganization
{
  IList<IPeople> Members { get; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文