开发者

Mapping to CSLA objects using Automapper

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 va开发者_开发知识库lue 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


Automapper is not going to help you here because it can only call public APIs.

Use regular CSLA.NET coding to construct your ProjectResources list from the DTOs. Whilst loading each ProjectResource you should be calling LoadProperty<T>(IPropertyInfo pi, T value) to populate each property, as per CSLA convention.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜