http://blog.f12.no/wp/2009/02/02/repository-pattern/
Showing posts with label DDD / Domain Driven Design. Show all posts
Showing posts with label DDD / Domain Driven Design. Show all posts
Tuesday, 14 June 2011
Repositories vs DAOs
"Are Repository and DAOs the same thing with a different name?". The answer is not direct. Because they overlap a lot. Repositories and DAOs are two solution styles for approaching the same problem, with some small differences.
- DAOs are strictly tied to the underlying representation on a DBMS. The original specification is more generic, allowing for file-based persistence and the like. But the vast majority of DAOs are pointing to a DBMS. Moreover, commonly used persistence frameworks, such as Hibernate help a lot in managing database portability, so this is a smaller issue nowadays, than it used to be. As a result the DAO concept eventually downsized, to a "database access point" while Repositories are still intended to be generic.
- Repositories provide a more abstract view over the underlying data model, providing an interface strictly coherent with the domain. DAOs might be implemented basically in many ways, but frameworks and code generation tools tend to put the focus on the data structure rather than on the domain model. This is sometimes a tiny issue, sometimes just a matter of style, but in large systems can degenerate in a severe maintenance problem.
- Repositories enforce access to the persistence layer on a one-repository-per-aggregate basis, while DAOs are normally developed one-per-entity or one-per-table. So, repositories are more tied to the DDD concept of Aggregate Root and have a different granularity than DAOs. This definitely makes the most significant difference.
Repository Pattern
Repository means storage location for safety and preservation. Its a single place where you can find related items. This terminology is used by some frameworks like Spring. But whats the need of repository?
Example
Lets take an example. There is a big basket of toys. Toys contain Soft toys, wooden toys, miniature toys. If a kid wants miniature toys, he has to spill all the toys from the basket and separate the needed ones. Kid’s dad doesn’t want him to do that. He separates the miniature toys, that becomes a miniature toy repository, and gives it to child. Goal is never allow the kid to put hands on the basket.
Kid’s dad does the job of maintaining toy basket. Once the kid is done playing, he puts the toys back into the basket. When kid wants to play he gives the kid whatever toy he wants. When dad brings a new toy for the kid, he is going to put that in the toy bag. In general, dad maintains the toy repository.
Implementation
Repository pattern, as described in Domain Driven Design, in a typical java environment backed by frameworks like Hibernate and Spring.
Child.java
Get all the toys
Similarly we can have wooden toys, metallic toys and so on.
Get the toy repository
We are not allowing the kid to put hands on the toy bag. Same way, business logic has no knowledge of database and related implementation logic. All that business logic knows is how it can get the needed entity from repository, and how it can give the entity back to repository. It is the responsibility of repository to interact with the data source.
Advantage
This pattern has several advantages.
Example
Lets take an example. There is a big basket of toys. Toys contain Soft toys, wooden toys, miniature toys. If a kid wants miniature toys, he has to spill all the toys from the basket and separate the needed ones. Kid’s dad doesn’t want him to do that. He separates the miniature toys, that becomes a miniature toy repository, and gives it to child. Goal is never allow the kid to put hands on the basket.
Kid’s dad does the job of maintaining toy basket. Once the kid is done playing, he puts the toys back into the basket. When kid wants to play he gives the kid whatever toy he wants. When dad brings a new toy for the kid, he is going to put that in the toy bag. In general, dad maintains the toy repository.
Implementation
Repository pattern, as described in Domain Driven Design, in a typical java environment backed by frameworks like Hibernate and Spring.
Child.java
public class Child {
public void Play(ToyRepositoryBase toyRespository) {
//Gets all the toys seperated by Dad
List<Toy> toys = toyRespository.GetToys();
//Now child starts playing
System.out.println("Child is playing with " + toyRespository.GetType());
//Child is done playing. Now dad puts back the toy into the bag
toyRespository.PutToysBackIntoBasket(toys);
}
}
Get all the toys
//Toy is a abstraction of miniature toy or soft toy or wooden toy
public abstract class Toy
{
}
//Miniature toy is concrete class.
public class MiniatureToy extends Toy
{
}
Similarly we can have wooden toys, metallic toys and so on.
Get the toy repository
//This toy repository is abstract of miniature toy repository, wooden toy repository or soft toy repositoryI have omitted other sub classes like WoodenToyRepository for brevity here.
public abstract class ToyRepositoryBase {
//Child calls this to get toys before starting to play
public abstract List<Toy> GetToys();
//One child is done playing, dad puts the toys back into the basket
public abstract void PutToysBackIntoBasket(List<Toy> toys);
}
//Concrete class of miniature toy repository
public class MiniatureToyRepository : ToyRepositoryBase {
//Consider this as action performed by dad,
//who gives the miniature toys to child
@Override
public List<Toy> GetToys()
{
List<Toy> miniatureToys = new ArrayList<Toy>();
return miniatureToys;
}
//This is action performed by dad. Once child is done playing,
// he puts back the toys into bag
@Override
public void PutToysBackIntoBasket(List<Toy> toys)
{
//Here you can use cache or database
}
}
We are not allowing the kid to put hands on the toy bag. Same way, business logic has no knowledge of database and related implementation logic. All that business logic knows is how it can get the needed entity from repository, and how it can give the entity back to repository. It is the responsibility of repository to interact with the data source.
Advantage
This pattern has several advantages.
- No duplicate codes needed. If you got another child who wants wooden toys, same logic works.
- Business logic is simplified, since its interactions is only with repository and repository entities.
- Less scope for errors
- Strong typing, since Miniature toy repository gives miniature toys.
- Easy to test.
Subscribe to:
Posts (Atom)