gpt4 book ai didi

使用 Automapper 映射到 CSLA 对象

转载 作者:行者123 更新时间:2023-12-02 13:37:15 28 4
gpt4 key购买 nike

我正在尝试将 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 对象吗? IE。我应该如何配置映射器?请注意,在这种特殊情况下,为 Resources 成员使用解析器没有帮助。

谢谢!

禅宗

最佳答案

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

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

关于使用 Automapper 映射到 CSLA 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5654300/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com