如何使用StructureMap在视图模型上自动设置属性

时间:2013-11-21 08:33:17

标签: c# asp.net-mvc structuremap

我有一个接口ITranslateStuff和一个带有受限制的泛型参数的静态类和方法(其中class,ITranslateStuff,new())。

string translation = Translator.TranslateStuff<ITranslateStuff>();

根据我传递的ITranslateStuff的实现,该方法返回一个不同的字符串。

我的ViewModels有很多不同的属性,它们返回ITranslateStuff的实现,例如:

public class ExampleViewModel
{
    public string OtherStuff {get; set; }
    public string TranslateStuffExample1 Translations { get; set; }
    public ExampleViewModel2 SubModel {get; set; }
}

public class ExampleViewModel2
{
    public string MoreStuff { get; set; }
    public string TranslateStuffExample2 Translations { get; set; }
}

DoStuffExample1和DoStuffExample2都实现了ITranslateStuff。

我目前使用以下代码填充所有theese属性:

model.Translations = Translator.TranslateStuff<TranslateStuffExample1>();

model.SubModel.Translations = Translator.TranslateStuff<TranslateStuffExample2>();

在项目中我们使用StructureMap。我想避免使用相同的静态方法调用手动设置我的视图模型上的所有属性。我有一个ActionFilter,我在我的视图模型上设置公共属性,并且我想我也想在动作过滤器中执行此操作。

我已经尝试在StructureMap中找到可以为我做这件事的事情。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)