WayneBrantley wrote:
So you wont change it, ok. Help me understand the solution then.
Lets say I have this code in 10 projects...
public void SomeSharedFunction(UnitOfWork uow, ...other params)
{
// do some stuff,
// Add new entity to uow, etc..
}
I want to create a library that has this shared code, package it up and put on private nuget server
This is your mistake: you want this and if it breaks it's someone else's problem to solve. But that's not going to work of course (analogy: class C isn't thread safe. My design requires C to be threadsafe, and my code only works if I'm careful. So I ask whether C can be made thread safe. If that's not done (as that's pehaps a burden/impossible) it's a problem for someone else? No, it's a problem for me, as I use C wrong).
Sharing things means you have a strict boundary and if things cross that boundary it has to work always. Your design doesn't work here, so you have to do it differently. If you want to share a common persistence system, make sure the code using it doesn't need to know about unit of works. We already discussed that in your unit of work question where you wanted a 'unit of work with other unit of works inside it'.
So don't share this api, share an api, e.g. a service (doesn't need to be a true service, can be a system injected with DI) that accepts an entity to persist and makes sure it is saved in the right DB, does transaction management etc. Your shared API is too low level for this as it requires all entities in the UoW to be in the same DAL which can't be guaranteed.
I would not think I am the only person that would like to do this. We end up with all kinds of different project from windows services to websites to http rest services that need to share code...as the code is complex.
This is working today with self servicing...the one catch is you cannot have the FIRST item added to a UOW come from the DAL in the shared library....other than that works perfectly.
Let's say I am using adapter. You are saying I cannot do this in adapter either...something about running on a different server or something....no idea what you are talking about. I simply want to share code that happens to use your product across multiple projects.
Dal A is in server ServerA. Dal B is on ServerB. If I put an entity from A and an entity from B in 1 UoW, and persist it with the adapter of A, it doesn't work either (besides that that adapter doesn't even know the persistence info for B): opening a connection to ServerA, saving the A entity, then saving the B entity doesn't work: B isn't even on that server.
So this requires something above that, something that can start 2 unit of works here one on each dal, and use a system.transactions transaction to span both persistence actions in 1 transaction.
This is the same with your situation. You want to effectively do the same as passing the persistence info of the adapter of B to the adapter of A to make the unit of work with the two entities from the example work. But that's not going to fly. That your current code 'works' if you're careful is luck. You should refactor some of its design.
So instead of sharing a low-level DAL with a low-level API, share a service type API that provides a service API for your system: it accepts work and can route it to the right place to get it done. Doesn't need to be super fancy, but at least it offers a clear boundary you won't cross.