I'm currently working with a project that use following technologies.
- ASP.net MVC - presentation Layer
- Data Service Layer - (WCF)
- Data Transfer Objects(DTO) Layer with Auto Mapper
- Domain Layer (POCO, Code First Entity Framework)
- Repository Layer + Entity Framework 4.3 + DbContext.
We use DTOs to convert Domain objects vice versa using auto mapper and sent to front end by using WCF Service.
Also, we are creating per request based DBContext in WCF layer for each request and our WCF service context is constructed by Per Call and No Tracking enable in client side DTOs and it is fully disconnected.
Also we have following object graph.
public class User : BaseEntity
{
public virtual Identity Identity { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int IdentityId { get; set; }
public virtual IList<Group> Groups{ get; set; }
}
public class Identity : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual IList<Email> Emails { get; set; }
public virtual IList<PhoneNumber> PhoneNumbers { get; set; }
}
Our Dto structure is more like same as compared to Domain.
My Questions:
When it comes to update object graph For Example: UpdateUser(User user); what is the best approach with Entity Framework ?
Now we use single functions to save Navigation data ex:UpdateEmail(userId, Email)(only saves primitive data not relationships); so it makes a lot of inserts and updates in the data base when we considering the one UnitOfWork.
Current implementation as follows
public void UpdateUser(User user)
{
UpdateEmail(user.userId, user.Idenity.Emails);
UpdatePhone(user.userId, user.Identity.PhoneNumbers);
etc.............
UpdateUser(user);
UnitOfWork.Commit();// Calling DbContext.SaveChanges();
}
Is there any pattern or best practice that we can use with Entity Framework in above situation with disconnected object graph ?