Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have created a small application which has a three tier architecture and I have business object classes to represent entities such as User, Orders, UserType etc. In these classes I have methods that are executed when the Constuctor method of, for example, User is called. These methods perform calculations and generate details that setup data for attributes that are part of each User object.

Here is the structure for the project in Visual Studio:

enter image description here

Here is some code from the business object class User.cs:

Public Class User
{
    public string Name { get; set; }
    public int RandomNumber { get; set; }
    etc

        public User
        {
            Name = GetName();
            RandomNumber = GetRandomNumber();
        }

        public string GetName()
        { 
          ....
          return name;
        }

        public int GetRandomNumber()
        {
           ...
           return randomNumber;
        }

    }

Should this logic be included in the Business Object classes or should it be included in a Utilities class of some kind? Or in the business rules?

share|improve this question
1  
Is it possible to show some code from the User, Users, UserData and UserUtilities classes? It's hard to answer your questions based on classnames alone. –  Kristof Claes Jul 3 '12 at 11:48
    
@Kristof Claes, I have updated my question and added some example code. –  Theomax Jul 3 '12 at 11:56
add comment

2 Answers

up vote 8 down vote accepted

The theory:
A class should be responsible for its data and know what to do with its data. Any methods that the class needs to enforce that responsibility should be within the class.

In Practice:
So the class will have User.Name and it should also have User.GetName(), User.GetFirstName(), User.GetLastName() as Name belongs to the User and the User should know how to manipulate its Name property.
You should not have a UserUtils.GetFirstNameFromName() for example.

Let's also say that we have User.TaxID and there should be appropriate get / set statements in order to retrieve that property. However, we should not have User.CreateTaxID as the User class wouldn't / shouldn't have anything to do with getting a foreign identifier to the class.
Instead, this is a great case for UserUtils.CreateTaxID.

So the answer to your question is both "yes" and "no" depending upon what the functions are actually doing.

share|improve this answer
    
and don't call all those functions in the constructor, move them to an Initialize method instead –  Steven A. Lowe Jul 3 '12 at 18:35
add comment

If you want follow the OO principles, the answer to this question is yes. An object is responsible to manage its state and behaviour. This means that all the property and methods that describe the object should be implemented in the the base class.

This is a written rule, a theory.

Form the practical side, you can decide to split classes for many reasons:

  • Personal organization
  • Readability
  • Avoid huge classes

This is up to you.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.