yakya
yakya

Reputation: 5220

WCF TransactionScope in Client or Service

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

Answers (1)

Kien Chu
Kien Chu

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))

  1. Efforts needed

    • Modify Service Contract classes (OperationContract and ServiceBehavior) to support transaction
    • Decorating existing methods (OperationBehavior) to suppor transaction
    • Modify binding config
    • Re-config client binding configuration
  2. PROS (CLAIM TO BE):

    • Requires less efforts => As described in point 1., in my opinion, it's difficult to say this method requires less efforts to implement unless everything was is in place
  3. 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

    • Duplicate codes (you will need to copy the code over and over every time you want to insert Person & Unit)

Hope it helps,

Sorry for bad English

Upvotes: 1

Related Questions