Does command/query separation apply to a method that creates an object and returns its ID?

Posted by Gilles on Programmers See other posts from Programmers or by Gilles
Published on 2011-12-12T17:53:38Z Indexed on 2012/11/14 23:18 UTC
Read the original article Hit count: 364

Filed under:
|

Let's pretend we have a service that calls a business process. This process will call on the data layer to create an object of type A in the database.

Afterwards we need to call again on another class of the data layer to create an instance of type B in the database. We need to pass some information about A for a foreign key.

In the first method we create an object (modify state) and return it's ID (query) in a single method.

In the second method we have two methods, one (createA) for the save and the other (getId) for the query.

    public void FirstMethod(Info info)
    {
        var id = firstRepository.createA(info);           
        secondRepository.createB(id);
    }

    public void SecondMethod(Info info)
    {
        firstRepository.createA(info);
        var key = firstRepository.getID(info);
        secondRepository.createB(key);
    }

From my understanding the second method follows command query separation more fully. But I find it wasteful and counter-intuitive to query the database to get the object we have just created.

How do you reconcile CQS with such a scenario?

Does only the second method follow CQS and if so is it preferable to use it in this case?

© Programmers or respective owner

Related posts about object-oriented

Related posts about cqrs

  • CQRS at Jax Code Camp 2012

    as seen on ASP.net Weblogs - Search for 'ASP.net Weblogs'
    Continuing my CQRS world tour...I gave my CQRS presentation at the Jax Code Camp 2012 this past Saturday.  It was a great crowd with lots of representation from the wicked smart engineers at Feature[23] and others from the Jacksonville developer community. If you'd like to take a… >>> More

  • CQRS without using others patterns

    as seen on Programmers - Search for 'Programmers'
    I would like to explain CQRS to my team of developers. I just can't figure out how to explain it in the simplest way so they can implement the pattern rapidly without any others frameworks. I've read a lot of resources including video and articles but I don't find how to implement CQRS without using… >>> More

  • Domain queries in CQRS

    as seen on Stack Overflow - Search for 'Stack Overflow'
    We are trying out CQRS. We have a validation situation where a CustomerService (domain service) needs to know whether or not a Customer exists. Customers are unique by their email address. Our Customer repository (a generic repository) only has Get(id) and Add(customer). How should the CustomerService… >>> More

  • CQRS - The query side

    as seen on Stack Overflow - Search for 'Stack Overflow'
    A lot of the blogsphere articles related to CQRS (command query repsonsibility) seperation seem to imply that all screens/viewmodels are flat. e.g. Name, Age, Location Of Birth etc.. and thus the suggestion that implementation wise we stick them into fast read source etc.. single table per view mySQL… >>> More

  • CQRS event versioning

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Versioning If your events changes you would create a new version of that event, and keep the old ones. To keep your domain code form being bloated with handling of all versions of events you would basically introduce a component that converts your events from previous to newer versions, and then… >>> More