Strongly typed collection classes and OpenAccess forward mapping

2 posts, 0 answers
  1. Jose Rodriguez
    Jose Rodriguez avatar
    1 posts
    Member since:
    Dec 2009

    Posted 21 Dec 2009 Link to this post

    We have a object model which includes many Strongly typed collection classes. We are exploring using OpenAccess for persistence and used the mapping forward wizard to get started but it does not seem to be able to get it going...

    It creates the tables in the database (SQL Server 2005 Express) but when I add items to the strong typed collection, items are not added to the database...

    The following are 3 classes I'm using for the testing.... and the code to add items to the strong typed collection PSUsers


    Project Class

     

     

    Option Strict Off  
    Option Explicit On 
    Imports System  
    <Telerik.OpenAccess.Persistent()> Public Class Project  
     
        <Telerik.OpenAccess.SerializeToBlob()> Private m_strName As String = "" 
        Private m_strDescription As String = "" 
     
        <Telerik.OpenAccess.Transient()> Private m_PSUsers As New PSUsers  
     
        Public Property Description() As String 
            Get 
                Return m_strDescription  
            End Get 
            Set(ByVal Value As String)  
                m_strDescription = Value  
            End Set 
        End Property 
     
        Public Property Name() As String 
            Get 
                Return m_strName  
            End Get 
            Set(ByVal Value As String)  
                m_strName = Value  
            End Set 
        End Property 
        Public ReadOnly Property PSUsers() As PSUsers  
            Get 
                Return m_PSUsers  
            End Get 
        End Property 
        Protected Overrides Sub Finalize()  
            m_PSUsers = Nothing 
        End Sub 
    End Class 

     

     

    PSUsers Collection Class
    Option Strict Off  
    Option Explicit On 
    Imports Microsoft.VisualBasic  
    Imports System  
    Imports System.Collections  
    Public Class PSUsers  
        Inherits CollectionBase  
        Default Public Property Item(ByVal Index As IntegerAs PSUser  
            Get 
                Return CType(List.Item(Index), PSUser)  
            End Get 
            Set(ByVal Value As PSUser)  
                List.Item(Index) = Value  
            End Set 
        End Property 
     
        Public Function Add(ByVal Item As PSUser) As Integer 
            Return List.Add(Item)  
        End Function 
     
    End Class 

    PSUser Item
    Option Strict Off  
    Option Explicit On 
    Imports System  
    <Telerik.OpenAccess.Persistent()> Public Class PSUser  
     
        Private m_strUsername As String = "" 
        Private m_strName As String = "" 
        Private m_strDescription As String = "" 
     
     
        Public Property Username() As String 
            Get 
                Return m_strUsername  
            End Get 
            Set(ByVal Value As String)  
                m_strUsername = Value  
            End Set 
        End Property 
        Public Property Name() As String 
            Get 
                Return m_strName  
            End Get 
            Set(ByVal Value As String)  
                m_strName = Value  
            End Set 
        End Property 
        Public Property Description() As String 
            Get 
                Return m_strDescription  
            End Get 
            Set(ByVal Value As String)  
                m_strDescription = Value  
            End Set 
        End Property 
     
    End Class 


    This is the code for a button to add item to the PSUSers collection

     

           Private Sub cmdAdd_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles cmdAdd.Click  
                Dim myUser As PSUser = New PSUser ' PSUser = New PSUser 'PS2Project.PSUsers.Add()  
                Dim lCount As Integer 
     
                PS2Project.PSUsers.Add(New PSUser)  
                lCount = PS2Project.PSUsers.Count  
     
     
                PS2Project.PSUsers.Item(lCount - 1).Username = "User" & lCount  
                PS2Project.PSUsers.Item(lCount - 1).Description = "My Description " & lCount  
                PS2Project.PSUsers.Item(lCount - 1).Name = "My Name " & lCount  
                UpdateGUI()  
     
            End Sub 
     

     

     


    Any pointers are welcome.


  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 23 Dec 2009 Link to this post

    Hi Jose Rodriguez,

    First of all you should not use the Transient attribute. When it is applied to a field, the field is not 'persistent' any more and its information will not be stored in the database. You should better use a strongly typed collection like List<T> or at least a collection that derives from it. When a non-generic collection type is used OpenAccess cannot know what type of values will be persisted, hence it cannot create the corresponding tables.

    Here is a sample solution:
    1.    You could change the m_PSUsers type from PSUsers to List<PSUser> and remove the Transient attribute:
    Private m_PSUsers As IList(Of PSUser) = New List(Of PSUser)

    2.    Change the PSUsers item initialization to:
    PS2Project.PSUsers (lCount - 1).Username = "User" & lCount 
    PS2Project.PSUsers (lCount - 1).Description = "My Description " & lCount 
    PS2Project.PSUsers (lCount - 1).Name = "My Name " & lCount

    Hope that helps.

    All the best,
    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
Back to Top