What is the correct way of passing a resource link to an entity constructor as parameter? - spring-data-rest

When a resource is being posted, is it possible to use the resource's entity constructor instead of a repository event handler to run logic on the posted relationships? If so, how? I am looking for the case where the related resource already exists but only the relationship is being created with the newly created resource.

Related

How can I check whether or not EF Core has queried the database for an object with the same Id in different phase of a request?

For example, my API controller action logic might retrieve a User object by its Id as the first step. Then after some processing, within the same http request, I need the User instance with the same Id again. Somehow the User object can't get passed all through. So at this point, when I call something like dbContext.Users.Find(1) again, how can I know for sure that the DbContext doesn't have to query the database again and instead use the Local tracked version of this User object?
So at this point, when I call something like dbContext.Users.Find(1) again, how can I know for sure that the DbContext doesn't have to query the database again and instead use the Local tracked version of this User object?
You know that because that is guaranteed by the definition of the Find method (which makes it different from FirstOrDefault, SingleOrDefault and similar):
Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.
So as soon as you use one and the same db context instance (which is the case with ASP.Net Core requests and the db context is registered with scoped lifetime), then the Find method will use the tracked object if exists and won't make database calls.

Name for class with `get existing or create new` logic

I have an User class, a repository with find method for finding existing user (in a storage) and a factory, which creates new user on demand.
No my question is where would I put the getExistingOrMakeNew method?
I guess it doesn't really fit to respository/factory classes - if so it should be isolated to a separate class. What would be the right name? Is there a known pattern for this?
Maybe just create a factory that have access to repository and can use it during creation process?

Spine.js have updateAttributes include new attributes

It seems that Spine's Model.updateAttributes only updates attributes, and does not create new ones in case you supply any.
In my usecase, I have a controller that creates part of the attributes. Then through an Ajax request the server responds with the full object, and I want to update the model instance living in Spine with the additional variables.
For example, I have a model with attributes: name, date_created. Through the controller a user instantiates an object providing only the name. An Ajax request notifies the server which in turn responds with a name and a date_created. This date_created should then be added to the user's model.
Model.updateAttributes doesn't work, and I wouldn't be too fond of deleting the object and creating a new one - that just seems as too much overhead. I could provide default values for variables that are not set upon creation, but that also has a negative side. I guess what I'm looking for is a method that could be called Model.createOrUpdateAttributes. Can anybody recommend a way to achieve this? Thanks!
I might haven't fully understood your usecase, but I'll try to answer.
You need to declare whatever attributes a type of a model has with the configure class method. This declaration helps various model function to do their job later.
After you declare all the attributes you need, you can create model instances with any of the previously declared attributes.
You don't have to provide values for all the declared attributes.
After the ajax call returns, the date_created will be set on your model instance. Until this happens it will be just undefined.
If this solution still can't work for you, please describe why, and I'll gladly try to help.

Intercepting object creation in RavenDb

I am trying to run some code on objects that are loaded from RavenDB, and I need to do it just after the object has been loaded with its property values.
I've tried intercepting the deserialization process using a CustomCreationConverter and overriding ReadJson, but the object I can access at that point has all the properties set, except the one I need : the Id. Is there somewhere else I can slot into the pipeline in order to do this?
The reason you don't see the Id is because it's not part of the document, it's in the metadata as #id.
If you want to intercept client side, you can register a custom Conversion Listener. Create a class that implements IDocumentConversionListener and register it with documentStore.RegisterListener(). In the DocumentToEntity method, you can run your custom logic. The documentation is lacking on Listeners in general, but there is another topic that also uses them:
http://ravendb.net/kb/16/using-optimistic-concurrency-in-real-world-scenarios
The other option would be to add a bundle that intercepts on the server side. For that, you would use a Read Trigger.

Better Approach for Creating Temp Object for Core Data with Restkit

In my app, I have this scenario where I need to post an object to remoter server and get an object key back and then store the object locally. I have Core data and Restkit implemented in my app.
The object value are collected from user input. I couldn't figure out a great way to prepare the object before posting it to remote server. This object is an entity of type NSManagedObject, and I don't want to store it before I get the object id from server.
I came across this which suggested to use a transient object to handle this situation. But as discussed in that thread, this causes issue with code maintenance.
Is there a better way to handle this scenario? Thanks.
Make your core data model class adhere to the RKRequestSerializable protocol.
Then when the user input is validated, create an entity as normal and set it as the params value to the RKRequest, this will send your object as the HTTP body. Look inside RKParams.m for an example.
Also set the newly created entity as the targetObject for the RKObjectLoader. That way, when your web service returns the information (like the new unique ID), it will target the new object and save the new unique ID to this object without creating a duplicate.
Clear as mud?
PS: Oh and be careful mixing autogenerated core data classes with custom code! I recommend mogen to help you not lose code each time you make a change.