私はAutomapperで実際に私の髪を引き出しています。私はそれが直感的ではなく、使いにくいことがわかります。マッピングコードを手作業で書くのはずっと簡単です。ビジネスオブジェクトをデータオブジェクトにマップできません
namespace BusinessObjects
{
public class Application
{
public int ID { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public System.DateTime DateOfBirth { get; set; }
public System.DateTime CreatedTime { get; set; }
public System.DateTime ModifiedTime { get; set; }
public System.DateTime ApplicationDate { get; set; }
public string Qualified { get; set; }
public int salary { get; set; }
public string userID { get; set; }
}
}
namespace DataObjects
{
using System;
using System.Collections.Generic;
public partial class Application
{
public int ID { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public System.DateTime DateOfBirth { get; set; }
public System.DateTime CreatedTime { get; set; }
public System.DateTime ModifiedTime { get; set; }
public System.DateTime ApplicationDate { get; set; }
public string Qualified { get; set; }
public int salary { get; set; }
public string userID { get; set; }
}
}
とコントローラ:
public ApplicationController()
{
Mapper.Initialize(cfg => cfg.CreateMap<BusinessObjects.Application, ApplicationModel>());
config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<BusinessObjects.Application, ApplicationModel>();
});
config.AssertConfigurationIsValid();
Mapper.Initialize(cfg => cfg.CreateMap<ApplicationModel,BusinessObjects.Application>());
config2 = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ApplicationModel,BusinessObjects.Application>();
});
config2.AssertConfigurationIsValid();
}
public int Create(BusinessObjects.Application businessApplication)
{
var mapper =config2.CreateMapper();
DataObjects.Application dataApplication = AutoMapper.Mapper.Map<DataObjects.Application>(businessApplication);
int count = 0;
using (CreditCardPreQualificationEntities CreditCardPreQualificationDatabase = new CreditCardPreQualificationEntities())
{
CreditCardPreQualificationDatabase.Applications.Add(dataApplication);
count = CreditCardPreQualificationDatabase.SaveChanges();
}
return count;
}
私はこの作業を取得しようとしているほぼすべての週末を過ごしませんし、何より近い午前私は2つの単純な種類があります。私はAutomapperの利点を全く見ることができません。 "AutoMapper.AutoMapperMappingException型の例外はAutoMapper.dllで発生しましたが、ユーザーコードでは処理されませんでした。
追加情報:タイプマップの設定またはサポートされていないマッピングが見つかりませんでした。
...初期化コードも非常に混乱します。アプリの開始時にAutoMapperを一度設定するだけで、各タイプマップを一度作成するだけで済みます。 – stuartd
@stuartd、初期化コードはどこに入れますか?私はそれをビジネスオブジェクトからデータオブジェクトに1回、データオブジェクトからビジネスオブジェクトに1回ずつ2回入力しました。それは間違っていますか? – w0051977
MVCアプリケーションのApplication_Start()にあります。 AutoMapperは一度のみ初期化する必要があります。**は古い構文と新しい構文のいずれかを使用します。古い方法は、 'Mapper.Initialize(cfg => { cfg.CreateMap(); cfg.CreateMap (); }});と' Mapper.Map (businessApplication); ' –
stuartd