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 294427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:21:57+00:00 2026-05-12T06:21:57+00:00

I have a service layer returning a collection of entities : /// <summary> ///

  • 0

I have a service layer returning a collection of entities :


/// <summary>
/// Returns list of all existing accounts.
/// </summary>
IList<Account> GetAllAccounts();

This is called on UI layer to get IList collection and bind it to UI via IBindingList :


public BindingList<Account> Accounts
{
   get; 
   private set;
}

public void LoadAccounts()
{
   var service = IoC.Resolve<IAccountService>();
   Accounts = new BindingList<Account>(service.GetAllAccounts());
}

Then UI changes the the IBindingList collection (adding, deleting and editing entities), so at some point (before saving) we have a collection that has edited, deleted and new entities. How can I save this collection in a way to avoid unnecessary updates (do not update entities that are not changed). When I do the following, an Insert / Update / Delete statement is ran for ALL items in the collection:

on UI :


public void Save()
{
   var accountService = IoC.Resolve();
   accountService.Update(Accounts);
}

on Service Layer:


public void Update(IList<Account> accounts)
{
    if (accounts == null || accounts.Count == 0)
        return;

    using (UnitOfWork.Start())
    using (var session = UnitOfWork.CurrentSession)
    using (var tx = session.BeginTransaction())
    {
        foreach (var acc in accounts)
        {
            if (acc.AccountId == 0)
            {
                session.Save(acc);
            }
            else
            {
                session.Update(acc);
            }
        }

        tx.Commit();
    }            
}

Note that entities are some POCO classes. Is there any way to know if a collection has changes, or an easy way to find if the entity is dirty (HasChanges or IsDirty kinda thing).

  • 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-12T06:21:57+00:00Added an answer on May 12, 2026 at 6:21 am

    If the objects were loaded in the same session, then the session is keeping track of changed objects and update only the changed ones. But in your case the objects are not associated with this session.

    You could track the changed objects yourself and save or update the changed ones. NHibernate cannot help you there.

    Or you could try and implement Session per Conversation pattern and keep the objects in one session over multiple requests.

    As a side note you could use the ISession.SaveOrUpdate(Object) method instead of the if statement. The session will do it for you ;).

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

Sidebar

Related Questions

I have my service layer, entities and DTOS in a separeted assembly called CCL.Data
I have the standard repository pattern with a service layer and I need to
I have this sort of format asp.net MVC View -> Service Layer -> Repository.
I have a service, business and data access layer. In which layer should I
I have a quite simple WCF service method which returns an IQueryable, just for
Is there a simple way to have your Service Layer deployed separately from your
I have developed webservices using CXF framework. We are exposing all our Database operation(Service
I am designing a WCF service using DDD. I have a domain service layer
I have a service layer that I would like to convert into a webservice.
If I have a service (service-layer object not web service) called WidgetProcessor and that

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.