Using EF with connected POCOs in multi-tier applications

193 Views Asked by At

I am developing a multi tier desktop application(Onion architecture), with a WinForm Project as UI, and I used EF code first to access my DB, and for my Domain models, I want to use POCOs, so I have two choices :

  • Connected POCOs
  • Disconnected POCOs

If I use disconnected POCOs I have to do a lot of stuff outside of EF and do not use EF features, because:

  • I have to save and manage entity's State in client side
  • When I want save changes to DB, I have to sync client side POCO's State with DbContext entity's State.
  • During adding POCOs to newly created DbContext, I have to control to prevent adding two entities with same key to the DbContext.

So, it seems normal, to use Connected POCOs, but in this situation I think I have the following challenges:

  • If I want to manage lifetime of my DbContext using an IoC container, in a multiuser environment, and keep alive the DbContexts for all users from time their getting POCOs to time that saving back their changes to DB, it take large amount of server memory, I think, and it isn't efficient.
  • I want to save below related graph of my entities in one transaction enter image description here

I have these questions:

  1. How can I use connected POCOs in a my multitier application?
  2. which config should I use to manage lifetime of my DbContexts using IoC containers in my case?
  3. Is there any way to make(or change) the total graph(in client side) and then pass it to save in DbContext?
  4. Is there any sample that implement this situations?
0

There are 0 best solutions below