I need to have some unique&consistent ID for indexing data, I tried to use objectID of NSManagedObject, but looks like for the same entity, its objectID keeps changing, does anyone know if this is not consistent?
Unless you haven't saved a new object, the objectID is unique and consistent.
To quote the Core Data Programming Guide:
Managed Object IDs and URIs
An NSManagedObjectID object is a universal identifier for a managed
object, and provides basis for uniquing in the Core Data Framework. A
managed object ID uniquely identifies the same managed object both
between managed object contexts in a single application, and in
multiple applications (as in distributed systems). Like the primary
key in the database, an identifier contains the information needed to
exactly describe an object in a persistent store, although the
detailed information is not exposed. The framework completely
encapsulates the “external” information and presents a clean object
oriented interface.
NSManagedObjectID *moID = [managedObject objectID];
There are two
forms of an object ID. When a managed object is first created, Core
Data assigns it a temporary ID; only if it is saved to a persistent
store does Core Data assign a managed object a permanent ID. You can
readily discover whether an ID is temporary:
BOOL isTemporary = [[managedObject objectID] isTemporaryID];
Related
Even though I use a specific ORM framework, Bold for Delphi, I'm more interested in framework agnostic theoretical view on the problem.
So the question is about having a persistent object and a transient attribute with initial value tag.
The initial tag specifies the value attribute will get when instance of owning object is created.
However when subsequently loading this object from persistence, what should be the value of transient attribute?
Should initial value tag be applied again? Logically, it should, otherwise it will be left unassigned (null).
I couldn't find any specs on this particular case in any of the docs.
We can't create object up to the DB record only - because we would lose all transient attributes. So, when you are loading a persistent object, it can be done only into the already created instance. And there is no other way of instantiating without using the base object constructor, which sets the initial values. Of course, some language could make a workaround about it, but why?
Here is my definition of the two terms, though I'm not sure if it is a complete one:
A persistent object is an instance of a class in the domain model
that represents some information extracted from the database. A
transient object is an instance of a class in the domain model, which is created in memory
a) I assume the terms persistent and transient are used only for objects in the domain model, but not also for objects in business layer that live outside the domain model?
b) Do we also use the two terms for Data-Transfer-Objects?
c) Are the two terms also used for Value Objects?
Thank you
Persistent means that the object has been saved to the database whereas transient means that it hasn't been saved yet. So for example when you get an entity from a repository, that entity is persistent. When you create a new entity, it is transient until persisted.
a) These terms are more affiliated with ORMs than they are with DDD so they apply to anything that is not DDD. Within DDD persisted/transient apply to entities and aggregate roots because these are the objects that are persisted with repositories.
b) No, DTOs are designed to carry data across process boundaries and don't have a life-cycle that objects that you wish to persist to a database do.
c) No because value objects don't have an identity and can only be persisted as part of an entity or aggregate root. A value object is just a value, sort like 1 is a integer value and it doesn't make sense to speak about whether it is persisted or not.
Transient means unprocessed object or the object which is instantiated or newly created. Once the object is being submitting for any other operation than the object state is known an persistent
An object typically has two components: state (value) and behavior (operations).It can have a complex data structure as well as specific operations defined by the programmer.9 Objects in an OOPL exist only during program execution; therefore, they are called transient objects. An OO database can extend the existence of objects so that they are stored permanently in a database, and hence the objects become persistent objects that exist beyond program termination and can be retrieved later and shared by other programs. In other words, OO databases store persistent objects permanently in secondary storage, and allow the sharing of these objects among multiple programs and applications.
I am working on an application where users can follow each other, in a similar fashion to Twitter.
After reading up on DDD, I understand that my users are Entity Objects - I refer to them using their unique ID.
When one user 'follows' another (i.e. forms a Connection), the relationship is stored in a many-to-many table. Its fields include FollowerID, TargetID, and Status. There can be only two records for each Follower/Target combination (one Active, the other Inactive), so I can safely identify objects based on their attributes.
So, I think my Connection objects are Value Objects, not Entity Objects, but I'm not sure. Can you help me with this decision?
You are correct that entities are unique and carry the notion of having an identity (i.e. only one unique user can exist). A Connection is dependent on other User entities. It represents some aspect between two users. That aspect is whether there is an active or inactive connection. Without containing the data of which users are connecting, a connection has no identity. It may even have it's own primary key in the database, but from a domain perspective, it has no identity of it's own.
Therefore, I would say that Connection is a value object.
To support my conclusion, Microsoft.Net Architecting Applications for the Enterprise, page 187, says:
A value object class represents an entity in the domain that mostly
contains data and lives for the data it contains. A value object is
fully identified by a combination of values it contains. An entity
object, on the other hand, has its own life and rich behavior
regardless of the data it contains. Entity objects are usually objects
with a longer lifetime. A value object represents an aspect of an
entity and can live only in relation to an entity.
And also on page 189:
One further comment is needed to explain the difference between
entities and value objects. You don’t need a repository or a data
mapper for a value object. You need a repository only for an entity.
The repository (or the mapper) for a given entity will certainly take
care of all value objects that depend on a given entity.
Some time ago, I saw a cartoon about scientist that had invented cloning. Every time he cloned himself, he destroyed previous version. Then person that was watching demonstration decided to interrupt and sabotaged destruction part so there were two scientists. Cartoon ended with some interesting existential questioning.
Values vs entities is not about having or not having id fields in one or another form. Point is - how we are looking at those objects through our domain perspective. If they are value objects, then only their value matters - 1st, 3rd and 53rd scientist are the same. If we care about identity, if we think that cloning 3rd scientist will never be like 1st one, then our object is an entity.
I am implementing "duplicate" functionality in my iOS application. I'm using the following workflow:
present a list of managed objects in initial context in root view controller
when user taps on a row, create a new context pass it to "detail" view controller with duplicated managed object ([[DetailController alloc] initWithObject:clonedObject inContext:newContext]).
However I am struggling with the concept of reassigning relations from source object to the cloned one since their managed object contexts differ. What would be correct approach to this:
Should I just reassign the pointer value and do not bother about MOC or...
I should refetch the values in new context depending on their unique identifiers?
Any other option I did not think of?
P.S. Contexts are using same persistent store coordinator.
Managed object IDs are thread safe. As such, you can pass a managed object ID to the MOC in your view controller, retrieve that object via existingObjectWithID:error, then perform the duplication in that context. This way, the objects never cross MOC boundaries.
Please share your view on this kind of thing i'm currently testing out :
Have an JPA entity inside my JSF managed bean
Bind the entity's properties to the JSF form elements like input text, combo, even datatable for the entity's list of detail objects for example.
Have the entity processed by a service object, meaning the entity object itself, and perhaps with some other simple variables / objects
The service will do some basic validation or simple processes, and deliver the entity object to the DAO layer to be persisted
And the JSF view will reflect on the detached entity
Is this kind of solution with passing the entities between tiers OK ?
Forgive me for my inexperience in this matter, since i was used to play with 'variables' in webapp (using map based formbean in struts 1), but i've read about transforming the entity objects into some other format, but i'm not sure what it is for ?
If the relations between entities are defined, we can bind it to JSF components, and therefore render based on and populate the entity's properties.
Yes, this is perfectly fine and in fact the recommended way to do it nowadays.
This "transforming the entity objects into some other format" refers probably to the Data Transfer Object pattern, which was necessary in the bad old days before annotations, when entity classes usually had to inherit from some framework-specific base class, undergo bytecode manipulation or were implemented as proxy objects by an EJB container.
Such entity objects were either impossible to serialize or contained much more state than the actual entity data and therefore would waste a lot of space when serialized. So if you wanted to have a separate app server tier, you had to use the DTO pattern to have it communicate efficiently with the web tier.