Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3483718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:42:23+00:00 2026-05-18T10:42:23+00:00

I have seen some questions related to topic, but could not find answer for

  • 0

I have seen some questions related to topic, but could not find answer for this scenario.

I have structure like
alt text

Part of my controller

//
// GET: /Person/Edit/5

public ActionResult Edit(int id)
{
    var viewModel = new PersonViewModel(PersonRepository.Get(id));
    return View(model);
}

//
// POST: /Person/Edit

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(PersonViewModel model)
{
    PersonRepository.Update(model.Person, model.Phones);
    return RedirectToAction("Index");
}

In my repository im doing something like this:

public void Update(Person person, ICollection<Phone> phones)
{
    using (var unitOfWork = UnitOfWork.Current)
    {
         Attach(contact);
         UpdatePhones(contact, phones);
         unitOfWork.Commit();
    }
}
public Person Attach(Person person)
{
     Context.AttachTo("Persons", entity);
     Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
     return entity;
}
public void UpdatePhones(Person person, ICollection<Phone> phones)
{
    if (phones == null || phones.Count == 0) return;
    foreach (var phone in phones.Where(o => o.IsDeleted && !o.IsNew))
    {
         PhoneRepository.Delete(phone);
    }
    foreach (var phone in phones.Where(o => !o.IsDeleted && o.IsNew))
    {
         party.Phones.Add(phone);
    }
    foreach (var phone in phones.Where(o => !o.IsDeleted && !o.IsNew))
    {
         phone.PartyID = party.ID;
         PhoneRepository.Attach(phone);
    }
}

IsDeleted and IsNew are not persisted into db and used in dynamic form. PhonesRepository Attach() is same.

Im doing all of my updates like this because i need to reduce number of db calls as much as possible. Maybe there is a known best practice for this?

Thanks=)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:42:24+00:00Added an answer on May 18, 2026 at 10:42 am

    That’s not bad at all. Very similar to our setup.

    Couple of pointers:

    1 – Use generics for your Repositories. Your code for Attach is very simple, and should not need to be duplicated amongst entities. If you created a GenericRepository<T>, then your attach code would be:

    public T Attach<T>(T entity) where T : class
    {
         Context.GetEntitySet<T>.Attach(entity); // pluralization on (typeof)T.name to get entity set
         Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
         return entity;
    }
    

    2 – I would seperate the UpdatePhones method into seperate methods. I wouldn’t rely on a flag (IsDeleted, etc) to determine the course of action. I would be more explicit.

    3 – Looks like you have a PhoneRepository? Why? Person is your aggregate root, and Phone is always related to a particular Person, therefore you should only have a PersonRepository in this aggregate boundary. You should be attaching to the Phones ObjectSet` of a particular person.

    BTW – i assume you have lazy loading disabled? If not, those LINQ operations over the ICollection will cause silent round trips.

    Apart from the above points (which are mainly design related), optimization-wise your code looks good to me.

    And a final note – there is another recommended technique (Alex James) for updating entities in a detached context (aka MVC) called the “stub techinque”.

    I asked a question (and solved it myself) a few days ago if your interested, check it out.

    HTH.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen some of the questions/answers related to this topic here, however still
I have seen some other questions about this but the errors were related to
I have seen a few questions similar to this around but could not figure
I have seen some related questions but none focusing on the specific problem I
I have seen some of the other answers on this topic but dont really
Ok, I have seen several questions related to this issue, and I have tried
I've seen a few related questions posted like two years ago, but I would
I've seen a couple of questions about this particular error message, but couldn't find
I have looked at the other questions related to this problem but I do
Have seen some similar questions: What is the difference between a JavaBean and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.