我正在尝试将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 readonly属性的映射有关,该属性是继承自BusinessListBase的集合。向此集合添加项目的唯一方法是执行方法Assign(resourceId)。
是否有人知道如何将DTO对象映射回CSLA对象。即我该如何配置映射器?请注意,在这种特殊情况下,使用资源成员的解析器没有帮助。
谢谢!
禅
答案 0 :(得分:3)
Automapper不会在这里帮助你,因为它只能调用公共API。
使用常规CSLA.NET编码从DTO构建您的ProjectResources
列表。根据CSLA惯例,在加载每个ProjectResource
时,您应该调用LoadProperty<T>(IPropertyInfo pi, T value)
来填充每个属性。