In a domain model we have behavior (and business logic) within our business objects. In contrast with the Manager (Service) Model our business objects have no behavior and no business logic (except possibly validation logic). A “Manager” class provides the behavior and applies the business rules. Let's take a look at an example of each:
Domain Model:
public class Customer {
// properties
public string FirstName;
public string LastName;
// methods
public bool Save();
public bool Delete();
public Customer Get(int id);
public CustomerCollection Find(Query aQuery);
}
Vs.
Manager Model
public class Customer {
// properties
public string FirstName;
public string LastName;
} // Note: no behavior
public class CustomerManager {
public bool SaveCustomer(Customer aCustomer);
public bool DeleteCustomer(Customer aCustomer);
public Customer GetCustomer(int customerID);
public CustomerCollection Find(Query aQuery);
}
As you can see using the Manager Model introduces another class into the mix that performs all the operations on your business objects. An important thing to note is that the Manager Model and Domain Model are not mutually exclusive. We could very easily have this:
public class Customer {
// properties
public string FirstName;
public string LastName;
public bool Save();
public bool Delete();
public Customer Get(int id);
public CustomerCollection Find(Query aQuery);
}
public class CustomerManager {
public bool SaveCustomer(Customer aCustomer) {
// do pre save stuff, apply aspects, etc.
aCustomer.Save();
// do post save stuff, apply aspects, etc.
}
public bool DeleteCustomer(Customer aCustomer) {
// do pre delete stuff, apply aspects, etc.
aCustomer.Delete();
// do post delete stuff, apply aspects, etc.
}
public Customer GetCustomer(int customerID) { // etc. }
public CustomerCollection Find(Query aQuery) { // etc.}
}
Essentially with this approach the Manager (Service) object is working as a facade. This approach is often beneficial when you have a significant investment in the domain model and want to add a service layer on top. Rather then just targeting the Customer, the service would likely be broader in scope and include other methods for handling other types of objects. The scope that the service covers will depend on the application and the anticipated use of the objects within your application.
How else are people using the Manager model and Domain model together. When does it work? When doesn't it? Anyone else care to shed some light on how they define each?