使用Automapper映射到CSLA对象



我正在尝试将DTO对象映射到CSLA。. NET(参见:http://www.lhotka.net/cslanet/)对象。为了回答这个问题,我使用了Lhotka随其框架提供的样例应用程序。下面是我正在使用的类的一个例子(为了清晰起见,我删除了大部分属性和方法):

 <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)()

我遇到的问题与资源只读属性的映射有关,该属性是继承自BusinessListBase的集合。向这个集合添加项目的唯一方法是执行Assign(resourceId)方法。

有人知道如何将DTO对象映射回CSLA对象吗?例如,我应该如何配置映射器?请注意,在这种特殊情况下,为Resources成员使用解析器没有帮助。

谢谢!

禅宗

Automapper在这里帮不了你,因为它只能调用公共api。

使用常规的CSLA。从dto构造ProjectResources列表。在加载每个ProjectResource时,您应该调用LoadProperty<T>(IPropertyInfo pi, T value)来填充每个属性,按照CSLA约定。

相关内容

  • 没有找到相关文章

最新更新