使用Automapper映射到CSLA对象
问题描述:
我正在尝试将DTO对象映射到CSLA.NET对象(请参阅:http://www.lhotka.net/cslanet/)。为了这个问题,我使用了Lhotka提供的示例应用程序。下面是我使用的类的实例(我删除了大部分属性和清晰度方法):使用Automapper映射到CSLA对象
<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对象。即我应该如何配置映射器?请注意,使用资源成员的解析器对这种特殊情况没有帮助。
谢谢!
禅
答
Automapper是不会帮助你在这里,因为它只能调用公共API。
使用常规CSLA.NET编码从DTO构建您的ProjectResources
列表。按照CSLA约定,在装载每个ProjectResource
时,您应该调用LoadProperty<T>(IPropertyInfo pi, T value)
来填充每个属性。
Thanks Ed!我非常怀疑会有这样的解决方案。 CSLA.NET有自己的做事方式。不幸的是,我没有足够的声望来接受你的回答。 – UncleZen 2011-04-15 12:56:56