Domain Driven Design - How to use value object on the UI - java

I was wondering how you use your ddd model within a web application. Within Eric Evan Cargo application there's the Cargo class which contains the value object Itinerary. Within Itinerary is a collection of Legs, again a value object. All value objects hide the surrogate id to the outside world.
So when using this domain model, how would I create a web app, where you can click on a cargo itinerary, list all legs and then show the details of a leg by redirecting to a new "leg detail" page. Usually I would pass the LegId within the query fields and read it out again on the detail page. But since it has no id, how would you do that?
Using the index of a leg which might change when the collection gets sorted?
Passing all values within the query fields since this is the value object identity?
Sounds like a step backwards to me :)

If the leg has no id, the only way you have to refer to it is through the Cargo, which has an identity, and therefore can be associated with a URI/URL. To refer to a specific leg, you have only the index, which can be a number, or a dictionary key. If you have sorting issues, you can define two lists: one with the canonical ordering for reference purposes, and another with the ordering, mapping order position and canonical index.
As for the reason why the value objects in Evans' example have ids, I think it's for serialization purposes.
Of course, you can also opt for a Itinerary/Leg with identity.

Related

Can not understand the sequence of method and constructor

According to sequence diagram I should create firstly method "regisreItem(Item item)" with argument "item" as an object. I see my problem that the constructor for "items" is called after the method "regisreItem(Item item)" so that I have nothing to insert into "regisreItem(Item item)" method according sequence diagramm. Or not ?
Sequence diagram
Class diagram
Here is a part of sequence diagram i am interested in
https://drive.google.com/open?id=1eJolWNoN32IubP3iaaXPc_cLM5Es08hK
Here is all my sort of code.
Please provide me some sort of code ho is it possible to implement.
And clarify the beginning of sequence diagram.
Since the operation registerItem expects an item as parameter, the Auctioneer object needs to create it, before calling the operation. That means the Auctioneer has to send a create message, not the Auction (using new Item() as a parameter is not possible in a sequence diagram - and it doesn’t change the creator anyway). i1 and i2 are attributes of the interaction. They can be used as parameters of registerItem.
addBid also expects a bidder. Again the attributes Max and Moritzof the interaction should be used here.
In a real program these interaction attributes would be temporary variables of the Auction::addBid operation or of the Auctioneer. The Auctioneer is probably not supposed to have variables, therefore the registerItem Operation should probably only have generic data types such as stringas parameters.
The Auction is supposed to send messages to i1 and i2, however, since these are attributes of the interaction, the Auction object does not know them. It’s Ok to omit this detail, but it would be better to show how the Auction finds the relevant Item, for example with a findItemByName Operation called on itself.
A better alternative is, to let the Auction send the messages to its own attribute allItems. Then two lifelines would represent the same attribute, but with different objects. A selector could be used to distinguish between the two objects in the slot defined by this attribute (allItems[0], allItems[1], this is optional). The same is applicable to allBids instead of b300EUR and the like.
You can get around the issue of the Item constructor being called after registerItem by using:
registerItem(new Item(...));
and passing in the attributes of Item i1 and i2. That will create the new Item and then it can be added to the auction Item list.
I'm assuming the start of the sequence diagram is the auctioneer creating or opening an already created auction and then adding a list of items that will be used in the auction by repeatedly calling registerItem(new Item(...)); which can then have bids added to them by Max and Moritz via the Auction object

Indexing a simple Java Record

I have a Java Object, Record . It represents a single record as a result of SQL execution. Can CQEngine index collection of Record ?
My class is of the form
public class Record {
private List<String> columnNames;
private List<Object> values;
... Other getters
}
I have looked through some examples, but I have no luck there.
I want to index only specific column(s) with its name and corresponding value. Can this be achived using cqengine or is there any other alternatives to achieve the same.
Thanks.
That seems to be a strange way to model data, but you can use CQEngine with that model if you wish.
(First off, CQEngine will have no use for your column names so you can remove that field.)
To do this, you will need to define a CQEngine virtual attribute for each of the indexes in your list of values.
Each attribute will need to be declared with the data type which will be stored in that column/index, and will need to be able to cast the object at that index in your list of values, to the appropriate data type (String, Double, Integer etc.).
So let's say your Record has a column called 'price', which is of type Double, and is stored at index 5 in the list of values. You could define an attribute which reads it as follows:
public static final Attribute<Record, Double> PRICE =
attribute("PRICE", record -> ((Double) record.values.get(5));
If this sounds complicated, it's because that way of modelling data makes things a bit complicated :) It's usually easier to work with a data model which leverages the Java type system (which your model does not). As such, you will need to keep track of the data types etc. of each field programmatically yourself.
CQEngine itself will work fine with that model though, because at the end of the day CQEngine attributes don't need to read fields, the attributes are just functions which are programmed to fetch values.
There's a bunch of stuff not covered above. For example can your values be null? (if so, you should use the nullable variety of attributes as discussed in the CQEngine docs. Or, might each of your Record objects have different sets of columns? (if so, you can create attributes on-the-fly when you encounter a new column, but you should probably cache the attributes you have created somewhere).
Hope that helps,
Niall (CQEngine author)

How to add multiple values to a property in neo4j

I am new to neo4j and trying to add multiple values to a property of a node.How to do it?
create (e:Employee{name:"Sam",languages:["C","C#"]})
Tried this but didn't find any proper way to add multiple values to an attribute.
Properties cannot have object values. If you're looking to store multiple properties on language, and those properties all belong to the language and not any other entity, then you should model Language as a node. You can store properties on the relationship between the employee and language as well if required.
Then you'll end up with something like this:
create (l:Language {name:"C", otherProperty:"property value"})
create (e:Employee {name:"Sam"})
create (e)-[:SPEAKS {level:"Fluent"}]->(l)
In fact, you can have array values in properties. You should be able to create them like:
CREATE (n:Node { color: ['Red', 'Blue']})
RETURN n
In your case:
CREATE (e:Employee { name:"Sam",languages: ["C", "C#"]})
RETURN e
is working perfectly fine (you can check it in http://console.neo4j.org/)
Keep in mind that all values in the array must be of the same type, only Strings, or Integers, etc. You can find more info here -> http://neo4j.com/docs/stable/rest-api-property-values.html
However that's not the best approach for that particular example given that C and C# are languages that Sam knows, you should have them as different nodes pointed by Sam through some kind of Knows relationship.

Java API design - Pass by Object or Pass By Value

I am designing the service layer of my web application. There are some scenario which I need to get the Orders based on the Product.
When I design the API, should I pass by object or value?
Order order = new Order();
String orderId = "1";
order.setId(orderId);
List<Product> products = getProductByOrder(order);
List<Product> products = getProductsByOrderId(orderId)
Well, i think you are making some mistakes with this concepts, in this two ways you are making references to this objects, order and orderId(since String is an object too.)
But the best approach in this case is using getProductsByOrderId(orderId) because your code will be loosely coupled, since your other layer won't have to know about an Order object, and just know about a string object. If we can pass simpler objects as parameters, we do.
Good example from #Pienterekaak posted as comment:
"In many cases its easier to obtain just an orderid, then a whole order object. (for example, you would include an order id in a REST call, not the whole order object)"
From my experience i would go for:
List<Product> products = getProductsByOrderId(orderId)
With the argument, that for the first call, you need an Order object, and for the second call you just need an id, which is probally easier to obtain.
Actually both of these are passing by value. Java only passes by value. In both of these cases you are passing a reference to an Object (String) or (Order). You are passing as a value the location of memory where this object is (pointer). If you are passing a primitive type like int it passes the value like 1 but if you are passing an object it passes the value of the pointer to the object ie. memory location. In any case you are always passing by value.
If you have a Map storing the Order objects it is actually more efficient to pass the Object itself because you are directly passing the pointer for that object. If you pass the String id of 1 you are passing a pointer to that string then you would have to use that string to look up your Order object which is actually adding more processing then just passing a pointer to the object directly.
OrderId belongs to the concept of an order. If you pass the order id, the product has to know, how an order is identified. That's not loosely coupled.
If you put the method into Order, so you can call a property like
List order.Products
only the Order concept knows, how products and orders are connected, which sounds right to me.
Products shouldn't know anything about orders but orders should know about products.
If you use Hibernate, you could configure it to do it for you with an one-to-many, since orderId is a primary key in your order table.

java dynamic attributes table

I am developing a java application which needs a special component for dynamic attributes. The arguments are serialized (using JSON) and stored in a database and then deserialized at runtime. All attributes are displayed in a JTable with 3 columns (attribute name, attribute type and attribute value) and stored in a hashmap.
I have currently two problems to solve:
The hashmap can also store objects and the objects can be set to null. And if set to null i dont know which class they belong to. How could i store objects even if they are null and known which class they belong to? Do i need to wrap each object in a class that will holds the class of the stored object?
The objects are deserialized from json at runtime. The problem with this is that there are many different types of objects and i don't actually know all object types that will be stored in the hashmap. So i am looking for a way to dynamicly deserialize objects.. Is there such a way? Would i have to store the class of the object in the serialized json string?
Thanks!
Take a look to the Null Object Pattern. You can use an extra class to represent a Null instance of your type and still could contain information about itself.
There is something called a Class Token, Which is the use of Class objects as keys for heterogeneous containers. Take a look to Effective Java By Joshua Bloch, Item 29. I'm not sure how this approach could work for you since you may have many instances of the same type but I leave it as a reference.
First of all, can you motivate why you use JSON serialization for your attributes ?
This method is disadvantageous in many ways in my opinion, it can cause problems with database search and indexing, make database viewing painful and caus unnecessary code in your application. These problems can be not an issue, it depends how you want to use your attributes.
My solution for situation like these is simple table containing columns like:
id - int
attribute_name - varchar
And then add columns for each supported data type:
string_value - varchar
integer_value - int
date_value - date
... and any other types you want.
This design allow for supreme performance using simple and typesafe ORM mapping without any serialization or other boilerplate. It can store values of any type, you just set correct column for attribute type, leaving all other with null. You can simulate null value by using null in all data columns. Indexing and searching also becomes a piece of cake.

Categories

Resources