2012-04-07 63 views
1

automapperを使用して私は自分のビジネスオブジェクトに私のViewModelをマッピングしていますが、私は既存のオブジェクトインスタンスにマップし、ビューモデルのプロパティのみをマッピングします。例えばAutomapperは特定のプロパティのみをマッピングします

ProductModelは、ID、名前、コード ProductBusinessは、ID、名前、コード、dateadded

Function Add(ByVal model As ProducModel) As ActionResult 
    dim _ProductBusiness = (Load ProductBusiness from db) 

    dim newProductBusiness = AutoMapper.Mapper.Map(Of Business.User)(model) 
End Function 

私は何とか既存のビジネス・オブジェクトのインスタンスを渡すだけで両方のオブジェクトにある3つのプロパティをマップしたいを持っていましたdateaddedはデータベース内のものと同じままでなければなりません。

ありがとうございました

答えて

0

マッピングを作成すると、プロパティを無視できます。

あなたの洗練されたsoemthingを探しているかどうかわかりません。あなたはより一般的な何かをwan't場合

は、C#

Mapper.CreateMap<ProductModel, ProductBusiness>() 
.ForMember(target => target.dateadded, opt => opt.Ignore()); 

で、それは可能だが、詳細は歓迎されるであろう。

0

あなたはこれを行うことができます:

Public Sub MapIt(input As ProductModel, output As ProductBusiness) 
     AutoMapper.Mapper.CreateMap(Of ProductModel, ProductBusiness)() 
     AutoMapper.Mapper.Map(input, output) 
    End Sub 

をしかし、あなたは毎回CreateMapを呼び出す必要はありません、覚えておいてください。 AutoMapperは、dateaddedを無視します。 (テストしていないが、それはそうだと私は信じている。)。

または代わりに、あなたはこれを行うことができます。第二のコードは最初の1と同じある

Public Sub MapIt(input As c1, output As c2) 
     AutoMapper.Mapper.DynamicMap(input, output) 
    End Sub 

DynamicMapCreateMapとなります。

関連する問題