小编Pau*_*ann的帖子

从MVC 3中的视图返回列表<T>

我目前有一个对象Tag定义如下:

public class Tag
{
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在,这是一个模型的集合属性,我将其定义为:

public class MyModel
{
    public string Name { get; set; }
    public IList<Tag> Tags { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在我看来,我有以下代码:

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name)
    </div>

    <div>
        <!--
        Here I'd like a collection of checkbox inputs, where the selected names
        get passed back to my controller via the IList<Tag> collection
        -->
    </div>

    <input type="submit" value="Submit" />
} …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc-3

4
推荐指数
1
解决办法
8175
查看次数

AutoMapper优雅地处理NULL

我正在将一个非常深层嵌套的实体映射到一个扁平化的Dto对象,并想知道我如何使用AutoMapper优雅地处理它.我知道我可以对每个属性进行null检查,因为它正在被映射,但这对于以下内容非常难看:

ForMember(s => s.Property, o => o.MapFrom(s => s.Parent.Child1.Child2.Child3.Property)
Run Code Online (Sandbox Code Playgroud)

所以我猜我可以使用各种地图配置到相同的目标对象......但是相对不熟练使用AutoMapper我不确定它的性能影响是什么.还有什么其他更好的方法可以实现我想要的目标?

要重新迭代,我希望避免这样的事情(我知道下面的代码可能不会编译......这只是一个例子),我必须为每个成员做些事情:

ForMember(s => s.Property, o => o.MapFrom(
    s => s.Parent == null ? string.Empty :
    s => s.Parent.Child1 == null ? string.Empty :
    s => s.Parent.Child1.Child2 == null ? string.Empty :
    s => s.Parent.Child1.Child2.Child3 == null ? string.Empty :
    s => s.Parent.Child1.Child2.Child3.Property));
Run Code Online (Sandbox Code Playgroud)

automapper

3
推荐指数
1
解决办法
1015
查看次数

自定义模型绑定后验证视图模型

我有一个实现的视图模型IValidatableObject包含一个字符串和另一个视图模型的集合,如下所示:

public sealed class MainViewModel
{
    public string Name { get; set; }
    public ICollection<OtherViewModel> Others { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的验证Others使用以下提供的合同检查每个对象与不同的规则IValidatableObject:

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    foreach (var other in this.Others)
    {
        // validate or yield return new ValidationResult
    }
}
Run Code Online (Sandbox Code Playgroud)

由于真实的复杂结构,MainViewModel我不得不创建一个自定义模型绑定器,它重新构建模型并将POST数据分配给相关组件.我得到的问题是没有任何东西得到验证,导致上下文级别的验证错误,因为它违反了某些数据库约束,我不确定我做错了什么 - 我以为我ModelState.IsValidValidate在我的视图上调用该方法模型,但它似乎没有那样下去.

我的模型绑定器看起来像这样:

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
    int modelId = (int)controllerContext.RouteData.Values["id"];

    // query the database and re-build the components of the view …
Run Code Online (Sandbox Code Playgroud)

validation custom-model-binder asp.net-mvc-3

2
推荐指数
1
解决办法
4270
查看次数

当单元测试中出现故障时,验证方法调用仍会通过

我刚刚开始在单元测试中使用Moq,但我遇到了单元测试通过的问题 - 我认为不应该这样.

我有两个对象,一个将数据发送到队列,另一个实现INotifier在调度程序失败时调用的对象,它们看起来像这样(为简洁而减少):

public class EmailNotifier : INotifier
{
    public void Notify(string message) 
    {
        // sends the notification by email
    }
}

public class Despatcher
{
    public void Despatch(int batchNumber, INotifier failureNotifier)
    {
        try
        {
            if (batchNumber.Equals(0)) throw new InvalidOperationException("Error message");
        }
        catch (InvalidOperationException ex)
        {
            failureNotifier.Notify(ex.ToString());

            throw ex;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在进行单元测试,Despatcher以便在它失败时专门验证Notify所提供的INotifier(我正在嘲笑)被调用(我故意Despatcher通过传递0批号来强制失败).我设置了这样的模拟:

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void Despatcher_notifies_on_failure()
{
    var mockNotifier = new Mock<EmailNotifier>();
    mockNotifier.Setup(n => n.Notify(It.IsAny<string>())).Verifiable();

    var despatcher …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq

2
推荐指数
1
解决办法
156
查看次数

模拟实体中的ICollection属性

我正在对我的实体进行一些单元测试,并且我有一些精神块嘲弄一个属性.采取以下实体:

public class Teacher
{
    public int MaxBobs { get; set; }
    public virtual ICollection<Student> Students { get; set; }
}

public class Student
{
    public string Name { get; set; }
    public virtual Teacher Teacher { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个Teacher被调用的方法,AddStudent它首先检查一个老师是否有太多的学生叫Bob分配.如果是这样,那么我提出一个自定义异常,说太多了.该方法如下所示:

public void AddStudent(Student student)
{
    if (student.Name.Equals("Bob"))
    {
        if (this.Students.Count(s => s.Name.Equals("Bob")) >= this.MaxBobs)
        {
            throw new TooManyBobsException("Too many Bobs!!");
        }
    }

    this.Students.Add(student);
}
Run Code Online (Sandbox Code Playgroud)

我想单元测试这种使用起订量嘲笑-特别是我想嘲弄.Count的方法Teacher.Students,我可以通过它的任何表达式,它会返回一个数字表明,目前有分配给老师10个鲍勃.我这样设置:

[TestMethod]
[ExpectedException(typeof(TooManyBobsException))]
public …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq mocking

2
推荐指数
1
解决办法
1297
查看次数