0

I have two development paths regarding what a return value should be from a RESTful architecture standpoint. What are the pros and cons of each method below?

First method.

The Client will submit a post of item Hologram. Here is the model in method one.

public class Hologram
    {
        public double HoloWidth { get; set; }
        public double HoloHeight { get; set; }
        public double HoloDepth { get; set; }
        public string HoloGuid { get; private set; }

        public Hologram()
        {
            this.HoloGuid = new Guid().ToString();
        }
    }

The idea is, that this method will be able to provide a faster response to the client, because I will return a guid immediately associated to this Hologram. Here is the repository for method one, I don't have the database connection set up yet but _holos will be the database.

        public string Add(Hologram hg)
        {
            Hologram hig = new Hologram();
            hig = hg;
            AsyncCode(_holos.Values.Add(hig)); /// this adds to the database via injection, running asynchronously.

            return hig.HoloGuid;
        }

Second method.

The client will submit a post of item Hologram as well, but the model is a bit different.

public class Hologram
    {
        public double HoloWidth { get; set; }
        public double HoloHeight { get; set; }
        public double HoloDepth { get; set; }
        public string HoloId { get; set; }

        public Hologram()
        {

        }
    }

The idea for this method is somehow the Stored Procedure or EntityFramework that inserts a new record into the database will return the id ( I haven't figured out how to get the id yet). Once this happens, the ID of the Hologram can go back to the App server, and then provide the return id. Here is the Repository for the second method.

public int Add(Hologram hg)
        {
            _holos.Values.Add(hg); ///adds to database via injection
           ///hg will have the ID from the database at this point... somehow.
            return hg.ID;
        }

2 Answers 2

3

Pros and cons of the first method

Pros

  • Client can receive id faster, without waiting for database operation to complete.

Cons:

  • He received it faster, but what would he do with it? If he wants to immediatly query hologram by that id, what if it was not inserted yet? What if insert failed, how would client know about that?
  • When you generate guids on server, not in database, they are usually not sequential. In database you can configure generated guids to be sequential which will help with insert performance if you have index on the guid column (and you usually have such index, because you usually want to query by guid).

Pros and cons of the second method

Pros and cons of second method are basically the reverse of the above :) In conclusion, given only info you provided I'd better use second method, because it's more reliable and more easy to implement correctly.

Sign up to request clarification or add additional context in comments.

Comments

0

I would recommend moving forward with your second approach. It seems like an anti-pattern to have a property on your request DTO that is supposed to be generated and returned in the response DTO.

Your first method also introduces the potential for incorrect GUIDs to be persisted compared to what the client expected. A client application might, for example, take note of the public GUID property before submitting the POST request to your API. When the server receives the request, it will construct a new Hologram object, thereby generating a new GUID that is different from the one generated in the calling application.

At least with your second approach, the unique identifier is only generated once and the client will only expect it back if the entire request is successful. If there is a transient error, the client will not have an ID since it was never successfully saved to the database.

1 Comment

I would have it only happen on creation though, but other than that, yea second approach is better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.