C# Interface Return Types
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
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.
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();
}
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()
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.
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; }
}
精彩评论