I wondering what's the difference between members() and fields() in ArchUnit. I can't find any documentation about it.
The ArchUnit User Guide documents the domain model.
The members of a class consist of all of its fields and code units (i.e. methods, constructors and static initializers).
I look into the code and the answer is very simple:
fields are what expected the fields of a class
methods are the methods
constructors are the constructors
and member is the union of all this lists.
Related
Let's assume I have 2 classes with association relation.
What is the difference between adding attributes to one of the classes and adding an Association Class with attributes?
I understand that association class describes the association relation, but cannot I use simple class attributes instead?
What are the added values of Association Class?
Association and attributes are actually different renderings of the same thing. The difference is more a bit of esoteric nature. An association is visually more exposed than an attribute. So you would use the attribute if it has a more local importance rather than a more system wide in case of an association.
What you should not do is to use both. When using an association and you need to name it then use a role along with the association and do not repeat it as attribute.
An association class (AC) is actually a combination of a class and an association (in UML it inherits from both). This way the pure relation between two classes can have methods and attributes itself. An association class is used if you want to relate two classes with M-N relation. E.g. the AC between Student and Exam describes the results a student has achieved in exams. So you have a Class 1-M AC N-1 Class relation rather than having this information as array attributes inside either of the opposing classes.
As a side note (since you tagged it EA): EA offers a drop down in the roles which is fed from attributes of the class. You should not make use of that. This is some heritage and/or a misinterpretation of the definition. As said: its not wrong, but also not good style.
You say you already have two classes with an association between them. In that case, there is a huge difference between adding attributes to one of those two classes and changing the association into an association class with its own attributes. They are not at all similar.
Unnamed properties already exist at each end of the association and are typed by their adjacent class. Adding redundant attributes is unnecessary and would be incorrect.
An association class allows you to record additional facts about a link between instances of the other two classes, such as the point in time the link came into existence. Association classes are like associative entities in a database.
I think part of your question is also about when to use an association vs when to use attributes. The UML spec doesn't provide any guidance on this. However, common practice is to use an attribute only for types that have no identity, such as the number "5", the string "Hello", or the combination of "7" with an enumeration literal of "pounds". In UML, these types are called primitives and datatypes. If you want a property typed by one of those, use an attribute; otherwise, use an association with named association ends.
The domain driven design differentiates two model types: entities and value objects. In the most examples the attributes of an entity are value objects or other entities, while the attributes of a value object are mostly simple strings, integers etc. (i. e. primitive data types).
That leads me to the question: Can an entity also have attributes of primitive data types? Or do you normally model each attribute of an entity as a value object or another entity?
The following might be an example to answer this question: We have an entity Comment with an attribute text. Is text simply a string variable or a value object?
While it is common attitude to compose an entity of another entities or value objects, it is not necessary. Please remember that you should think about an abstraction. Primitive types are ok when there is no business logic involved in using them. For example:
public class User {
private UserId id;
private String nickname;
private Date joinDate;
}
As you can see, nickname is an primitive type, because we can't do anything special with nickname. On the other hand joinDate should be Value Object, because dates has some logic (as comparing dates, adding, subtracting etc.)
Even in "Implemeting Domain-Driven Design" by Vaughn Vernon are examples of entities composed of primitive types.
I'm checking out Sharp Architecture's code. So far it's cool, but I'm having problems getting my head around how to implement DDD value objects in the framework (doesn't seem to be anything mentioning this in the code). I'm assuming the base Entity class and Repository base are to be used for entities only. Any ideas on how to implement value objects in the framework?
In Sharp Arch there is a class ValueObject in namespace SharpArch.Domain.DomainModel. This object inherits from BaseObject and overrides the == and != operators and the Equals() and GetHashCode() methods. The method overrides just calls the BaseObject versions of those two methods which in turn uses GetTypeSpecificSignatureProperties() method to get the properties to use in the equality comparison.
Bottom line is that Entity's equality is determined by
Reference equality
Same type?
Id's are the same
Comparison of all properties decorated with the [DomainSignature] attribute
For ValueObjects, the BaseObject's Equals method is used
Reference equality
Same type?
Compare all public properties
This is a little bit simplified, I suggest you get the latest code from github and read through the code in the mentioned 3 classes yourself.
Edit: Regarding persistence, this SO question might help. Other than that, refer to the official NH and Fluent NH documentation
Value objects are simple objects that don't require a base class. (The only reason entities have base classes is to provide equality based on the identity). Implementing a value object just means creating a class to represent a value from your domain. A lot of times value objects should be immutable and provide equality comparison methods to determine equality to other value objects of the same type. Take a look here.
Take for example:
CreateOrderTicket(ByVal items As List(Of OrderItems)) As String
Where would you put this sort of logic given:
CreateOrder should generate a simple list ( i.e. Item Name - Item Price )
PizzaOrderItem
SaladBarOrderItem
BarOrderItem
Would you recommend:
Refactoring common to an abstract class/interface with shared properties a method called CreateOrderTicket
Or,
Creating a common service that exposes a CreateOrderTicket
We obviously would not want three createOrderTicket methods, but adding methods, inheriting, overloading and using generics seem like a high cost just to abstract one behaviour..
Assume for the sake of a simple example that (currently) there is no OrderItem baseclass or interface..
Help!! :)
p.s. Is there a way to overload without forcing all inheriting objects to use the same name?
Abstract base class sounds like the best option in this situation. Of course it all depends on what kind of shared behaviour these items have. Without knowing more, I'd guess all of these order items have Name and Price for example - and in future you might add more common stuff.
Without a shared base class which contains the Name and Price properties, you'll probably have troubles implementing a CreateOrderTicket method which takes a list containing more than 1 kind of orders.
Also I don't think inheriting from an abstract base class would be that high cost as technically the objects already derive from the Object base class. (Though I don't think this is completely equal to a custom base class.)
VB.Net can implement methods from an interface using a different name than the one specified in the interface but don't think the same goes for overriding abstract functionality.
I have a data class with the following methods:
ExecuteUDIQuery(string query)
ExecuteSelectQuery(string query)
ExecuteSP(string anme, string[,] params)
I have a lot classes which use the data class. Now i want to create a class diagram, but i don't know what kind of relation the classes have with the data class. Is it a composite? Is it 1:1 or .. ?
An example of a class which use the data class is the Staff class. This class has a method Load(), which will load a staff object with the Id of the staff member. This method contains a query which is passed to the ExecuteSelectQuery(string query) method of the Data class.
EDIT:
The data class isn't static. However, i have my doubts. I actually don't know what to. The point is, the only thing it does is executing queries and returning the results.
I would suggest its a usage dependency relationship.
See here for a brief description.
I would query the naming of your classes. a class name should normally be a singular noun. Examples;
Window
Person
Transaction
Data is a plural, and in any case I think it should be Database.
Similarly for Staff - once again a plural, I think it should be MemberOfStaff. Unless of course it is a list of members of staff, in which case I would call it something like Department, Project or Division - whatever your problem domain indicates.
You will find that coming up with good names for classes is suprising ly difficult, but it is well worth the effort.
The difference between aggregations, composites and 1 on 1 relations are a bit vague and somewhat arbitrary.
I use the aggregation (open diamond) if one class owns the other class (is responsible for the lifecycle.
I use 1 on 1 relationships in all other cases.
Is the class instantiated by the classes that use it or are the methods static?
If they are static I would represent this as an unqualified dependency (dotted arrow pointing from the classes that is using the data class to the data class)
If the classes that are using the data class create their own private instance of that class this would be a 1:1 composition (assuming that the data class instance's lifcycle is tied to the object that is using it)
I cannot refrain from commenting your overall design, I would try to move the Load method out of the Staff class, so that this class is not dependent on the Data class directly.
Within the scope of your existing design I would suggest the following:
If the staff class contains an instance variable of the data class, then it is an association. If the data class is instantiated just to retrieve the instance, it is just a dependency of a given type, like #toolkit says.
Not enough data.
Give us some class outlines or something. From what I can see, I wouldn't have actually called this a data class (it looks more like a data accessor) which sounds like it might be a singleton (many:1, aggregation or association), or if instanced will be a 1:1 component.
Now i want to create a class diagram, but i don't know what kind of relation the classes have with the data class.
Nor do we - you've only described the Data class, and not said how Staff gets the Data it uses.
If Staff holds on to one or more instances of the data class, then there is either an association between Staff and Data, or Staff has an attribute of type Data (if Data has value semantics).
If the Data instances are referenced by multiple Staff instances, and their lifecycles are dependent on being referenced by Staff instances then this may be shown as an aggregation relation. If the Data instances are not shared between Staff instances and their lifecycles are dependent on being referenced, then this may be shown as an composition relation.
If X doesn't keep hold of the Data instances it uses, then a usage relationship is appropriate.
Dependency and Usage are the two weakest kind of "connectors". You might consider stereotypes, keywords to refine the relationship. You might find that instantiate,call,create,send stereotypes work. Without more information though the correct answer seems to be usage.