Reputation: 5220
Lets say I need to call multiple services that insert some records with EF in the same transaction which are for inserting Person
and Unit
.
I'm uncertain for whether I should create a new operation contract named AddPersonAndUnit
and use TransactionScope
in that method and calling it from the client OR don't create any additional method and just use TransactionScope
in the client (ASP.NET MVC client) and call AddPerson
and AddUnit
which are already existing.
For single responsibility and moving all business logic into service layer, I think defining an extra method in service layer and calling it from the client seems a better choice but on the other hand just calling these multiple methods from the client requires less effort.
What do you think with the respect of a good design choice? Do you think dealing with the transactions in client is "bad"? Is it worth it to create a new method for this?
Upvotes: 0
Views: 593
Reputation: 4895
Based on your question, you're having two projects (ASP.NET MVC
& WCF
) and you want to make sure Person
and Unit
are being inserted/updated transactionally
Taking those two as inputs
Let's do quick analysis about option 1 (Use TransactionScope in client code (proxy class))
Efforts needed
OperationContract
and ServiceBehavior
) to support transactionOperationBehavior
) to suppor transactionbinding
configbinding
configurationPROS (CLAIM TO BE):
CONS:
Since both application code (MVC
) and service code (WCF
) are being controlled fully by you, you can make sure that you ALWAYS use the transaction when insert/update Person
and Unit
. However, I would prefer to make it as black-box
service, so that I could expose it to other 3rd client or give the service to another developer to use without worrying about data inconsistency (what if they forgot/intentionally skip the transaction?). Even though you can force the client to ALWAYS use transaction when calling the web services (by using TransactionFlowOption.Mandatory
option), I think the good practice is only expose minimal service for client to use
Single Responsibility is also another concern
Person
& Unit
)Hope it helps,
Sorry for bad English
Upvotes: 1