I just did an experiment where I rebuilt my forum application using nothing but a Linq2Sql designer, some viewmodels, and a base controller class. That’s it.
I had the forum controller and it’s views up and running exactly as I wanted within about an hour of casual coding (kids kept bothering me).
Yes, the controller is rather fat. I’ll show you an example below for those who are curious. The end result though, is that in doing so, 99% of my logic problems vanished immediately.
I have to wonder now, having spent nearly a year learning domain driven design, and how to use common tools like nhibernate, castle and automapper, was it worth it?
I happen to like, even prefer, the coding style below. I can actually “see” what’s going on. I have a hard time doing this when concerns are separated into various classes.
[HttpGet]
public ActionResult Edit(int id)
{
// just grab the entity
Forum entity = venueContext.Forums.Single(x => x.Id == id);
// and fill out the model - possibly trancating title later
return View(new ForumEditModel
{
Id = entity.Id,
ParentId = entity.ParentId,
Sequence = entity.Sequence,
Title = entity.Title,
Description = entity.Description,
Moderated = entity.Moderated,
Enabled = entity.Enabled,
Visible = entity.Visible
});
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Edit(ForumEditModel model)
{
// Locale.validation_error will be replaced later with specific things
// forum cannot parent itself
if (model.Id == model.ParentId)
ModelState.AddModelError("parentid", Locale.validation_error);
// title must not be null, empty, or whitespace
if (string.IsNullOrEmpty(model.Title) || string.IsNullOrWhiteSpace(model.Title))
ModelState.AddModelError("title", Locale.validation_error);
// ensure that description is at least empty and not null
if (string.IsNullOrEmpty(model.Description) || string.IsNullOrWhiteSpace(model.Description))
model.Description = string.Empty;
// redisplay on error
if (!ModelState.IsValid) return View(model);
// grab the entity
Forum entity = venueContext.Forums.Single(x => x.Id == model.Id);
// update it's values
entity.ParentId = model.ParentId;
entity.Sequence = model.Sequence;
entity.Title = model.Title;
entity.Description = model.Description;
entity.Moderated = model.Moderated;
entity.Enabled = model.Enabled;
entity.Visible = model.Visible;
// and save it
venueContext.SubmitChanges();
// redirect to either the parent detail or overall manager
if (model.ParentId.HasValue)
return RedirectToAction("Detail", new { id = model.ParentId.Value });
else
return RedirectToAction("Manager");
}
Honestly though, with some time away from nhibernte, I think I can sum up my main beef with it now. The first sentance on nhforge reads:
“NHibernate is a mature, open source object-relational mapper for the .NET framework.”
This is a bit of a missnomer. It should read:
“NHibernate is a mature, open source object-relational mapper for the .NET framework, intended for the domain driven developer.”
I like having both back references and foreign keys in my entities. I like being able to update a foreign key and save the entity, knowing the results will be accurate on next select. I use the back references for building tree branches among other things, that would otherwise required multiple (manual) calls to the databse. With nhibernate, you can have one, or the other, but not both.
With other ORMs it is my choice what to include in the entities. With nhibernate, this choice has been taken away. Those decisions were made, of course, with domain driven development in mind.
It’s a wonderful product, and does what it was intended to do, it just didn’t suit my needs. I just got wrapped up in the hype and trendiness of it, and felt driven to learn it. What I walked away with was not only a better knowledge of domain driven development, but the variety between the various ORM’s as well as what kind of developer I am at heart.
And knowing that, I think, made it all worth the while.
That’s funny, my kids always bother me on the computer too.
This is just my 2 cents, but in my experience each developer has their own innate style. You can train yourself to code in just about any way but there are definitely certain styles that suit you best and others that don’t. I really believe that coding is like playing music. With the right style guide or coding standards you can work in harmony as part of a team on anything from a small code duet to a full application symphony.
I know that DDD is all the rage right now, but the primary technical imperative of any code that you write is to reduce complexity. If you’re writing code that makes things simpler to you, than you’re getting the most important thing done right, and you can attack secondary goals as you’re able. But if your code is not making things simpler for you than you’re missing job one.
I will also throw in that this code seems much more readable than any of the DDD stuff I’ve seen you or anyone else post. That may just mean I’m wacky though.
If you’re not making people’s heads spin, then what are we learning. That’s exactly what this forum is for.
I also think the value is even more important than “Now I know more about DDD.” Now you know more about programming. There are always things you can take with you and tools you can apply elsewhere. The thing I’m proudest of in my CSS work is the OO style components I’ve created. The thing I’m proudest of in my OOP is the POSH semantics I picked up in XHTML. Ideas hook up with other ideas in ways you never would have expected. That’s why I keep learning anything I can get my hands on, sometimes just by trying to make it through to the end of your DDD code. (Confession: I don’t often make it before my head implodes. )
Your really doing my head in!! Pick what works for you, KISS and stick with it! I personally feel your project isn’t complex enough to warrant full DDD. Hell, I’ve still to work on a full DDD project!
We use all the tools above because we have been able to put together a friction-less development cycle that works for us. We could do that because we have people that have experience with them and can knowledge share.
“Was it worth it?” I would like to think you think it was. While you might not use them, you have that knowledge, you know the kinds of problems they solve, and can now make a fairly well informed decision about if you should them, for this project or others. I’m personally feel gaining any knowledge is worth it
I agree D. Learning itself, is worth it, regardless of what you’ve actually learned. As you said, I can see business solutions that might need that kind of attention. This one is just not one of them.
And I apologize for making your head spin. Heh. Until I felt like I really knew the patterns and ideology behind them though, I simply had to keep plugging away and asking questions.