When implementing a Onion architecture in ASP.NET MVC, it is my understanding that we should/could expose the IDataContext interface, which can be injected and referred to in the UI.
So basically in ASP.NET MVC, we could do:
_context.Products.Add(myBadProduct);
Doesn't that allow the UI layer to implement business logic? I believe that one of the basic philosophies of Onion is that it provides clear understanding what code goes into what layer and explicitly forbids the UI layer from implementing business logic.
If we are exposing the storage to the UI (with, or without SaveChanges
capability), we let the UI developers implement custom business logic.
This can be 'fixed', by exposing all operations to the underlying IDataContext, via a Domain Service such as this only.
To summarize my question into one sentence:
Should we allow the UI layer to touch the IDataContext or should we expose all our context operations only via a Domain Service? If we expose the IDataContext, is it also ok to expose SaveChanges method, defined in IDataContext (and implemented for example in the actual EfDataContext)?
I think the IDataContext should be exposed as readonly (i.e. without SaveChanges capability), for querying purposes, while the C_UD methods should be exposed as Domain Services. Right or wrong?