Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to separate concerns for this code?

In a previous question one of the comments from Dr. Herbie on the accepted answer was that my method was performing two responsibilities..that of changing data and saving data.

What I'm trying to figure out is the best way to separate these concerns in my situation.

Carrying on with my example of having a Policy object which is retrieved via NHibernate....

The way I'm currently setting the policy to inactive is as follows:

Policy policy = new Policy();
policy.Status = Active;

policyManager.Inactivate(policy);

//method in PolicyManager which has data access and update responsibility
public void Inactivate(Policy policy)
{
    policy.Status = Inactive;
    Update(policy);
}

If I were to separate the responsibility of data access and data update what would be the best way to go about it?

Is it better to have the PolicyManager (which acts as the gateway to the dao) manage the state of the Policy object:

Policy policy = new Policy();
policy.Status = Active;

policyManager.Inactivate(policy);
policyManager.Update(policy);

//method in PolicyManager
public void Inactivate(Policy policy)
{
    policy.Status = Inactive;
}

Or to have the Policy object maintain it's own state and then use the manager class to save the information to the database:

Policy policy = new Policy();
policy.Status = Active;

policy.Inactivate();

policyManager.Update(policy);

//method in Policy
public void Inactivate()
{
    this.Status = Inactive;
}
like image 695
mezoid Avatar asked Sep 15 '09 07:09

mezoid


People also ask

What is separation of concerns in coding?

Separation of concerns is a principle used in programming to separate an application into units, with minimal overlapping between the functions of the individual units. The separation of concerns is achieved using modularization, encapsulation and arrangement in software layers.

What is separation of concerns HTML?

Separation of concerns is the idea that each module or layer in an application should only be responsible for one thing and should not contain code that deals with other things. Separating concerns reduces code complexity by breaking a large application down into many smaller units of encapsulated functionality.

How do you do a separation of concerns?

Separation of concerns is a software architecture design pattern/principle for separating an application into distinct sections, so each section addresses a separate concern. At its essence, Separation of concerns is about order.

Which one is a good example of separation of concerns?

Common examples include separating a space into rooms, so that activity in one room does not affect people in other rooms, and keeping the stove on one circuit and the lights on another, so that overload by the stove does not turn the lights off.


1 Answers

What I would do:

  • Create a repository which saves and retrieves Policies. (PolicyRepository)

  • If you have complex logic that must be performed to activate / deactivate a policy, you could create a Service for that. If that service needs access to the database, then you can pass a PolicyRepository to it, if necessary. If no complex logic is involved, and activating / deactivating a policy is just a matter of setting a flag to false or true, or if only members of the policy class are involved, then why is 'Activated' not a simple property of the Policy class which you can set to false / true ? I would only create a service, if other objects are involved, or if DB access is required to activate or deactivate a policy.

like image 105
Frederik Gheysels Avatar answered Sep 21 '22 17:09

Frederik Gheysels