Class design - return list <object> from <object>

Given a simple class:

public class Person 
{
    public string FirstName;
    public string LastName;

    public string GetFullName() 
    {
     return FirstName + LastName;
    }
}

A user of this class will populate an object List<Person>by reading an Xml file or other data source. Should the list filling logic be in the Person class or remain in the calling class? In other words, should there be a method public List<Persons> GetPersons()in the Person class or in the calling class? Or should access data in another class at all?

I know this is a pretty simple question, but I'm just wondering how others usually do it.

+3
source share
2 answers

, ? , Person XML , ? Person. , - "IPeopleRetriever" -:

public interface IPeopleRetriever
{
   IEnumerable<Person> GetPeople();
}

XMLPeopleRetriever:

public class XMLPeopleRetriever : IPeopleRetriever
{
   public IEnumerable<Person> GetPeople() { ... }
}

, IPeopleRetriever , .

+4

. , , "" Person. , List<T> T. . , .

public class Node()
{
    public List<Node> GetChildNodes()
    {
        //etc...
    }
}

, , List<T>. . - , Person, .

0

Source: https://habr.com/ru/post/1741370/


All Articles