Value Objects

27 12 2009

The value objects are simple but very useful objects. Value object, one of the powerful concept from DDD, is an object that describes a characteristic of a thing.

According Eric Evans,

“An object that represents a descriptive aspect of the domain with no conceptual identity is called a value object. Value objects are instantiated to represent elements of the design that we care about only for what they are, not who or which they are.” [Evans 2003]

Here a few characteristics of value object:

  • The key defining characteristic of a value object is that it has no identity. In other words, the intention of a value object is to represent a concept by it’s attributes only.
  • They should probably be immutable, once created they cannot be changed or altered.
  • They are not DTOs, not Java beans and not objects with public fields, they encapsulate data with some behavior. DTOs are bunch of data, which are not necessarily coherent, VOs contains high-chorent data with behavior. The purpose of DTOs is data transfer but the purpose of the VOs is domain representation.
  • They are mostly very simple objects.
  • They “swallow computational complexity”.
  • More extensible
  • More testable
  • Some examples of value objects are money, addresss, phone number, product code, email address, etc..





Unit Testing Repositories with DbUnit and Spring

28 05 2008
In this post, I will create a base class for repository tests. I used DBUnit to populate database configured in a testContext.xml. And I used Spring test annotations to wire test classes with dataSource and repositories.
Requirements
* Spring 2.5
* DbUnit 2.2.2
* Junit 4.4Here you are

import javax.sql.DataSource;

import org.dbunit.DataSourceDatabaseTester;
import org.dbunit.IDatabaseTester;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:*testContext.xml" })
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })

public abstract class AbstractRepositoryTest {

 private IDatabaseTester databaseTester;

 private DataSource dataSource;

 @Autowired
 public void setDataSource(DataSource dataSource) {
     this.dataSource = dataSource;
 }

 @Before
 public void setUpDatabase() throws Exception {
    try {
      databaseTester = new DataSourceDatabaseTester(dataSource);
      databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
      IDataSet dataSet = getDataSet();
      databaseTester.setDataSet(dataSet);

      databaseTester.onSetup();
  } catch (Exception e) {
     e.printStackTrace();
     throw e;
  } } protected abstract IDataSet getDataSet() throws DataSetException ; @After public void tearDownDatabase() throws Exception { if (databaseTester != null) { databaseTester.onTearDown(); } } }

And here is a saimple example test for MyRepository.

import java.util.List;

import javax.sql.DataSource;

import org.dbunit.DataSourceDatabaseTester;
import org.dbunit.IDatabaseTester;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.csv.CsvURLDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;import org.junit.Assert;
import org.junit.Before;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

public class MyRepositoryTest extends AbstractRepositoryTest {

 private MyRepository myRepository;

 @Autowired
 public void setMyRepository(@Qualifier("myRepository") MyRepository myRepository) {
      this.myRepository = myRepository;
 }

 protected IDataSet getDataSet() throws DataSetException {
    // returns CVS files in the currenct directory
    return new CsvURLDataSet(getClass().getResource(""));
}

 @Test
 public void listMyXEntities() {
    List xList = myRepository.listXEntities();
    Assert.assertEquals(3, xList.size());
  }
}

I really like the simplicity that comes with annotations!

PS : In the AbstractRepositoryTest’s setUpDatabase() method, there is a try-catch. I added it because in  org.springframework.test.context.junit4.SpringMethodRoadie.java (v1.13), at line 335, there is a catch like :

catch (FailedBefore ex) {
}
So in case of any exception in set up, you loose it.




DDD : Beginning

30 09 2007

Domain Driven Design is a hot topic nowadays. Over the last month I have been diving into Domain Driven Design. I think DDD is a very good method who have been suffering that he/she cannot do real OO with layered arhitectures.

Ok, what is DDD? In the DDD book, it says :
“Domain-driven design is not a technology or a methodology. It is a way of thinking and a set of priorities, aimed at accelerating software projects that have to deal with complicated domains.”

“The premise of domain-driven design is two-fold:

  • For most software projects, the primary focus should be on the domain and domain logic; and
  • Complex domain designs should be based on a model.”
  • I am going to write few entries about DDD, which are going to include information about DDD , examples and some exeperinces.








    Follow

    Get every new post delivered to your Inbox.