4

I am trying to map a DTO object to a CSLA.NET (see: http://www.lhotka.net/cslanet/) object. For the sake of this question I am using the sample application that Lhotka provides with his framework. Below is an example of classes I am using (I removed the majority of properties and methods for clarity):

 <Serializable()> _
    Public Class Project
        Inherits BusinessBase(Of Project)


        Private mId As Guid = Guid.NewGuid
        Private mName As String = ""

        Private mResources As ProjectResources = _
          ProjectResources.NewProjectResources()

        <System.ComponentModel.DataObjectField(True, True)> _
        Public ReadOnly Property Id() As Guid
            <System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
            Get
                'CanReadProperty(True)
                Return mId
            End Get
        End Property


        Public Property Name() As String
            <System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
            Get
                'CanReadProperty(True)
                Return mName
            End Get
            <System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
            Set(ByVal Value As String)
                'CanWriteProperty(True)
                If Value Is Nothing Then Value = ""
                If mName <> Value Then
                    mName = Value
                    PropertyHasChanged()
                End If
            End Set
        End Property



        Public ReadOnly Property Resources() As ProjectResources
            Get
                Return mResources
            End Get
        End Property


    End Class




    Public Class ProjectDTO


        Private _id As Guid
        Public Property Id() As Guid
            Get
                Return _id
            End Get
            Set(ByVal value As Guid)
                _id = value
            End Set
        End Property



        Private _name As String
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property


        Private _resources As New List(Of ProjectResourceDTO)()
        Public Property MyResources() As List(Of ProjectResourceDTO)
            Get
                Return _resources
            End Get
            Set(ByVal value As List(Of ProjectResourceDTO))
                _resources = value
            End Set
        End Property

    End Class


Mapper.CreateMap(Of ProjectDTO, Project)().ConstructUsing(Function(src As ProjectDTO) Project.NewProject())
Mapper.CreateMap(Of ProjectResourceDTO, ProjectResource)()
Mapper.CreateMap(Of ResourceDTO, Resource)()

The issue that I am experiencing is related to the mapping of the Resources readonly property which is a collection inheriting from BusinessListBase. The only way to add items to this collection is by executing the method Assign(resourceId).

Does anybody have an idea as to how can I map the DTO object back to the CSLA object. I.e. How should I configure the mapper? Please note that using a resolver for the Resources member did not help in this particular case.

Thanks!

Zen

4

1 回答 1

3

Automapper 在这里不会为您提供帮助,因为它只能调用公共 API。

ProjectResources使用常规 CSLA.NET 编码从 DTO构建您的列表。根据 CSLA 约定,在加载每个属性时,ProjectResource您应该调用LoadProperty<T>(IPropertyInfo pi, T value)以填充每个属性。

于 2011-04-15T07:46:11.413 回答