0

I have an application that is trying to follow Domain Driven Design to a certain extent. I have a Domain layer, and the classes inside have parameterised constructors.

My ViewModels of course have parameterless constructors, to allow for model binding. However one page is giving me problems.

One of my pages displays a List of Cars, which the user can edit the details of and then via JQuery POST the data of one car to the back end. Car is a Domain object, and has only a constructor with parameters (I do my business logic checks inside the constructor).

My code at the backend looks like this

[AjaxOnly]
public JsonResult Update(Car car)
{
    //...
}

Of course this fails, because Car is a Domain class and has no parameterless constructor.

I would like to know if there is a recognised best practice for this scenario. I can think of two, possibly three options:

1) Create a parameterless constructor for the Cars domain class. Decorate it with

[Obsolete("For model binding only")]

to prevent users calling it directly. Then in my JsonResult method immediately call the parameterised constructor. Unfortunately this means changing my private setters to public in the model.

2) Accept a FormCollection, extract the values, and call the parameterised constructor. Which is similar to 1) but no change to the private setters is required.

3) Perhaps a custom model binder? I do this for my MVC controllers already for IoC but don't know how it would work for non-Controllers.

Is there a best practice way to handle this?

2
  • You description of problem smells of anemic domain model.... Commented Jul 13, 2015 at 9:20
  • I haven't given a full description of Car, but it does have behaviour. I would not say it is anemic. Commented Jul 13, 2015 at 9:29

2 Answers 2

2

Your description smells of anemic domain model. Your Update method let me think of a CRUD approach to the DDD. This is wrong.

Your Car domain model should have each type of updatable command (e.g. UpdatePrice, UpdateAvailability supposing you are developing administration bounded context). And so you presentation layer, in other words your ASP.NET application should have:

[AjaxOnly]
public JsonResult UpdateColor(UpdateColorViewModel vm)
{
    Car car = repository.getById(vm.id);
    try
       car.UpdateColor(vm.color);
    catch
       // return some sort of errors

    // return some sort of ok
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your operations may be segregated into focused commands, but your choice of language is still very poor for DDD. Whenever you see Create/Update/Delete something is wrong with your ubiquitous language.
1

If you don't have, strictly speaking, anemic domain model then your problem is your UI. To apply DDD you need task-based UI design. Without a good task-based UI you are stuck; physically (your architecture) and, what is worse, mentally; in anemic model because your system doesn't know what the user is doing.

Once you know, in your UI layer, what user is doing; you can provide commands from the UI to domain layer actions and you never have to "create" a version of a existing car with updated atributes. Just create the command with the info needed to perform an action.

marianoc84 (+1 to him) gives you a simple but effective example.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.