Value Objects: AppCode’s Code Generation to the Rescue

January 12, 2016 — 6 Comments

A Value Object is a handy way to combine multiple values in a single object. It sounds simple — you just make each value a property, right? Well, there’s usually more to it than that. There’s even an entire objc.io article about Value Objects. …So how do you define a new Value Object?

I believe in laziness. Let’s do as little work as possible!

[This post is part of the series TDD Sample App: The Complete Collection …So Far]

What we need

In the Marvel Browser iOS TDD sample app, the goal is to display comic characters whose names start with a given prefix. The Marvel Comics API offers many parameters for fetching characters. We want to use just three of them:

  • nameStartsWith
  • limit
  • offset

This will allow us to fetch the results a page at a time, and request the next page.

Services are better without state

The page size will likely be a fixed quantity in the app. We could build a Service that knows this fixed quantity. It could also keep track of the offset, encapsulating “request the next page.”

I’ve written Services like that before. But the guidance given by Domain-Driven Design is “make the Service stateless.” Why?

Let’s picture what it means to make the Service stateless. In our example, it means the Service won’t track the prefix, the page size, or the page offset. We’ll track these values elsewhere in the app. The Service only does what it’s told. What we get is a Service that is ignorant about our app.

This makes testing easier. It also opens the door to reusability. As I wrote last time: when it comes to objects, ignorance is bliss!

Generating Value Objects with AppCode

So we’ll pass in three parameters. Let’s pass them together as a Value Object. Are you ready?

Instead of assembling another set of screenshots, I made a screencast — my shortest yet. It’s just three-and-a-half minutes long:

Whether you use Xcode or AppCode, be sure to get my free set of test snippets:

Links:

What do you think about stateless Service objects? Have I convinced any of you to try AppCode yet? Click here to leave a comment.

Domain-Driven Design: Tackling Complexity in the Heart of Software

[This post is part of the series TDD Sample App: The Complete Collection …So Far]

Jon Reid

Posts Twitter Facebook Google+

I've been practicing Test Driven Development (TDD) since 2001. Learn more on my About page.

6 responses to Value Objects: AppCode’s Code Generation to the Rescue

  1. Hi, I can’t view the video I get the “private content” error.

  2. Hey John – FYI the YouTube vid you posted is marked as private.

    Cheers,

    Ben.

  3. Sorry about that! It’s public now, please try again. *blush*

  4. Hi Jon,
    Do you know if there is a way to auto-generate with AppCode a unit test for the initialiser?
    Cheers,
    Juan

    • Juan, there’s really no need for such a test. Here’s why: Other tests will use the Value Object by creating and initializing it with certain values. Those tests will then confirm that the input (via the initializer) affects the output in a certain way.

      Did that make sense?

      • Yes, it makes sense. But at this stage of the project there is production code that is not tested (the body of the initializer). If you comment out that code and return nil, unit tests still pass. So we broke the rule “You are not allowed to write any production code unless it is to make a failing unit test pass.”

        This is why I thought the unit test is needed now. Maybe it can be removed later if it adds no value.

        Maybe I’m too strict applying TDD.

        Thanks for your response!

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*