2

https://i.stack.imgur.com/YZXZN.png (I'm currently not allowed to embed images)

I could really use some help with my class model above. I'm ashamed to say that I have been one of "those" developers that learned object orientation at university, wrote the exams, aced them but then never set about implementing the principles in my real world code. I never truly sat down and considered my application design before beginning the codification of it. Thus my design and coding skills have been slowly dying and stagnating under the weight of monolithic legacy banking application development and maintenance. After years of this I've decided that it's definitely time for a change! I've been delving deep into the world of design patterns, DDD, NoSQL, DI, etc etc. The last 2 weeks have been a really intense experience for me, and at times I think I was nearly brought to tears at the sheer volume of best practices and tech that I had missed while working for large corporations and banks. I simply couldn't believe how far removed I had been from cutting edge tech and good design approaches for so long, and the sudden swathe of everything threatened to send me into a state of coding paralysis! I simply could not start coding, as I felt my design needed more tweaking, or I needed more studying on a particular topic. Enough is enough though, and I need to crack on and at least make a first iteration on the project.

Anyway, enough of the drama, on to my issue:

I have begun work on the model creation for my golfing app. Wanting to adhere somewhat to DDD and also wanting to make use of NoSQL (RavenDB), I set about with the following requirements.

  • My platform stack is Windows / IIS / MVC 3.0 / RavenDB
  • I need to find my aggregate roots! I have set about defining them as the only elements in my system that are capable of persisting in their own right. Everything else I've simply deemed a "sub-component" of the aggregates. Note that no real behaviour has yet been defined.
  • My aggregate roots will be the only classes to actually persist in my RavenDB doc store, and they will persist "as-is". Having large tree-like class structures would appear to be a best case scenario for RavenDB in terms of performance benefits realised.
  • I don't feel the need for a repository layer (been following some of Ayende's posts), as the RavenDB API feels fluent and quite lightweight. I'll be simply opening and closing my sessions via Custom Action Attributes on my controllers where required. I've seen that without the repository layer testing might be tricky, but surely I should be able to simply mock some "in-memory" domain objects?
  • Writes to the DB will happen in a separate service layer
  • At one point I stopped and asked myself: "Where on earth am I going to put my domain behaviour!?". The general consensus from searching the web would seem to indicate I should leave my domain (entities) void of any behaviour (business logic) and have it all handled in my service layer. But after reading some Eric Evans, I'm convinced that as much of my domain behaviour should exist right there... in the domain!

Questions - As a bona fide noob in the area of DDD and architectural design, am I at least on the right track, or am I destined for destruction? - Any thoughts, admonishments, constructive criticism and insights into the above would be greatly appreciated!

1
  • Who said the domain should not contain behaviour? Seriously, where is this coming from, because I hear it all the time but I've never seen any well established developers or books say it? To help you/confuse you further, please take a look at my blog on this subject: lucidcoding.blogspot.co.uk May 14, 2012 at 8:13

3 Answers 3

3

To counter being overly academic about it all and be stuck in analysis too long: First make it work. Then make it pretty.

Put behavior near data as much as possible. Use services where you can't cleanly assign a responsibility to a class (e.g. should a 'transfer money' method be on an SavingsAccount class?). Services can be part of an aggregate.

Do use repositories (I don't agree with Ayende). You mention using a separate service layer for DB writes. Repository is a perfect interface to put that layer behind. It's also a perfect testing seam.

Didn't look at your class diagram thoroughly, but you may be overusing inheritance here and there. Favor composition over inheritance. Inheritance can rear it's ugly head quite quickly.

When choosing aggregate roots, an important criteria is life cycle. When the aggregate root dies, everything else in the aggregate dies also. The aggregate root is also in control, everything outside the aggregate passes through it. When in doubt just create a lot of them (single entity aggregate). With a document database you would typically store a document per aggregate, so that does matches somewhat with how you choose them. Store IDs of references to different aggregates.

1
  • Thanks so much for the tips! Love the general rule on when to use a service! Was wondering why I would want a service layer in the first place if I felt that all behaviour could be encapsulated in my model. I'm also leaning towards making use of repositories as I can see that my "reads" are going to require some logic to correctly build up my classes and there's no way I want that logic dispersed around the project. I do want to build my classes/aggregate roots with future performance needs in mind and can see that performance would be better on a larger aggregate root.
    – ProxyTech
    May 12, 2012 at 23:57
0

So yeah, going down the rabbit hole will not increase your productivity in short term, but may help you mature as a developer in long term. There is so much to DDD, NoSQL, etc that you could spend years just learning.

If you want your next project to be successful, my advice would be to stick to what you know, and introduce new techniques gradually so that you can always feel in full control, and not depend on "best practices" someone has to put up for you.

3
  • Fantastic advice! I was pretty much thinking along these lines, but having someone in the community validate my position just helps give me the gentle prod I need to get going. It can be quite daunting developing by ones self when one has always been used to the luxury (yes at times pain) of large development teams to help focus and guide ones development efforts.
    – ProxyTech
    May 13, 2012 at 0:04
  • 2
    I totally disagree with this. Don't just 'stick to what you know' - learn from what you know and improve on it, and learn from the 'best practices' of people who have dealt with what you're doing and have more experience than you. May 14, 2012 at 8:11
  • @PaulTDavies I guess the take-away for me from the above, was not to simply dismiss the wealth of knowledge and best-practices out there, but at the same time not to let the desire to gain the knowledge paralyse one into never actually taking that first step and putting some code down.
    – ProxyTech
    May 16, 2012 at 0:15
0

Firstly, may I congratulate you on deciding to take the steps to try and become more professional. I despair at the lack of profession in this industry and sometimes feel like I'm walking amongst 80% cowboys/hackers 20% professionals.

To your question:

  • Have you read this article by Vaughn Veron? If not, you should. It provides an excellent guide to designing aggregates, which I think is underrated in its complexity.
  • Looking at your model, I'm not sure if you actually have defined aggregates? I can see you have identified aggregate roots, but the aggregates should have clear boundaries and be separate from other aggregates (i.e. don't have entities referencing other aggregate roots, let them reference their ID). The property name RefereeUserIDList hints that you are in fact doing this, but the diagram shows it holding reference to the actual 'User' aggregate root?
  • In terms of identifying aggregates & roots & the model design, I don't really think we can help you here as this is entirely circumstantial to the behavioural requirements. I will say though: try to base your design on behaviour, not data structure. It's a difficult mindset to shift to, but try not picturing the database structure.
  • I haven't read what Ayende has said about repositories, but as long as you can mock the Raven API (which I assume you can given he made Rhino mocks) then it shouldn't be a problem.
  • Possibly most importantly, do not put all your domain logic into the service layer. You'll end up with an Anemic domain model, which is the DDD equivalent to the anti-christ.
  • Personally when learning DDD I understood all of the principals but struggled when attempting to turn theory into practice. If I'm honest I'd say I've only really been successful with it since I understood the principals CQRS which compliments DDD. I'd really recommend watching some videos on the subject by Greg Young.
2
  • Yes, I can see where you're coming from Dave with regards to my aggregates. I think I've kept the boundaries fairly consistent, but I am definitely breaking the "clear-boundaries" rule with regards to the RefereeUserIDList. My issue is that I've gone about attempting 2 paradigm shifting approaches. I'm using RavenDB (NoSQL) and attempting a more DDD approach with the design. It was tough in the beginning, as I started out with one massive aggregate root (therefore one aggregate). I thought this would be awesome for RavenDB and performance, but definitely not for architectural simplicity.
    – ProxyTech
    May 18, 2012 at 19:50
  • Think I can totally relate with your last point. I really feel like I have a firm grasp on a lot of these theories and best practices, and then I start with a fairly simple contrived real world example and I start second guessing myself and wondering if I've just missed the boat somewhere. I eventually have just had to make the decision to "start somewhere" and PROMISE myself to come back and refactor code and design at a later stage as my understanding and knowledge increases (famous last words for a developer). Thank you for your words of encouragement and help!
    – ProxyTech
    May 18, 2012 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.