c# - Should I hide DTOs and View Models behind interfaces or abstractions? -
in there value in using ioc resolvable interfaces specify dtos?
fer example:
private readonly igettransactionsquery _query; private readonly icreatetransactioncommand _createcommand; public transactionscontroller( igettransactionsquery query, icreatetransactioncommand createcommand) { _query = query; _createcommand = createcommand; } [enablequery] public iqueryable<itransactionquerymodel> get() { return _query.execute(); } public async task<ihttpactionresult> post(icreatetransactionmodel transaction) { if (!modelstate.isvalid) { return badrequest(modelstate); } await _createcommand.execute(transaction); return created(transaction); }
here, using itransactionquerymodel , icreatetransactionmodel, instead of concretions. there business value here? scenarios can benefit approach? can think of few, wanted consensus of use case scenarios
note: referring controller "action" methods, not constructor, benefit of having ioc obvious
there 2 common reasons use interfaces:
- as abstraction hide behavior.
- to allow set of types expose same data members, used if same type.
abstractions allow intercept, mock or replace behavior without having change consumers of these types. needed if such type contains behavior needs extended or replaced.
in case of dtos, unlikely have behavior needs abstracted. matter of fact, objects should not have of such behavior. makes unreasonable hide dto behind abstraction reason.
your application might have data objects have things in common. instance, might decide entities in application should have id
property. can define ientity
interface or entity
base type contains id
. allows define methods operate on base type or interface, instead of having specify them on , on again each of entities in system.
in case of dtos however, unlikely have other dtos have same set of properties. name itransactionquerymodel
implies, set of data defines 'transaction query model'. in other words, have one-to-one mapping between itransactionquerymodel
abstraction , implementation. useless.
Comments
Post a Comment