使用 Automapper 映射到 CSLA 对象

发布于 2024-11-01 10:54:42 字数 3032 浏览 1 评论 0原文

我正在尝试将 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)()

我遇到的问题与 Resources 只读属性的映射有关,该属性是从 BusinessListBase 继承的集合。将项目添加到此集合的唯一方法是执行方法Assign(resourceId)。

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

谢谢!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2024-11-08 10:54:42

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文