a·gen·tic a·gil·i·ty

Sync extension for Lists/Collections or whatever

TL;DR; Explains how to synchronise two .NET collections by applying differences, with thread safety using locks, ensuring target lists match source lists efficiently.

Published on
2 minute read
Image
https://nkdagility.com/resources/hDlrQdNzLp1
Subscribe

I recently found the need to Sync two lists. I have one list that is used for display, and I want to dynamically sync that list with a new one by applying a delta.

I thought that this would be difficult, but I was surprised at its ease.

1   1: 
1   2: Module SyncExtensions
1   3: 
1   4:     <System.Runtime.CompilerServices.Extension()> _
1   5:     Public Sub Sync(Of TItem)(ByVal targetItems As ICollection(Of TItem), ByVal sourceItems As IEnumerable(Of TItem))
1   6:         Dim o As Object = DirectCast(targetItems, ICollection).SyncRoot
1   7:         If Monitor.TryEnter(o, TimeSpan.FromSeconds(10)) Then
1   8:             ' Find items in source that are not in target
1   9:             Dim itemsToAdd As New Collection(Of TItem)
1  10:             For Each Item In sourceItems
1  11:                 If Not targetItems.Contains(Item) Then
1  12:                     itemsToAdd.Add(Item)
1  13:                 End If
1  14:             Next
1  15:             ' Apply all adds
1  16:             For Each Item In itemsToAdd
1  17:                 targetItems.Add(Item)
1  18:             Next
1  19:             ' Find tags in target that should not be in source
1  20:             Dim itemsToRemove As New Collection(Of TItem)
1  21:             For Each Item In targetItems
1  22:                 If Not sourceItems.Contains(Item) Then
1  23:                     itemsToRemove.Add(Item)
1  24:                 End If
1  25:             Next
1  26:             ' Apply all removes
1  27:             For Each Item In itemsToRemove
1  28:                 targetItems.Remove(Item)
1  29:             Next
1  30:             ' Dispose Timer
1  31:             Monitor.Exit(o)
1  32:         End If
1  33:     End Sub
1  34: 
1  35: End Module

You need to remember to lock the object while you sync. This is to allow your threading to take place without incident. The nitty gritty is just a case of comparing the two lists and building a list of changes to make and then removing them :)

Technorati Tags: .NET

Smart Classifications

Each classification [Concepts, Categories, & Tags] was assigned using AI-powered semantic analysis and scored across relevance, depth, and alignment. Final decisions? Still human. Always traceable. Hover to see how it applies.

Subscribe

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Illumina Logo

Illumina

MacDonald Humfrey (Automation) Ltd. Logo

MacDonald Humfrey (Automation) Ltd.

CR2

Emerson Process Management Logo

Emerson Process Management

Freadom Logo

Freadom

DFDS Logo

DFDS

Healthgrades Logo

Healthgrades

Slicedbread Logo

Slicedbread

Flowmaster (a Mentor Graphics Company) Logo

Flowmaster (a Mentor Graphics Company)

ProgramUtvikling Logo

ProgramUtvikling

Higher Education Statistics Agency Logo

Higher Education Statistics Agency

Lockheed Martin Logo

Lockheed Martin

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

Ericson Logo

Ericson

Boeing Logo

Boeing

Deliotte Logo

Deliotte

Genus Breeding Ltd Logo

Genus Breeding Ltd

Slaughter and May Logo

Slaughter and May

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Ghana Police Service Logo

Ghana Police Service

Nottingham County Council Logo

Nottingham County Council

Royal Air Force Logo

Royal Air Force

Washington Department of Transport Logo

Washington Department of Transport

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

Flowmaster (a Mentor Graphics Company) Logo

Flowmaster (a Mentor Graphics Company)

NIT A/S

ALS Life Sciences Logo

ALS Life Sciences

Schlumberger Logo

Schlumberger

CR2