Confused over new structure of Child Objects

Confused over new structure of Child Objects

Old forum URL: forums.lhotka.net/forums/t/11219.aspx


gajit posted on Thursday, March 08, 2012

Hi gents,

I'm trying to get to grips with CSLA4, specifically it's implementation of ChildObjects and am somewhat confused.

I am using the encapsulated invoke model using (Safe)Datareader

I have a simple Business Object. Userprofiles which has UserRoles as children.

I currently have - for the parent, UserProfileEdit (and it's matching Interface and Dal, UserProfileDal and IUserProfileDal)

And for the children, UserRoleDal and IUserRoleDal.

I have no requirement to use the userrole as a parent, so I don;t believe I need to create a "UserRoleEdit" class.

I also have the UserRole(BusinessBase)  and UserRoles (BusinessListBase of UserRole) - These two are what I would expect to have used in CSLA2.

Now, business object can be retrieved and children fetched successfully.

My problem is the implementation of the methods to Add and Delete Children.

In CSLA2, I would have had this code in my UserRoles class:

Public Function GetItem(ByVal role As String) As UserRole

        For Each u As UserRole In Me
            If u.ROLE = role Then
                Return u
            End If
        Next
        Return Nothing

    End Function

    Public Sub Assign(ByVal ROLE As String)

        If Not Contains(ROLE) Then
            Dim rol As UserRole = _
              UserRole.NewUserRole(ROLE)
            Me.Add(rol)

        Else
            Throw _
              New InvalidOperationException("Role already assigned to user")
        End If

    End Sub
    Public Overloads Sub Remove(ByVal ROLE As String)

        For Each u As UserRole In Me
            If u.ROLE = ROLE Then
                Remove(u)
                Exit For
            End If
        Next

    End Sub

    Public Overloads Function Contains( _
        ByVal role As String) As Boolean

        For Each u As UserRole In Me
            If u.ROLE = role Then
                Return True
            End If
        Next

        Return False

    End Function

    Public Overloads Function ContainsDeleted( _
      ByVal role As String) As Boolean

        For Each u As UserRole In DeletedList
            If u.ROLE = role Then
                Return True
            End If
        Next

        Return False

    End Function

and in my USerRole I would have had the factory methods:

Friend Shared Function NewUserRole( _
     ByVal rol As String) As UserRole

        Return New UserRole(rol)

    End Function

    Friend Shared Function GetUserRole( _
      ByVal dr As SafeDataReader) As UserRole

        Return New UserRole(dr)

    End Function

    Private Sub New()

        MarkAsChild()

    End Sub

    Private Sub New(ByVal dr As SafeDataReader)

        MarkAsChild()
        Fetch(dr)

    End Sub

    Private Sub New(ByVal role As String)

        MarkAsChild()

        mROLE = role

    End Sub

I could then, from my User interface, add a role to the user by executing the method:

mUserProfile.ROLES.Assign(cboRoles.Text)

 

 

However, in CSLA 4, in the project tracker solution, in is implemented differently;

_project.Resources.Assign(dlg.ResourceId);

 

 

The ProjectResource class implements the use of a "Creator" class??, :

public ProjectResourceEdit Assign(int resourceId)
    {
      var resource = ProjectResourceEditCreator.GetProjectResourceEditCreator(resourceId).Result;
      this.Add(resource);
      return resource;
    }
#endif

    public void Remove(int resourceId)
    {
      var item = (from r in this
                    where r.ResourceId == resourceId
                    select r).FirstOrDefault();
      if (item != null)
        Remove(item);
    }

    public bool Contains(int resourceId)
    {
      var item = (from r in this
                  where r.ResourceId == resourceId
                  select r).Count();
      return item > 0;
    }

    public bool ContainsDeleted(int resourceId)
    {
      var item = (from r in DeletedList
                  where r.ResourceId == resourceId
                  select r).Count();
      return item > 0;
    }

 Do I need the creator class?? It intimates that I have to then create a ProjectResourceEdit class (UserRoleEdit in my example) - which is completely superfluous to my needs.

I have tried to implement the Assign, Delete, etc methods a-la CSLA2; in my UserRoles class as;

  Public Sub Assign(_role As String)

        If Not Contains(_role) Then
            Dim role As UserRole = _
              UserRole.NewUserRole(_role)
            Me.Add(role)
        Else
            Throw _
              New InvalidOperationException("Role already assigned to this user")
        End If

    End Sub

#End If

    Public Overloads Sub Remove(_role As String)

        For Each item As UserRole In Me
            If item.Role = _role Then
                Remove(item)
                Exit For
            End If
        Next

    End Sub

    Public Overloads Function Contains( _
                        _role As String _
                    ) As Boolean

        For Each item As UserRole In Me
            If item.Role = _role Then
                Return True
            End If
        Next

        Return False

    End Function

    Public Overloads Function ContainsDeleted( _
                        _role As String _
                    ) As Boolean

        For Each item As UserRole In DeletedList
            If item.Role = _role Then
                Return True
            End If
        Next

        Return False

    End Function

It _appears_ to work, but is this code (still) legitimate in the CSLA4 world?

Bear in mind I'm eventually looking at the other (Silverlight, WP) interfaces. Will this methodology support these technologies?

Any advice or assistance would be appreciated.

 

Thanks,

Graham

 

 

 

 

 

Copyright (c) Marimer LLC