Domain-driven design (DDD) focuses on what matters in enterprise applications: the core business domain. And Naked Objects lets you build DDD applications just by writing the core domain classes, the rest of the application is taken care of for you.

This blog supplements and expands on my book, Domain Driven Design using Naked Objects, describing how you can rapidly develop and test domain applications using Naked Objects.

Simulated Enums – supporting choices

In my last post I showed how to simulate enums, and then Giorgio asked in the comments as to how this fits in with the choices() method, used to provide a drop-down list of values.

To start with, let’s add an all() method to StockType:

import org.nakedobjects.applib.DomainObjectContainer;

public class StockType extends EnumeratedAbstract {

  private static final String CD = "cd";
  private static final String BOOK = "book";

  public static StockType Book(DomainObjectContainer container) {
    return lookup(container, StockType.class, BOOK);
  }

  public static StockType Cd(DomainObjectContainer container) {
    return lookup(container, StockType.class, CD);
  }

  public static List all(DomainObjectContainer container) {
    return Arrays.asList(Book(container), Cd(container));
  }

  public static void createAll(DomainObjectContainer container) {
    create(container, StockType.class, BOOK);
    create(container, StockType.class, CD);
  }

}

Then, in the StockType domain object we just add the choices() method, for example:

    // {{ StockType
    private StockType stockType;
    @Disabled
    @MemberOrder(sequence = "2")
    public StockType getStockType() {
      return stockType;
    }

    public void setStockType(final StockType stockType) {
      this.stockType = stockType;
    }
    public List choicesStockType() {
        return StockType.all(getContainer());
    }
    // }}

Short and sweet. In the next post I’ll show how we can teach Naked Objects to support Java 5 enums natively (a feature that we’ll be adding formally in Naked Objects 4.1).

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

1 comment to Simulated Enums – supporting choices

Leave a Reply

 

 

 

You can use these HTML tags

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