views:

60

answers:

4

Hello, I'm trying to get a handle on good DDD principles, so pardon this question if it is noobish.

Take an Author object that has a Books collection. I want an IsAwardWinningAuthor property on my Author, with each book having an IsAwardWinningBook property. Would I simply make IsAwardWinningAuthor loop through (or query, or whatever) the Books collection looking for any instance where IsAwardWinningBook is true? Is this the proper place for this logic, even if it may trigger a lazy load of all books from my database?

Thanks!

A: 

Question isn't about domain driven design at all. It's about technology.

From domain driven design perspective - it matters that You reflect domain correctly (in this case - Author is award winning when any book has won an award). In Your model database related performance problems does not exist.

Unfortunately - we cannot ignore them.

But it's a bit hard to help You. You didn't specify technology You are using. From mentioning lazy loading i guess You are using ORM. Then again - I got no ideas which one.

I've worked with NHibernate and it was smart enough to filter books by author id foreign key on lazy loading book collection. Therefore - it didn't cause selecting all books but just ones i needed.

Another common problem when using ORM You might not be aware of is so called select n+1 problem.


About ddd...

Don't stop and don't just assume that You understand it. It's a damn hard nut to crack.

Arnis L.
I am using ASP.NET and NHibernate. I didn't know if the best approach was to set up what I need, and then optimize it? I'm fully aware of my noobishness towards building effective architecture, and I know that nobody can give me all the answers, but I'd at least like to start in the right direction.
Mike C.
A: 

I use an ORM (NHibernate) heavily in my current project and I am starting to lean heavily towards just recording a piece of information if it's important. In your case I would just record on the Author that they are an award winning author. If I needed to find the book I could write a special query to find all their award winning books. It's much more efficient to do so then to load up all the books just to find a simple fact out.

ShaneC
Yes, I agree, but I'm working with a legacy codebase that I'm just starting to migrate.
Mike C.
+1  A: 

Don't try to solve potential performance issues too early in the process (but don't ignore them, either).

As always, how you actually execute the query depends on the nature of the data, and how it will be used. If you've got a batch process running through tens/thousands of records, you'd want to let SQL Server take care of it.

However, if you only need to execute the query once or twice, with fewer records, you can get away with an in-memory loop.

The missing piece to you solution is this: Use a Query Specification to wrap your query, and make your Author object use this new Query Specification.

Initially, just make it a simple loop. When you discover that it's too slow, replace the loop with a call to the database (via a FindAwardWinningBookCount method on your AuthorRepository).

The important part is this: Your Author object is no longer responsible for handling the query - it uses the Query Specification instead. Changing the Query Specification's implementation should not affect the behaviour of your Author.

See pages 229 to 234 in Evan's book for more detail.

Vijay Patel
Actually - I didn't say it's too early. I wanted more to emphasize that question isn't about domain driven design. :)
Arnis L.
Evan's book? Which one is this?
Mike C.
@Arnis: I guess I read *too* much into your answer :). Anyway, I've modified my answer now.
Vijay Patel
@Mike: . Eric Evans came up with "Domain Driven Design" back in 2004, in his book "Domain Driven Design: Tackling Complexity in the Heart of Software"
Vijay Patel
@Mike You haven't read Evans book? I strongly recommend to read it if You want to master domain driven design. and usually it's not enough to read it only once. :)
Arnis L.
I actually had it on my to-read list. Just wasn't sure which one you were talking about. Sounds like a good one.
Mike C.
A: 

I would model this so that Author has information whether she/he is AwardWinning. When books becomes AwardWinning, it notifies its author (by calling OnBookBecameAwardWinning method) that it became award winning. This method can be as simple as setting AwardWinning flag on the author, or much more complicated. It depends on particular case.

Szymon Pobiega
Yes, I'm coming to the realization that I need to rely less on aggregation.
Mike C.