1

Global.aspx に次のフックがあります。

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
        AutoMapper.Mapper.CreateMap<FormCollection, Models.IAmACustomer>().ForAllMembers(form => form.ResolveUsing<Models.FormCollectionValueResolver<Models.IAmACustomer>>());
    }

私のコントローラーで:

[HttpPost]
    public ActionResult Create(FormCollection formCollection)
    {

        var customer = AutoMapper.Mapper.Map<FormCollection,Models.IAmACustomer> (formCollection,null);
    }

この行は実行されますが、カスタム リゾルバーは呼び出されません。

リゾルバーは次のようになります。

public class FormCollectionValueResolver<TDestination>:ValueResolver<FormCollection,TDestination>
{
//Code removed for brevity
}

アプリケーションはコンパイルおよび実行されますが、カスタム リゾルバーがなければ、オブジェクトには何も入りません。get アクセサーを例外としてスローするモック オブジェクトを作成するだけです。

4

2 に答える 2

0

FormCollectionValueResolver<Customer>neverが呼び出されない理由は、メソッドによって定義されているように、指定されたメンバーオプションを適用して、メソッドForAllMembers()がすべてのプロパティマッピングを反復処理するためです。ForMember()ただし、指定したコードサンプルでは、​​プロパティマッピングが定義されていないため、リゾルバーが呼び出されることはありません。

このメソッドの使用例を次に示しForAllMembers()ます。

[Test]
public void AutoMapperForAllMembersTest()
{
    Mapper.CreateMap<Source, Destination>()
        .ForMember(dest => dest.Sum, 
            opt => opt.ResolveUsing<AdditionResolver>())
        .ForMember(dest => dest.Difference,
            opt => opt.ResolveUsing<SubtractionResolver>())
        .ForAllMembers(opt => opt.AddFormatter<CustomerFormatter>());

    Source source = new Source();
    source.Expression = new Expression
    {
        LeftHandSide = 2,
        RightHandSide = 1
    };

    Destination destination = Mapper.Map<Source, Destination>(source);
    Assert.That(destination.Sum, Is.EqualTo("*3*"));
    Assert.That(destination.Difference, Is.EqualTo("*1*"));
}    

public class Expression
{
    public int LeftHandSide { get; set; }

    public int RightHandSide { get; set; }
}

public class Source
{
    public Expression Expression { get; set; }
}

public class Destination
{
    public string Sum { get; set; }

    public string Difference { get; set; }
}

public class AdditionResolver : ValueResolver<Source, int>
{
    protected override int ResolveCore(Source source)
    {
        Expression expression = source.Expression;
        return expression.LeftHandSide + expression.RightHandSide;
    }
}

public class SubtractionResolver : ValueResolver<Source, int>
{
    protected override int ResolveCore(Source source)
    {
        Expression expression = source.Expression;
        return expression.LeftHandSide - expression.RightHandSide;
    }
}

public class CustomerFormatter : IValueFormatter
{
    public string FormatValue(ResolutionContext context)
    {
        return string.Format("*{0}*", context.SourceValue);
    }
}
于 2009-11-26T22:27:57.190 に答える
0

FormCollection を完全に捨てることを検討する必要があります。

http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

基本的に、厳密に型指定されたビュー + カスタム作成されたフォームの ViewModel 型に依存します。これらのフォームには検証属性などがあるため、検証フレームワークを介して実行できます。有効な場合にのみ、投稿されたフォームから永続化モデルを更新します。投稿されたフォームから直接ドメイン オブジェクトを作成することは避けています。

于 2009-11-28T05:12:38.033 に答える