1

I was just curious if I should put my age method into my POCO class or leave this method in my business layer.

Right now I have 4 layers:

  • BusinessObject(BO)
  • Data Acess Layer(DAL)
  • UserInterface(UI)
  • Service.

Right now I want to query users with age older than 13 years old. So I am debating if should have a method in my POCO class in BO which return the current age of users, or should I query the DOB from my business layer as follow

BO

public class Person:Base
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public DateTime DOB { get; set; }

    public int Age
    {
        get
        {
            DateTime today = DateTime.Today;
            int age = today.Year - DOB.Year;
            return age;   
        }
    }
}

Business Layer

public class PersonBLL
{
        private PersonDAL db = null;

        public PersonBLL()
        {
            db = new PersonDAL(new AppContext());
        }
        public IEnumerable<Person> PersonGEAge(int age)
        {
            DateTime td = DateTime.Now;
            DateTime birthday = DateTime.Now;
            DateTime date = new DateTime(birthday.Year - age, td.Month, td.Day, 4, 5, 6);
            return db.PersonDOB(date);
        }
    }
jgauffin
  • 99,844
  • 45
  • 235
  • 372
Jseb
  • 1,926
  • 4
  • 29
  • 51
  • 2
    Your age calculation is wrong! Please see http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c – Bas May 18 '16 at 15:47
  • 1
    This question seems more like a core review, it is probably better asked over on http://codereview.stackexchange.com – David Pine May 18 '16 at 15:48
  • @DavidPine: The question is not about the code, but layer & entity design. – jgauffin May 20 '16 at 05:24

1 Answers1

3

You should query the business layer (through the service) to get all business entities which have the specified age.

Your service layer can however return DTOs which contain a Age property instead of DateOfBirth

In other words: Your service can work with business entities, while it specialize the objects it return to the UI. It's done to decouple the information schema within your business domain from the information schema in the UI (which can be aggregations of business entities or partial information from a specific entity).

UIs tend to have a higher amount of changes than the backend when it come to the structure of information. By doing the suggested seperation you are not forced to remodel the business domain for every change in the UI. Instead you just change how the service put together the information that the UI require.

jgauffin
  • 99,844
  • 45
  • 235
  • 372