15

I have been reading this ebook about DDD and it says that only highly complex systems are suited for DDD architecture. This leads me to second guess my decision to move more towards DDD as my architecture.

I am converting a classic ASP application over to .NET section by section. It includes a robust product categorization scheme and shopping cart which gets ~100-200 orders per day, and a video section similar to YouTube (videos and social features like rating, commenting, etc.). Since I have converted it into chunks, I want to treat each area of the site as a separate project.

The site continuously gets new features and needs to be easy to maintain and update.

Right now I am just using a basic homemade ADO.NET DAL with BLL and DTOs that act as a common layer.

Would it be better to go with a different architecture than DDD for this project? I am new to architecture and want to use some sort of pattern as a guide that I can follow throughout the conversion process to avoid the dreaded spaghetti anti-pattern.

If not DDD, then what? Still trying to find a good direction. It needs to be fast and easy for me to get started without being a complete expert as I am still learning.

Darren
  • 138
  • 1
  • 8
jpshook
  • 4,834
  • 6
  • 36
  • 45

3 Answers3

45

DDD is not an architecture.

It's a philosophy of design, you cannot just rename all your FooDAO's to FooRepositiories, throw in an Anti-Corruption Layer and call it DDD.

It stands for Domain Driven Design. It puts a focus on the models that you use to solve problems in your specific domain. What Eric Evans is suggesting is that if your site is simply a form to join a mailing list there's no reason to spend days in front of whiteboard playing with models. It's my opinion if there's only a single context in your domain you don't need DDD. More on that in a bit.

There's a famous quote:

“There are only two hard problems in Computer Science: cache invalidation and naming things.” — Phil Karlton

And DDD does have patterns to address these. It offers ubitiquous language as pattern to tackle naming, and it offers the oft misunderstood collection of patterns: Repository, Aggregate, Entity, and Value Object to tackle model consistency (which I will lump cache invalidation into and won't cover further here).

But I would say most importantly it adds a critical 3rd item (not off by 1 problems):

Where to put stuff

Where to put code and logic. For me, the most fundamental concept in DDD is that of context. Not all problems are best served by the same model, and knowing where one context ends and another begins is a critical first step in DDD.

For instance, at my company we work with Jobs, Jobseekers and Recruiters. The world of a jobseeker is very different from the world of a recruiter and they both look at a Job differently. As an example, In the world (context) of Recruiters they can post a job and say

I want to make this job available in New York, Austin, and San Fran.

In OO speak: One Job has one or many Locations.

In the world of the jobseeker a job in LA is not the same job as a job in Boston. Nevermind if they are the same position at the same company. The difference in physical location is meaningful to the the jobseeker. While the recruiter wants to manage all Widget Manager jobs from a single place even if they are spread out around the country, a Jobseeker in New York does not care if the same position is also available in Seattle.

So the question is? How many Locations should a Job have? One or Many?

The DDD answer is both. If you're in the context of jobseeker then a job has only one location, and if you're a recruiter in that context a job can have many locations.

The Jobseeker context is wholly separate from the Recruiter Context and they should not necessarily share the same model. Even if in the end of the day all the jobs end up in the same DB (another context itself perhaps), sharing models between contexts can make a model a jack of all trades and master of none.

Now this example is very specific to the Domain of recruitment and jobseeking. It has nothing to do with Entity Framework of ADO or MVC or ASP. DDD is framework agnostic.

And it is DDD heresy to claim that framework A or B makes your architecture DDD. The whole point of DDD is that a model should serve the needs of a specific Context within a particular Domain. Frameworks can only support that and make it possible, they cannot do:

$ dddonrails new MyDDDApplication
$ dddonrails generate context ContextA
$ dddonrails generate context ContextB
$ dddonrails generate model Widget ContextA
$ dddonrails generate model Widget ContextB
$ dddonrails start

To specifically address the question, "To DDD? Or not to DDD?" The good news is you don't have to decide at the onset, "This is going to be a DDD project!" DDD requires no toolset other than the willingness to think hard about the problems you're trying to solve and ask is my code helping or hurting me?

The bad news is DDD requires a serious commitment to look at and challenge your design, asking every day "Is this model making the problems in this context as easy as possible?"

But separate the somewhat tactical and practical concerns of what presentation framework (MVC or no MVC) and persistence framework (Entity Framework?) from the task of modeling your business domain. If you want to start now, think about what contexts are in your app. Some questions to ask:

  • Are multiple areas of the application solving different problems with the same basic data?
  • How many teams work on the app?
  • How do they integrate with each other?

Mapping these out is called Drawing a Context Map and it's an important first step to starting to do DDD.

I encourage you to checkout the ddd website for more. There's some good eric evans videos on qcon too. You may also want to read the Eric Evans' book Domain Driven Design, he has many more examples.

Kyri Sarantakos
  • 958
  • 9
  • 15
  • 1
    Great answer. DDD is a design paradigm, and it is not trivial to apply it correctly, framework or not. – Can Gencer Mar 23 '11 at 23:45
  • 2
    Good description for DDD. I would also like to add that the last 2 Ds in DDD are very important and often overlooked. They mean that the **Design** is **Driven** by the domain. Your business model drives the design. Like the Ds in TDD. – Iulian Margarintescu Mar 24 '11 at 11:35
  • @IulianMargarintescu good point I've edited the answer a bit to emphasize the separate concerns of infrastructure related to presentation and persistance and modeling the domain from the business. – Kyri Sarantakos Mar 24 '11 at 13:00
  • @Kyri Sarantakos - Thanks for the philosophical answer. I have read some about DDD but mainly looking for concrete advice on how to implement the patterns/architecture that most people use. My question was just not phrased correctly. I will read more about DDD before I move further in that direction – jpshook Mar 24 '11 at 17:13
  • @Developr don't fret ddd is not an implementation pattern, it is as @cangencer pointed out, a design paradigm. You don't need to give up on it, ddd is very simple at its core: constantly challenge your model, work hard to understand your domain. My advice for those wanting to get started is worry less about the ddd building block patterns, like entity, value object, aggregates, and repository. And instead focus on identifying bounded contexts. If you can clearly define contexts in your domain then you're 70% there. Understanding context is by far the most important concept in ddd. – Kyri Sarantakos Mar 26 '11 at 07:54
3

I would strongly consider looking into MVC and implementing the system as described in the sample NerdDinner website. If you use Linq to SQL or ADO.NET Entity Framework you get the domain layer for free as well as an ORM. This takes care of all data access and makes querying and filtering your domain objects a breeze.

cheers

Winger
  • 676
  • 3
  • 7
  • 1
    I'd agree with this. You get some basic patterns for free, and the code winds up being very simplistic and maintainable. I would definitely recommend ditching the custom ADO layer and using Entity Framework. Why reinvent the wheel and have to worry about maintenance and security? – drharris Mar 23 '11 at 18:06
  • The current site already has a few web forms (and a ton of ASP files). So far, I have been unable to get MVC running in the same site. Also, it is all just a web site project in VS2010. Any thoughts? – jpshook Mar 23 '11 at 18:12
  • 1
    If you're converting over a classic ASP site you are looking at a re-write. How about creating the new site and adding to it bit-by-bit. Once the new site goes live and replaces all functionality in the old site you can turn off the classic ASP site. – Winger Mar 23 '11 at 18:16
  • ASP.NET MVC is excellent, but you can do it with or without Domain-Driven Design, so this doesn't really address the question. – Jeff Sternal Mar 23 '11 at 18:45
  • @Winger - thanks for your comment. I am rewriting, but the ASP site changes so often and it is really only me doing the main coding so I would have no way to get both sites in sync and done. I have already got authentication working between Classic ASP and .NET side, so there should be no problem just having it all running simultaneously and updating section by section as I am able to. – jpshook Mar 23 '11 at 18:47
  • @Developr You're welcome. Sounds like you are headed in the right direction. Good luck on your project! – Winger Mar 23 '11 at 18:56
1

I have had a TON of success with the Onion Architecture... http://jeffreypalermo.com/blog/the-onion-architecture-part-1/ You can get a sample of this architecture (which uses DDD, MVC, NHibernate, Test Driven Development) from Code Camp Server....http://codecampserver.codeplex.com/

user673580
  • 11
  • 1