Question

I try my best to achieve a kind of UnitOfWork against storing data to datacontext. Without too much explanation i would like to make it possible to execute two queries within same using.

Take this,

using (IDataUnitOfWork uow = new Data.SomeContext.Core())
{
  CustomerRepo repo = new CustomerRepo(uow);
  Data.Entities.Customer c = repo.GetCustomerById(191);
  ...
  c.name = c.name.Replace("_", "");
  //c.name = c.name + "_";
  //uow.Save();
}

Which I feel are a very clear and strightforward way to complete a task. Now, though, i also have another context. In some circumstances, this task also have to be executed on a second context.

using (IDataUnitOfWork uow = new Provider.TheProviderContext())
{
  CustomerRepo repo = new CustomerRepo(uow);
  Data.Entities.Customer c = repo.GetCustomerById(191);
  ...
  c.name = c.name.Replace("_", "");
  //c.name = c.name + "_";
  //uow.Save();
}

That context are not based on same technique, but I think the above sample will reflect what i'm try to do (and need to). At this moment I can only see this way to solve it,,

using (IDataUnitOfWork uow = new Data.SomeContext.Core())
{
  ...
  if (anything == true) 
    using (IDataUnitOfWork uow = new Provider.TheProviderContext())
    {
      ...
    }
}

Which looks like a nice and clear way to work (bcz all information is already known to the second 'using'. Correct me if i'm completely wrong here, because i only have this on planning yet. But it would be EVEN BETTER if I could call

using (IDataUnitOfWork uow = new BothProviders())

..which in turn calls boths providers and ran the tasks simultaneous to each of them. I've readed about factories but it kind of confusing (yet). My skills are definitively ends here today, sadly to say..

Was it helpful?

Solution

depends what you want to achieve. Bear in mind that unit of work makes the whole operation atomic. If you want to unify providers consider adapter pattern

adapter pattern is suitable when you want to support (unify) multiple different interfaces at the same abstraction level, as in your case two UnitOfWork which I suppose will do different things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top