In the last essay of the Advancing Enterprise DDD series, we wrapped up our discussion on immutability, as well as the technical discussion overall. In this essay, we wrap up the series by reflecting on what we've learned, and discussing how to apply these lessons as we move forward.
Showing posts with label case classes. Show all posts
Showing posts with label case classes. Show all posts
2015-05-31
2015-05-24
Advancing Enterprise DDD - Entities, Value Objects, and Identity
In the previous essay of the Advancing Enterprise DDD series, we saw how we might improve our Domain Driven Design model by making our entity classes immutable. Here, we continue to investigate immutable entities by seeing how this affects one of the core DDD building blocks: the value object.
2015-05-17
Advancing Enterprise DDD - Migrating to Immutability
In the previous essay of the Advancing Enterprise DDD series, we saw difficulties emerge in maintaining intra-aggregate constraints. These difficulties arose due to the mutability of our entities, and the Java collections they use to maintain relationships between entities. We also saw that it would be difficult or impossible to use immutable alternatives within JPA. In this essay, we set aside the constraints of JPA for a moment, and imagine a world where entities are composed of immutable objects.
2011-07-01
Scala Enums: Real Life Example
I wanted to follow up my previous post on Scala Enums with a real-life example. The beauty of this example is my ability to use advanced typing techniques such as type parameters, mixin traits, even a composite pattern, in what basically amounts to a simple enumeration type. This code is quite easy to read, and perfectly conveys my intentions. I don't even want to imagine what I would have done in Java for something like this.
2011-06-27
Scala Enums
A post to the scala-users mailing list was asking about how to do enums. While Java enums are relatively lame, they allow me to do simple things such as attach properties to enumerated values, e.g.:
public enum ColumnHeader {
FirstName("First Name", true),
LastName("Last Name", true),
Age("Age", false);
private final String headerText;
private final boolean visibleByDefault;
private ColumnHeader(headerText, visibleByDefault) {
this.headerText = headerText;
this.visibleByDefault = visibleByDefault;
}
public String getHeaderText() {
return headerText;
}
public boolean isVisibleByDefault() {
return visibleByDefault;
}
}
Java provides a static method values() in your enum class that returns an array of your enum values, in the order you defined them. This is nice. Now you can do things like this:
public enum ColumnHeader {
//...
public static ColumnHeader[] getVisibleByDefaultColumnHeaders() {
final List<ColumnHeader> visibleColumnHeaders =
new ArrayList<ColumnHeader>();
for (ColumnHeader header: values()) {
if (header.isVisibleByDefault()) {
visibleColumnHeaders.add(header);
}
}
return visibleColumnHeaders.toArray(
new ColumnHeader[visibleColumnHeaders.size());
}
}
People switching from Java to Scala may want to do something like this, and may well find themselves expecting something similar from the scala.Enumeration class. But it's pretty hard to figure out how to extend scala.Enumeration and add properties to the extended class as well. I've tried a couple times, and I haven't figured it out. Instead, I've taken to using case objects to solve the kinds of problems that I solved with enums in Java.
public enum ColumnHeader {
FirstName("First Name", true),
LastName("Last Name", true),
Age("Age", false);
private final String headerText;
private final boolean visibleByDefault;
private ColumnHeader(headerText, visibleByDefault) {
this.headerText = headerText;
this.visibleByDefault = visibleByDefault;
}
public String getHeaderText() {
return headerText;
}
public boolean isVisibleByDefault() {
return visibleByDefault;
}
}
Java provides a static method values() in your enum class that returns an array of your enum values, in the order you defined them. This is nice. Now you can do things like this:
public enum ColumnHeader {
//...
public static ColumnHeader[] getVisibleByDefaultColumnHeaders() {
final List<ColumnHeader> visibleColumnHeaders =
new ArrayList<ColumnHeader>();
for (ColumnHeader header: values()) {
if (header.isVisibleByDefault()) {
visibleColumnHeaders.add(header);
}
}
return visibleColumnHeaders.toArray(
new ColumnHeader[visibleColumnHeaders.size());
}
}
People switching from Java to Scala may want to do something like this, and may well find themselves expecting something similar from the scala.Enumeration class. But it's pretty hard to figure out how to extend scala.Enumeration and add properties to the extended class as well. I've tried a couple times, and I haven't figured it out. Instead, I've taken to using case objects to solve the kinds of problems that I solved with enums in Java.
Subscribe to:
Posts (Atom)