DDD: Repositories & Factories

Repositories are used to re-create instances of objects from a data source.  Rather then placing the burden of retrieving data for object creation within our domain objects we should separate out that concern into "Repositories".  A repository is responsible for retrieving the data for our objects from our data store and re-creating instances of these objects using the data.  By abstracting the data retrieval operations out of our domain objects we provide greater flexibility within our domain.  We also ensure that our domain objects don't end up with knowledge of things they just shouldn't.   Since our objects aren't junked up with all the logic for data retrieval and object creation they can stay focused on representing our domain and the domain logic that makes up our applications.

After a repository has retrieved the data for a domain object it needs to re-create the domain object using the data.  There are times when the logic for performing the loading of the object from the data is appropriate within the repository, however, we'll often want to create a Factory object for this purpose.  A factory is responsible for the creation of objects.  The factory may be responsible for creating new object instances, as well as re-creating instances of objects from data.  By separating responsibility of object creation out of the repository we allow the repository to stay focused on its tasks of retrieving domain data out of the data store. 

Let's look at a quick example:

public class Customer {
   private string _firstName;
   private string _lastName;

   public string FirstName {
    get { return _firstName; }
    set { _firstName = value; }   
   }

   public string LastName {
    get { return _lastName; }
    set { _lastName = value; }   
   }
}

public class CustomerRepository {
   public Customer FindByID(int customerID) {
      IDataReader dataReader = ...;
      return new CustomerFactory().Create(dataReader);
   }
   public bool Save(Customer customer) {
      // save customer
   }
}

public class CustomerFactory {
  public Customer Create(IDataReader dataReader) {
    Customer customer = new Customer();
    if(dataReader.Read()) {
      customer.FirstName = dataReader["FirstName"].ToString();
      customer.LastName = dataReader["LastName"].ToString();
    }
    return customer;
  }
}

// UI Code
CustomerRepository repository = new CustomerRepository();
Customer customer = repository.FindByID(99);

// update fields/properties
...

repository.Save(customer);

By separating the responsibilities of data retrieval into our Repositories, and object creation into our Factories we allow our domain objects to stay focused on representing the domain.  We also allow our applications to become more de-coupled which allows us to more easily test our applications. 

 

# re: DDD: Repositories & Factories

Wednesday, August 11, 2004 6:14 AM by Shawn Oster    
I'm a Delphi developer and Object Persistence Frameworks (which this really is to my eye) are a big topic of discussion in the borland.public.delphi.oodesign newsgroup. I'd love to see more of your thoughts on Repositories. Especially in the following areas:

- Handling getting a list of customers, either all or by a search criteria (all customers that live in Colorado)

- Getting a customer by something other than an ID, say search by last name or address

- Handling the underlying repository magic, what is it backing into, how do you handle complex objects.

- How about aggregate objects, a customer that has an address object, does the customer repository instantiate an address repository internally?

In my own quest to make an Object Repositry / OPF those are the real world questions that you don't see any many answers for. Making that leap from theory to code is what I'm looking forward to hopefully seeing in future blogs.

# re: DDD: Repositories & Factories

Thursday, August 12, 2004 7:54 AM by Jiho Han    
I'm not sure whether this is limited to this repository idea but suppose that the customer object has an address object. And you call CustomerRepository.Save(customer). How will this handle the saving of the address object inside the customer object? Will it, in turn, call AddressRepository.Save(address) inside the CustomerRepository.Save method? If so, how can we make this an atomic transaction, preferably not involving Enterprise Services?

# re: DDD: Repositories & Factories

Thursday, August 12, 2004 11:43 AM by Steve    
Both very good questions. I'll try and formulate some thoughts around them as well as try and get some feedback from some others and blog whatever I come up with. Stay tuned...

# re: DDD: Repositories & Factories

Friday, August 13, 2004 6:11 AM by Kris    
I think the repository could support a strategy or specification interface when retrieving lists of entities.

The strategy interface could either specify the criteria, or perhaps the repository could query it directly to determine if an entity is a qualified match (eg. 'lives in colorado'.) Conceputally I like the latter approach, but it sucks in terms of performance. Its best to let the database do this sort of work, at which point your better off looking at a Query pattern I think.


# Repositories and aggregate objects...

Friday, August 20, 2004 7:04 AM by Jiho Han    
Repositories and aggregate objects...

# Udi's talking DDD

Saturday, September 18, 2004 12:48 PM by Steve Eichert    
Udi's talking DDD

# re: DDD: Repositories & Factories

Sunday, October 18, 2009 7:31 AM by Alessandro Brito    
Interesting article. I got a question: Factory objects should be placed in Domain layer Repository layer?

# info@shoppingnikesb.com

Thursday, October 22, 2009 10:36 PM by nike sb    
Eliminating the need for the extra trouble to wear cheap jordan shoes?nike sb?ugg boots sale?uggs?ugg sale?nike dunk?Gucci Shoes and nike sb for sale , in the extreme sports, look for happiness in life, with nike dunk high?nike sb dunk , showed off your tall body, do not worry about being laughed at, dancing new style discount ugg boots ?Christian Louboutin?nike dunk mid?Christian Louboutin Boots, for you to save money UGG Classic cardy boots will not forget UGG Classic Tall Boots?UGG Classic mini boots.

#

Wednesday, October 28, 2009 3:53 AM by ugg    

# Bailey Button Ugg Boots

Saturday, November 07, 2009 12:42 AM by Bailey Button Ugg Boots    
Your ugg boots uk article very interesting, I have introduced a lot of friends look at this Bailey Button Ugg Boots article, the content of the Classic Short Ugg Boots articles there will be a lot of attractive people to Ultra Tall Ugg Boots appreciate, I have to thank you such an article.

# re: DDD: Repositories & Factories

Thursday, November 19, 2009 2:49 AM by beads    

# Gucci Purse

Sunday, November 22, 2009 9:18 PM by Gucci Purse    

# ugg boots,ugg shoes

Monday, November 30, 2009 10:29 PM by onuggshoes.com    

# ugg boots,ugg shoes

Monday, November 30, 2009 10:29 PM by onuggshoes.com    

# re: DDD: Repositories & Factories

Thursday, December 03, 2009 4:41 AM by kkdai    

# re: DDD: Repositories & Factories

Friday, December 04, 2009 11:01 PM by wewe    
Yet, alltoo often, Hot Spots are hardly documented and over years of cheap uggs evolution, the source code that reifies them becomes cheap uggs entangled with the application specific code.



We base the technique for Hot Spot recovery on the design concept of template cheap uggs. We present the approach and the interactive analysis capabilities of SPOOL to visualize browse, and inspect Hot Spots in both separate and contextual form. The cheap uggs validated based on two industrial systems.

# jerseysleague

Friday, December 04, 2009 11:24 PM by nfl jerseys    
Old men wearing relics from another time -- nfl jerseysPittsburgh Steelers Jerseys Indians hats and collared shirts Arizona Cardinals Jerseyssip from wine glasses at a small table. Pittsburgh Steelers JerseysSitting there, they're the epitome of high Chicago Bears Jerseys, from the sweaters tied loosely around their necks to the Arizona Cardinals Jerseysway their legs are crossed over their nfl jerseyssockless Chicago Bears Jerseysankles and tasseled loafers.

#

Sunday, December 06, 2009 9:40 PM by edsss    

# re: Are you lazy loading your way to performance problems?

Sunday, December 06, 2009 9:41 PM by wedding dresses    
The perfect and accurate questions and 70-294answers in the Nortel PDF with the most functional quality has been designed for you in simexams.com which can help you get a good70-297 score in the coming Nortel. Once you have already checked outempire waist wedding dresses various Nortel Exam courses, it is very reasonable to make a decision of buying this Nortel PDF in our online store which is edited by the 70-298college senior experts and deserves the best approvals as the price is so cheap. The outstanding Nortel strappless empire waist wedding dressesPDF is written to the senior wedding dressescriterion in the subject.Aion Gold

# re: DDD: Repositories & Factories

Monday, December 07, 2009 11:20 AM by crvti    
Were a fine woman how few designer handbag decoration, had a section of fashionable favorable impression Thomas wylde Replica handbags, let your mood blossom in radiant splendor in the summer day, the delightful joy person's summer day fashionable Versace Replica handbags, let your delight and makings UP gets up! Sometimes the replica handbags looks like woman's label. A section elegant natural, material quality high quality Yves Saint Laurent Replica handbags , sees a woman's personal status and the makings sufficiently. This summer, regardless of being the color gorgeous leisure gucci shoulder bag, works the excellent dermis gucci messenger bag, has some unique details
designer pursehandbagsDiscount handbagsCheap handbagsMarni handbagsVersace handbagsThomaswylde handbagsJuicy Couture handbagsLoewe handbagsValentino handbagsAlexander Wang handbagsBottega Veneta handbagsKooba handbagsinsulated glassmarble composite slab|Marc Jacobs replica handbags|Bottega Veneta replica handbags|Burberry replica handbags|Cartier replica handbags|Celine replica handbags|Christian Dior replica handbags|Christian Dior bag|Coach replica handbags|Coach bagsreplica BreitlingReplica Breitling watchesladies breitling watchesbreitling seawolfbreitling BlackBirdbreitling Superoceanbreitling bentley watchbreitling bentley GTbreitling Montbrillantbreitling NavitimerIWC watcheReplica IWC watchesIWC PilotIWC InGenieurreplica iwc portugueseroyal oak watchesAudemars Piguet watchesAudemars Piguet Replica WatchBaume Mercier WatchesBell ross watchesBvlgari watchesBvlgari watches replicareplica jaeger le coultre watchesjaeger le coultre watchesReplica Cartier watchesCartier watchcartier tank francaise watchcartier tank watchchanel J12 watch

# re: DDD: Repositories & Factories

Monday, December 07, 2009 10:16 PM by erer    
The success of an object-oriented Audio software discount coupons development project highly depends on how well the designers can capture the Hot Spots of the application video software discount coupons, that is, those aspects that should be kept flexible to accommodate reuse and change.

# Abercrombie

Wednesday, December 09, 2009 8:52 PM by Abercrombie    
Online forums ed hardy are another Ed Hardy Clothing way of Cheap Ed Hardy finding great Ed Hardy Sale 50’s prom Discount Ed Hardy dress online. Cheap Ed Hardy Clothing You can Ed Hardy Clothing Sale find the right dress Discount Ed Hardy Clothing for you Abercrombie and Fitch by joining Abercrombie & Fitch a forum that Abercrombie Fitch discusses fashion Abercrombie and dresses A&F or posting abercrombie fitch sale questions regarding Cheap Abercrombie Fitch 50’s fashion Discount Abercrombie Fitch or seek Abercrombie and Fitch sale advice in getting Cheap Abercrombie and Fitch 50’s prom Discount Abercrombie and Fitch dress. Testimonials Abercrombie sale and advice Cheap Abercrombie regarding reliable Discount Abercrombie and top-notched Abercrombie Fitch Clothing designers can also be given from the forums. Abercrombie and Fitch Clothing Also they can give you dress shops online where high Abercrombie Clothing quality dresses can be found.

# ugg classic tall boots

Thursday, December 10, 2009 9:37 AM by ugg classic tall boots    

# www.focusugg.com

Friday, December 11, 2009 10:24 AM by ugg classic cardy cheap    

# discount rolex

Friday, December 11, 2009 12:13 PM by discount rolex    
designer tiffany jewelry
cheap jewelry

tiffany jewelry
silver bracelets
designer replica handbags
cheap nfl jerseys

NFL jerseys
footwear
gucci shoes
ugg store
ugg discount

ugg shoes
boots shoes
ugg discount
cheap ugg classic

fashion ugg
cheap ugg shoes
luxury ugg boots

fake ugg
timberland shoes online
buy timberland boots
cheap timberland
nike air max
discount air nike
nike af1 shoes

af1 shoes
replica rolex watches
rolex watches
ugg classic cardy
buy ugg boots
ugg discount
buy ugg boots

ugg classic
wholesale handbags
nfl football jerseys
nfl jerseys replica
NHL Jerseys

nfl football jerseys
bape shoes
boots shoes
buy ugg boots
ugg classic

ugg boots
Black Ultra Tall Ugg Boots
ugg boots
ugg boots

ugg discount
ugg shoes
discount ugg boots
cheap handbags
cheap nfl jerseys

fashion tiffany jewelry
buy cheap jewelry

tiffany bangles
buy tiffany jewelery
timberland boots
classic timberland

discount rolex
nike jordan
nike shox

Wholsale Footwear for sale
Wholsale Prada Shoes for sale
Wholsale Supra Shoes for sale
Wholsale UGG Boots for sale
Wholsale UGG Classic for sale
Wholsale UGG Ultra for sale

Wholsale Nike Air Max Shoes for sale
Wholsale Nike Shox Shoes for sale
Wholsale New Balance Shoes for sale
Wholsale Nike Basketball Shoes for sale
Wholsale Christian Louboutin Shoes for sale
Wholsale Timberland Boots for sale

Wholsale Nike Air Jordan Shoes for sale
Wholsale AF1-shoes for sale
Wholsale Nike Dunk SB Shoes for sale
Wholsale Gucci Shoes for sale
Wholsale Puma Shoes for sale
Wholsale UGG Bailey Button for sale

Wholsale UGG Classic Cardy for sale
Wholsale UGG Classic Short for sale
Wholsale Timberland Men's 6 Inch Boots for sale
Wholsale Classic Timberland for sale
Wholsale Kid's Timberland Boots for sale
Discount Kid's Timberland Boots

Discount Classic Timberland Short
Discount Timberland Men's 6 Inch Boots
Discount UGG Classic Short
Discount UGG Classic Cardy
Discount UGG Bailey Button
Discount Christian Louboutin Shoes

Discount Timberland Boots
Discount Nike Air Jordan Shoes
Discount AF1-shoes
Discount Nike Dunk SB Shoes
Discount Gucci Shoes
Discount Puma Shoes

Buy Footwear online
Buy Prada Shoes online
Buy Supra Shoes online
Buy UGG Boots online
Buy UGG Classic online
Buy UGG Ultra online

Buy Nike Air Max Shoes online
Buy Nike Shox Shoes online
Buy New Balance Shoes online
Buy Nike Basketball Shoes online
Buy Christian Louboutin Shoes online
Buy Timberland Boots online

Buy Nike Air Jordan Shoes online
Buy AF1-shoes online
Buy Nike Dunk SB Shoes online
Buy Gucci Shoes online
Buy Puma Shoes online
Buy UGG Bailey Button online

Buy UGG Classic Cardy online
Buy UGG Classic Short online
Buy Timberland Men's 6 Inch Boots online
Buy Classic Timberland online
Buy Kid's Timberland Boots online

[url=http://www.lookshoes.net/]nike shox[/url]







# re: Does an OR Mapper need to use dynamic sql?

Friday, December 11, 2009 12:16 PM by Cardy Ugg Boots    

# re: DDD: Repositories & Factories

Friday, December 11, 2009 12:18 PM by Cardy Ugg Boots    

# re: DDD: Repositories & Factories

Friday, December 11, 2009 12:23 PM by Cardy Ugg Boots    

# re: DDD: Repositories & Factories

Friday, December 11, 2009 12:25 PM by Cardy Ugg Boots    

# ecboots

Friday, December 11, 2009 8:47 PM by ecboots    

As you know, ugg boots are popular now. you can find online store supply discount ugg boots, as this is a reason for ugg boots. you can buy ugg boots on sale anytime anywhrere. Just a wind with uggs on sale.Not only are Ugg Bailey Button Boots incredibly trendy right now, but they keep your feet nice and cozy and warm.  Rarely does a hot style actually have a good function, too! like UGG cardy boots and ugg argyle knit boots.Anyway, they are amazing boots, keep your feet warm in the cool winter! So don’t be hesitate! Order one pair of the ugg boots, you will feel better in the future and be the leader of the future’s fashion !


# Puma Men's Future Cat GT Ferrari

Saturday, December 12, 2009 1:54 AM by discount puma shoes    
PUMA is the world-renowned multinational sports brand, provide the best puma sporting goods.Puma shoes is very popular by customers.Cheap puma shoes on sale,discount puma shoes, Free shipping and custom,6-8days to your door!!Top quality and lowest price!!Hot Sale Puma Men's Future Cat GT Ferrari,Puma Men's Baylee Future Cat II,Puma Men's Speed Cat,Puma Women's Engine Cat Low,Limited store,discount price,order it now!!!

# ugg boots class cardy

Saturday, December 12, 2009 2:04 AM by ugg boots class cardy    

# re: DDD: Repositories & Factories

Saturday, December 12, 2009 11:57 AM by Cardy Ugg Boots    

# re: DDD: Repositories & Factories

Saturday, December 12, 2009 11:59 AM by Cardy Ugg Boots    

# Kerja Keras Adalah Energi Kita

Saturday, December 12, 2009 2:36 PM by Kerja Keras Adalah Energi Kita    

# re: DDD: Repositories & Factories

Sunday, December 13, 2009 8:04 PM by Dragonsky money    
In the game a lot Dragonsky Nyangis very necessary,and you also can have a large number of Dragonsky Gold,you can do the task in the game to Dragonsky money to buy buy Dragonsky Nyang,you also can spend money tobuy Dragonsky Gold.

# re: DDD: Repositories & Factories

Sunday, December 13, 2009 10:36 PM by fff