most of the time in the service code I would have something like this:
public SomeService : ISomeService
{
ISomeRepository someRepository;
public Do(int id)
{
someRepository.Do(id);
}
}
so it’s kinda redundant
so I started to use the repositories directly in the controller
is this ok ? is there some architecture that is doing like this ?
I disagree with this one.
If business logic is where it should be – in domain model, then calling repo in controller (or better – use model binder for that) to get aggregate root and call method on it seems perfectly fine to me.
Application services should be used when there’s too much technical details involved what would mess up controllers.
I believe we are talking about 2 different things here. I suspect that Your ‘model binder’ means using model simultaneously as a view model too and binding changed values from UI directly right back to it (which is not a bad thing per se and in some cases I would go that road).
My ‘model binder’ is a class that implements ‘IModelBinder‘, that takes repository in constructor (which is injected and therefore – can be extended if we need caching with some basic composition) and uses it before action is called to retrieve aggregate root and replace
int idorGuid idorstring slugorwhateveraction argument with real domain object. Combining that with input view model argument lets us to write less code. Something like this:In my actual code it’s a bit more complex cause it includes ModelState validation and some exception handling that might be thrown from inside of domain model (extracted into Controller extension method for reuse). But not much more. So far – longest controller action is ~10 lines long.
You can see working implementation (quite sophisticated and (for me) unnecessary complex) here.
As You can (hopefully) see, this kind of approach actually almost forces us to move towards task based app instead of CRUD based one.
…and having new abstraction layer that invites us to mix infrastructure with domain logic and lose isolation of domain model.
I’m not sure if I did. I don’t think that I’m enlightened myself. 🙂
Here is my current model binder base class. Here’s one of controller actions from my current project. And here’s “lack” of business logic.