1

I am using the repository pattern with EF. In my project we are using two databases and these two databases are sitting in two different projects. For any time one project is a CoreLib (we are referring in the other). I have the following questions.

  1. Can I use one repository layer for the two projects?
  2. How can I provide the transaction safety using System.Transactions.TransactionScope? Note: I am using Microsoft's unity framework and UnitOfWork pattern.

Thanks for your reply. I have implemented the functionality to save the context changes within Transaction scope. Every time, it is throwing the following exception.

{"Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool."}

I think this is related to MSDTC configuration and I configured network DTC access in client and SQL server machine. The configuration is as follows.

Network DTS access  - Enabled.
Allow Remote Clients    - Enabled.
Allow Remote Administration – Enabled.
Allow Inbound   - Enabled.
Allow outbond   - Enabled.
No Authentication Required – Enabled.

Is there anything I am missing in configuring MSDTC?

One more question: Is this configuration linked to Domain configuration? Because in our environment my DB server is not resolved with its name (we are using the IP address).

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
hareen
  • 31
  • 1
  • 5
  • I've merged your unregistered account into your registered account. You should now be able to edit your question, leave comments under individual answers and eventually accept the answer that best helped you. – Tim Post Jun 22 '11 at 07:52

2 Answers2

1

Can I use one repository layer for the two projects?

Yes.

How can I provide the transaction safety using System.Transactions.TransactionScope? Note: I am using the Microsoft’s unity framework and UnitOfWork pattern.

Use transaction demarcation in the business layer as the business requires it (do all stuff that must be in one transaction in a transactionscope).

Since you are using the UnitOfWork pattern.

  1. A unit of work has nothing to do with a transaction (in the db sense). The unit of work determines the lifetime of the ObjectContext. You can create the TransactionScope as you please. It does not matter if you create it after or before you create the unit of work.
  2. It is possible to do transaction demarcation with attributes on the business methods since the UnityFW is an ioc container.

Some stuff to now about transactions and EF:

  1. Transactions work with DbConnections and have not much to do with EF.
  2. You can mix old SQL stuff with EF stuff and even more than one database in one transaction if all involved drivers support the 2 phase commit.
  3. SaveChanges of ObjectContext will participate in a global transaction. If there is no global transaction, it will create a new transaction, save all stuff and commit or rollback.
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
yonexbat
  • 2,902
  • 2
  • 32
  • 43
0

you can do something like this. Assuming your Two database contexts are named FirstContext andSecondContext`

public class ContextFacade : IUnitOfWork // your Unit of work interface
{
  FirstContext _fc;
  SecondContext _sc
  public ContextFacade(FirstContext fc, SecondContext sc)
  {
     _fc = fc;
     _sc = sc;
  }

  public void SaveChanges()
  {
    var scope = new TransactionScope(TransactionScopeOption.Required, options);
    using(scope)
    {
      _fc.SaveChanges();
      _sc.SaveChanges()

      scope.Complete();
    }
  }
}

Take a look at Ladislav Mrnka's answer on how all of these can be put together.

Community
  • 1
  • 1
Eranga
  • 32,181
  • 5
  • 97
  • 96