Can a Boundary Class interact with an Entity Class? - uml

For instance: link
Is the above Ok or it would be better to create more methods in Controller that handle the data that are sent/retrieved without the interaction between UI and the Entity?
In general when is it allowed (if it is) for a Boundary Class to interact with an Entity Class?

Depends on whether you want/need to stick religiously to the Boundary-Control-Entity pattern:
If yes (you do need to stick to the pattern): then no, the Boundary object can only speak to Control objects. See table at bottom of this page (also a good description of pattern).
If no: then yes it can!
That's not meant to be glib. It's questionable whether such strict separation is good design practice. It looks nice in pictures: Boundary, Control and Entity in nice horizontal layers with messages passing adjacent layers only.
The reality is rather different. Strict separation can lead to two problems:
A proliferation of pass-through methods. You allude to this. You end up with an amalgamation of methods on the controllers that do nothing more than pass through to the underlying entities.
Anaemic Entity classes. Rather than the Entities being home for data + behaviour, they become data containers only with all behaviour migrating out to controllers. That's not a good thing.
It's notable that in Domain Driven Design, Eric Evans recommends creating Services (akin to Controllers) only when the logic in question doesn't have a viable home in any of the Domain Classes (Entities).

Related

Is it possible to join 2 controller classes to 1 boundary class?

I'm doing a college project where I'm required to create a class diagram to a school registration system. There I have included a Student class, a registrationUI Boundary Class and a registrationController controller class:
I have created a separate controller class called systemController to do the other tasks such as calculate bill, etc...
I'm asking if it's possible to join the systemController class with the registrationUI class.
ECB architectural pattern
It can be useful to realize that the ECB architecture pattern originates from the use case models:
a controller represent a use case (e.g. RegistrationControler for use case "Register Student"). A controller is linked to all the entites that are involved in the use case (can be several, e.g. Registration, Student and Course)
boundaries connect the use case with the involved external actors (e.g. RegistrationUI offers the user interface to the registration manager, or to a student if it's a self service system). So several boundaries can be linked to a controler (for example if a secondary actor is involved such as a third party system).
entities represent domain objects (e.g. Student). So an entity can be linked to several other related entities (e.g. Registration record for the Student in a Course)
Consistency check
At the bottom of this article or that article you'll see a short matrix showing the possible relations between entities, controls and boundaries.
According to this logic, entities [should never be directly connected to a boundary. So your Access relation between Student and RegistrationUI is not a good idea (ECB is not MVC).
One boundary and two controllers ?
If you follow Jacobson's OOSE logic of decomposing a use case into boundaries and controllers, or if applying a basic step by step robustness analysis in a use case driven modeling approach, you'd identify the controllers (use cases) and create a boundary for each link between an actor and the use case. So at first sight, one could think that a boundary can be linked to at most one controller.
But you there are also "included" use cases or "extended" use cases. These are not connected directly to an actor, at least not explicitly in the graphic. This means that you could very well have one boundaries related to several controllers. In this tutorial you have a very nice ATM example with one boundary and several ATM transactions. In the DDJ article link above, you also have a similar example.
P.S.: Personally, I'm not quite sure what you want to achieve with the systemController. I suggest that you think about its role and its name. Looking at it's content, I could imagine that it's a part of the RegistrationController. Bt I could also imagine that it's a dispatcher launching the other controllers.

What is the use of control classes?

I am trying to understand how to classify the classes as boundary/control/entity classes. I can understand boundary and entity classes although my understanding may not beperfect. Boundary is the classes which interacts with the user. So the classes used for the userinterface will be boundary classes. Entity class handles data. So entities I use in the ER diagram will be entity classes. But I understand nothing why control object is used. It is said that control object is used to encapsulate domain functionalities. What if the control classes are not used. Can you please explain me with example.I found some explaination but I am still confused.Why boundary should not interact directly with entity? There are also classes which are not boundary/control/entity. What are they?
Background
The Entity/Boundary/Control approach was introduced by Ivar Jacobson in 1992 as part of his use case driven Objectory development method.
At that time Jacobson used the terminology Entity/Interface/Control. The strange circle notation that you can find in relaction with ECB was already used in his books in 1992 and in 1994. By the way, the use case of his methods were integrated into UML and his development process was merged into RUP, when Rational acquired Objectory.
The idea behind his method was to adopt a very logic and formal and deductive analysis and design approach. It starts with identifying the systems behavioral requirement with use cases. Each link of use case to the outside world would then be represented as an interface object responsible for encapsulating completely the user interface.
Each use case would be represented as one or several control objects:
Control object: An object that encapsulates functionality of one or
several use cases - I.Jacobson in The Object Advantage, ACM Press, 1994
Finally the business objects managed by the system can be partly inferred from the use cases, and during the analysis.
Additional information
The fundaments of the Iconix process were introduced in 1999 as part of the book "Use Case Driven Object Modeling with UML" by Rosenberg & Stephen. Some additional robustness constraints were introduced, certainly to improve separation of concerns. For example, the direct link between entity and boundary is prohibited. Everything has to be channelled through control objects:
Control objects (which we usually call controllers because they often
aren't real objects), serve as the "glue" between boundary objects and
entity objects - D.Rosenberg, in the linked DDJ article.
They add a recommendation to clarify the intent :
Both boundary objects and entity objects are nouns, and controllers
are verbs.
Conclusion
So the principle is that the control object represents the business logic offered by use cases, interacting on one side with the boundaries, and on the other side with the entities. Control objects can't be invoked/accessed directly by the outside world.
If you would want to avoid the control objects, you would have a boundary objects with methods corresponding to the verbs/functions/use-cases that your system is supposed to provide. This wouldn't be according to the modern ECB, but perfectly valid according to Jacobson's original approach. Nevertheless your boundary would no longer comply with the single responsibility principle of a SOLID design.
Boundary interact with actors (e.g. users).
Entity classes represent data.
Control mediates between the boundary and the entities (e.g. executes an operation on the entities)
Source: http://www.cs.sjsu.edu/~pearce/modules/patterns/enterprise/ecb/ecb.htm
The control classes contain the business logic. It's the most important part of a system. While the boundary just controls whether the text is green or blue (very basically) and the entities control whether data is stored in text files or databases (also very basically) the control classes do all the business logic. What to change in the entity when the boundary send mouse/keyboard events an vice verse what to show from the entities in the boundary.

Entity/Component concepts of GameplayKit

I am designing my game with Entity/Component concepts of GameplayKit in iOS 9, for ShootComponent, should define bullet/missile as Entity?
Reason for Yes:
separate logic from its parent, e.g. playerTank or enemyTank;
if not, TankEntity need distinguish whether its bullet collide with other Entities or itself.
Reason for No:
it is not actual entity in logic world, which is fired by my tank or enemy turret;
bullet always be shot and disappeared, so game need add/remove it now and then;
For your comments pls.
Finally decided to define bullet/missile as entity, so it acts as entity in contact test, rendering and other components.
I would have add it as a component for the entity using it.
So you will be able to make any entity fire bullet or missile.
Keep in mind that your entity should only act as a simple reference with no logic in it.
First lets read Adam Martins original description of his terms. It appears Apple got the idea of entities and components from Martin:
Entity: The entity is a general-purpose object. Usually, it only consists of a unique id.
Component: the raw data for one aspect of the object, and how it interacts with the world.
System: "Each System runs continuously (as though each System had its own private thread) and performs global actions on every Entity that possesses a Component or Components that match that Systems query."
Martin is just defining terms for doing compositional design, which is an alternative to inheritance that is more recombinable and flexible.
So entities are what you might recognize as instances of a class, but classes have been stripped of all their data and methods, which has been moved out into components - and the entities just delegate to the components.
So your missile... it would be an instance of a class in normal OO terms - an object, right? And a missile can behave in a variety of ways... it can seek out an enemy, it can fly straight ahead, it can speed up, etc. It also has properties that indicate if it's hit an enemy, properties for its total damage, health, and so on.
So the missile is an entity while these various methods / data would be components of the missile entity.
Martins approach is interesting, and there hasn't been as much focus on compositional design as there has been OO (for what reason I don't really know), so I can see why Apple would adopt it for a game framework like this.
But his ideas don't seem very well fleshed out. For example, usually in compositional design there is a delegation hierarchy, where objects will keep delegating up a chain until some data or method is found. At the top there's one meta-object that everything delegates to. In this way objects are both components and entities - they act as both the delegating and the delegated to. But Martins terms don't support this... his model is flat - there are only entities, and then components that can be added to them, but no delegation between entities and no meta-object.
Maybe he felt this flat design was appropriate for game development. I have my doubts... you seem to want some kind of hierarchical structure of objects. I would look for a way to mix in inheritance, or setup some kind of custom delegation hierarchy where objects could act as both entities and components. You might look to see if this is possible within that framework, or if it isn't just write your own.

How to be with huge Domain classes in DDD?

When you are developing an architecture in OO/DDD style and modeling some domain entity e.g. Order entity you are putting whole logic related to order into Order entity.
But when the application becomes more complicated, Order entity collects more and more logic and this class becomes really huge.
Comparing with anemic model, yes its obviously an anti-pattern, but all that huge logic is separated in different services.
Is it ok to deal with huge domain entities or i understand something wrong?
When you are trying to create rich domain models, focus entities on identity and lifecyle, and thus try to avoid them becoming bloated with either properties or behavior.
Domain services potentially are a place to put behavior, but I tend to see a lot of domain service methods with behavior that would be better assigned to value objects, so I wouldn't start refactoring by moving the behavior to domain services. Domain services tend to work best as straightforward facades/adaptors in front of connections to things outside of the current domain model (i.e. masking infrastructure concerns).
You can also put behavior in Application services, but ask yourself whether that behavior belongs outside of the domain model or not. As a general rule, try to focus application services more on orchestration-style tasks that cross entities, domain services, repositories.
When you encounter a bloated entity then the first thing to do is look for sets of cohesive set of entity properties and related behavior, and make these implicit concepts explicit by extracting them into value objects. The entity can then delegate its behavior to these value objects.
Since we all tend to be more comfortable with entities, try to be more biased towards value objects so that you get the benefits of immutability, encapsulation and composability that value objects provide - moving you towards a more supple design.
Value objects enable you to incorporate a more functional style (eg. side-effect-free functions) into your domain model and thus free up your entities from having to deal with the complexity of adding complicated behavior to the burden of managing identity and lifecycle. See the pattern summaries for entities and value objects in Eric Evan's http://domainlanguage.com/ddd/patterns/ and the Blue Book for more details.
When you are developing an architecture in OO/DDD style and modeling
some domain entity e.g. Order entity you are putting whole logic
related to order into Order entity. But when the application becomes
more complicated, Order entity collects more and more logic and this
class becomes really huge.
Classes that have a tendency to become huge, are often the classes with overlapping responsibilities. Order is a typical example of a class that could have multiple responsibilities and that could play different roles in your application.
Given the context the Order appears in, it might be an Entity with mutable state (i.e. if you're managing Order's commercial condition, during a negotiation phase) but if you're application is managing logistics, an Order might play a different role: and an immutable Value Object might be the best implementation in the logistic context.
Comparing with anemic model, yes its
obviously an anti-pattern, but all that huge logic is separated in
different services.
...and separation is a good thing. :-)
I have got a feeling that the original model is probably data-centric and data serving different purposes (order creation, payment, order fulfillment, order delivery) is piled up in the same container (the Order class). Can't really say it from here, but it's a very frequent pattern. Not all of this data is useful for the same purpose at the same time.
Often, a bloated class like the one you're describing is a smell of a missing separation between Bounded Contexts, and/or an incomplete Aggregate separation within the same bounded context. I'd have a look to:
things that change together;
things that change for the same reason;
information needed to fulfill behavior;
and try to re-define aggregate boundaries accordingly. And also to:
different purposes for the application;
different stakeholders;
different implicit models/languages;
when it comes to discover the involved contexts.
In a large application you might have more than one model, thus leading to more than a single representation of a single domain concept, at least for concepts that are playing many roles.
This is complementary to Paul's approach.
It's fine to use services in DDD. You will commonly see services at the Domain, Application or Infrastructure layers.
Eric uses these guidelines in his book for spotting when to use services:
The operation relates to a domain concept that is not a natural part of an ENTITY or VALUE OBJECT.
The interface is defined in terms of other elements in the domain model
The operation is stateless

Can a "rich domain model" violate the Single Responsibility Principle?

An interesting thread came up when I typed in this question just now. I don't think it answers my question though.
I've been working a lot with .NET MVC3, where it's desirable to have an anemic model. View models and edit models are best off as dumb data containers that you can just pass off from a controller to a view. Any kind of application flow should come from the controllers, and the views handle UI concerns. In MVC, we don't want any behavior in the model.
However we don't want any business logic in the controllers either. For larger applications its best to keep the domain code separate from and independent of the models, views, and controllers (and HTTP in general for that matter). So there is a separate project which provides, before anything else, a domain model (with entities and value objects, composed into aggregates according to DDD).
I've made a few attempts to move away from an anemic model towards a richer one in domain code, and I'm thinking of giving up. It seems to me like having entity classes which both contain data and behavior violates the SRP.
Take for example one very common scenario on the web, composing emails. Given some event, it is the domain's responsibility to compose an EmailMessage object given an EmailTemplate, EmailAddress, and custom values. The template exists as an entity with properties, and custom values are provided as input by the user. Let's also say for the sake of argument that the EmailMessage's FROM address can be provided by an external service (IConfigurationManager.DefaultFromMailAddress). Given those requirements, it seems like a rich domain model could give the EmailTemplate the responsibility of composing the EmailMessage:
public class EmailTemplate
{
public EmailMessage ComposeMessageTo(EmailAddress to,
IDictionary<string, string> customValues, IConfigurationManager config)
{
var emailMessage = new EmailMessage(); // internal constructor
// extension method
emailMessage.Body = this.BodyFormat.ApplyCustomValues(customValues);
emailMessage.From = this.From ?? config.DefaultFromMailAddress;
// bla bla bla
return emailMessage;
}
}
This was one of my attempts at rich domain model. After adding this method though, it was the EmailTemplate's responsibility to both contain the entity data properties and compose messages. It was about 15 lines long, and seemed to distract the class from what it really means to be an EmailTemplate -- which, IMO, is to just store data (subject format, body format, attachments, and optional from/reply-to addresses).
I ended up refactoring this method into a dedicated class who's sole responsibility is composing an EmailMessage given the previous arguments, and I am much happier with it. In fact, I'm starting to prefer anemic domains because it helps me keep responsibilities separate, making classes and unit tests shorter, more concise, and more focused. It seems that making entities and other data objects "devoid of behavior" can be good for separating responsibility. Or am I off track here?
The argument in favor of a rich domain model instead of an anemic model hinges on one of the value propositions of OOP, which is keeping behavior and data next to each other. The core benefit is that of encapsulation and cohesion which assists in reasoning about the code. A rich domain model can also be viewed as an instance of the information expert pattern. The value of all of these patterns however is to a great extent subjective. If it is more helpful for you to keep data and behavior separate, then so be it, though you might also consider other people that will be looking at the code. I prefer to encapsulate as much as I can. The other benefit of a richer domain model in this case would be the possibility to make certain properties private. If a property is only used by one method on the class, why make it public?
Whether a rich domain model violates SRP depends on your definition of responsibility. According to SRP, a responsibility is a reason to change, which itself calls for a definition. This definition will generally depend on the use-case at hand. You can declare that the responsibility of the template class is to be a template, with all of the implications that arise, one of which is generating a message from the template. A change in one of the template properties can affect the ComposeMessageTo method, which indicates that perhaps these are a single responsibility. Moreover, the ComposeMessageTo method is the most interesting part of the template. Clients of the template don't care how the method is implemented or what properties are present of the template class. They only want to generate a message based on the template. This also votes in favor of keeping data next to the method.
Well, it depends on how you want to look at it.
Another way is: "Can the Single Responsibility Principle violate a rich domain model?"
Both are guidelines. There is no "principle" anywhere in software design. There are, however, good designs and bad designs. Both these concepts can be used in different ways, to achieve a good design.

Resources