New Posts New Posts RSS Feed: BindingList that integrates Create and Delete with EntityManager
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

BindingList that integrates Create and Delete with EntityManager

 Post Reply Post Reply
Author
Randar View Drop Down
Newbie
Newbie
Avatar

Joined: 13-May-2011
Location: Toronto
Posts: 23
Post Options Post Options   Quote Randar Quote  Post ReplyReply Direct Link To This Post Topic: BindingList that integrates Create and Delete with EntityManager
    Posted: 30-May-2011 at 7:26am
No question for this post. 

Just wanted to post some code I wrote.  We have a number of UltraGrids (similar to DataGrids) and needed way to bind data to them.  The problem is that EntityManager returns IEnumerable(Of T), which does not support create or delete.

So wrote this wrapper around the BindingList which is directly incorporated into the EntityManager.

The nice thing about it is that once you ready to call SaveChanges, you can call .SaveDeletedEntityChanges and pass in the same EntityManager to get the deletes and saves as one atomic operation with minimal code.  Plus, it throws a bunch of events that can be used for custom cascading logic.

Sorry it's in VB.Net.  Victim of my current environment.

---

Imports System.ComponentModel
Imports IdeaBlade.EntityModel

''' <summary>
''' This class is a wrapper around BindingList, that is incorporated directly into the the EntityManager for new/delete operations.
''' </summary>
''' <typeparam name="T"></typeparam>
Public Class EntityBindingList(Of T As IdeaBlade.EntityModel.Entity)
    Inherits BindingList(Of T)

    Private m_DeletedEntites As List(Of T)

    ''' <summary>
    ''' Gets or sets the list of deleted entities.
    ''' </summary>
    ''' <value>
    ''' The deleted entities.
    ''' </value>
    Property DeletedEntities As List(Of T)
        Get
            If (m_DeletedEntites Is Nothing) Then
                m_DeletedEntites = New List(Of T)
            End If
            Return m_DeletedEntites
        End Get
        Set(ByVal value As List(Of T))
            m_DeletedEntites = value
        End Set
    End Property




    ''' <summary>
    ''' Occurs when an item is removed.
    ''' </summary>
    Public Event RemovingItem(ByVal sender As Object, ByVal e As RemoveItemEventArgs)
    ''' <summary>
    ''' Occurs when an object is created.  You can use this to populate the item with default values.
    ''' </summary>
    Public Event CreatingItem(ByVal sender As Object, ByVal e As CreateItemEventArgs)

    Public Sub New(ByVal list As IList(Of T))
        MyBase.New(list)
        AddHandler AddingNew, AddressOf AddingNewHandler
    End Sub

    ''' <summary>
    ''' Can be overriden to change the remove item logic
    '''
    ''' Raises the <see cref="E:RemovingItem" /> event.
    ''' </summary>
    ''' <param name="args">The <see cref="JonasNET.Shared.BL.RemoveItemEventArgs" /> instance containing the event data.</param>
    Protected Overridable Sub OnRemovingItem(ByVal args As RemoveItemEventArgs)
        RaiseEvent RemovingItem(Me, args)
    End Sub

    ''' <summary>
    ''' Removes the item at the specified index.
    ''' </summary>
    ''' <param name="index">The zero-based index of the item to remove.</param>
    ''' <exception cref="T:System.NotSupportedException">You are removing a newly added item and <see cref="P:System.ComponentModel.IBindingList.AllowRemove" /> is set to false. </exception>
    Protected Overrides Sub RemoveItem(ByVal index As Integer)
        OnRemovingItem(New RemoveItemEventArgs(Me(index)))

        ' Convert this to a entity so we can access the EntityAspect
        Dim entityToRemove As Entity = TryCast(Me(index), IdeaBlade.EntityModel.Entity)

        ' Remove it from the list first
        MyBase.RemoveItem(index)

        ' Keep track of the entities that need to be flagged and deleted in a another session.  This will not actually delete them
        ' Don't add an entity unless it belongs to an entity manager, since it implies there is nothing to delete
        If (Not entityToRemove.EntityAspect.EntityManager Is Nothing) Then
            DeletedEntities.Add(entityToRemove)
        End If
    End Sub

    ''' <summary>
    ''' Deletes the entities that are currently flagged.
    ''' </summary>
    ''' <param name="em">The em.</param>
    Public Sub SaveDeletedEntityChanges(ByVal em As EntityManager)
        If (Not m_DeletedEntites Is Nothing) Then
            m_DeletedEntites.ForEach(AddressOf DeleteEntity)
            em.ImportEntities(m_DeletedEntites, MergeStrategy.OverwriteChanges)
            m_DeletedEntites.Clear()
        End If
    End Sub

    Private Sub DeleteEntity(ByVal item As Entity)
        item.EntityAspect.Delete()
    End Sub


    ''' <summary>
    ''' Called when a new item is added.  This will create the object and call
    ''' an event that allows us to optionally set default values
    ''' </summary>
    ''' <param name="sender">The sender.</param>
    ''' <param name="e">The <see cref="System.ComponentModel.AddingNewEventArgs" /> instance containing the event data.</param>
    Private Sub AddingNewHandler(ByVal sender As Object, ByVal e As AddingNewEventArgs)
        ' New item added
        Dim newObject = Activator.CreateInstance(Of T)()
        ' Raise the event that allows the caller to set default properties
        RaiseEvent CreatingItem(Me, New CreateItemEventArgs(newObject))
        e.NewObject = newObject
    End Sub

End Class

#Region "EventArgs"

''' <summary>
''' Used when an item has been created and we want to populate it
''' </summary>
Public Class CreateItemEventArgs
    Inherits EventArgs
    Public ReadOnly Property CreatedItem() As [Object]
        Get
            Return m_createdItem
        End Get
    End Property
    Private m_createdItem As [Object]

    Public Sub New(ByVal removedItem As Object)
        Me.m_createdItem = removedItem
    End Sub
End Class

''' <summary>
''' Used when the event is raised, to pass the entity back
''' </summary>
Public Class RemoveItemEventArgs
    Inherits EventArgs
    Public ReadOnly Property RemovedItem() As [Object]
        Get
            Return m_removedItem
        End Get
    End Property
    Private m_removedItem As [Object]

    Public Sub New(ByVal removedItem As Object)
        Me.m_removedItem = removedItem
    End Sub
End Class

#End Region
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 31-May-2011 at 9:24am
Hi Randar,
 
Thanks for sharing.
 
Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down