3

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?

asked Jul 3, 2012 at 11:43
2
  • 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. Commented Jul 3, 2012 at 11:48
  • @Kristof Claes, I have updated my question and added some example code. Commented Jul 3, 2012 at 11:56

2 Answers 2

9

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.

answered Jul 3, 2012 at 12:08
1
  • 1
    and don't call all those functions in the constructor, move them to an Initialize method instead Commented Jul 3, 2012 at 18:35
0

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.

gnat
20.5k29 gold badges117 silver badges308 bronze badges
answered Jul 3, 2012 at 12:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.