在AutoFixture中创建复杂子级时使用父级属性的值 [英] Use value of a parent property when creating a complex child in AutoFixture

查看:81
本文介绍了在AutoFixture中创建复杂子级时使用父级属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AutoFixture为包含父对象和复杂子对象的结构生成数据,例如:

I'm using AutoFixture to generate data for a structure involving a parent object and complex child objects, like this:

public class Parent
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Child[] Children { get; set; }
}

public class Child
{
    public string Name { get; set; }
    public int ParentId { get; set; }
}

是否可以自动设置属性生成的 Child 对象的ParentId 分配给父代的ID?现在我的解决方案看起来不是很漂亮:

Is there a way to automatically set the property ParentId of the generated Child object to the id assigned to the parent? Right now my solution looks like this, which isn't very pretty:

var parent = fixture.Build<Parent>().Without(p => p.Children).CreateAnonymous();
parent.Children = fixture.CreateMany<Child>(10).ToArray();

foreach (var i in parent.Children)
{
    i.ParentId = parent.Id;
}

感觉有一种更好的方法让我丢失了?我考虑创建一个自定义的 ISpecimenBuilder ,但也没有设法解决该问题。

It feels like there's a better way to do this that I am missing? I looked into creating a custom ISpecimenBuilder but didn't manage to solve it that way either.

推荐答案

AutoFixture基于一组有关可能要求使用的API的规则和假设。考虑到它是在没有任何 Child Parent 类或任何其他类型的先验知识的情况下创建和编译的给定的API。它只需要使用公共API。

AutoFixture is based on a set of rules and assumptions about the API it may be asked to work with. Consider that it's been created and compiled without any prior knowledge of the Child and Parent classes, or any other types in a given API. All it has to work with is the public API.

AutoFixture是一个非常昏暗的程序员,甚至不懂您的语言(甚至不懂英语)。 防呆可以使您的API越容易

Think of AutoFixture as a very dim programmer who doesn't even understand your language (not even English). The more fool-proof you can make your API, the easier it will be to use AutoFixture with it.

像此处描述的父/子关系这样的循环引用的问题是它破坏了封装。首先,您需要创建至少一个处于无效状态的类实例。很难使AutoFixture与这样的API一起使用主要应作为警告信号,表明该API可能会受益于重构。

The problem with circular references like the Parent/Child relationship described here is that it breaks encapsulation. You'll need to create at least one of the class instances initially in an invalid state. That it's difficult to make AutoFixture work with such an API should mainly be taken as a warning sign that the API might benefit from refactoring.

此外,.NET Framework设计指南建议不要将数组作为属性公开-尤其是可写属性。因此,采用更好的封装设计,对于AutoFixture和您自己以及您的同事来说,API可能更容易使用。

Additionally, the .NET Framework Design Guidelines recommends against exposing arrays as properties - particularly writable properties. Thus, with a better encapsulated design, the API might be much easier to work with, both for AutoFixture and yourself and your colleagues.

鉴于上述API,我不会看不到任何可以更轻松地使用它的方式。考虑如何删除循环引用并使集合属性为只读,这会容易得多。

Given the API above, I don't see any way this can be made much easier to work with. Consider how to remove the circular reference and make collection properties read-only, and it will be much easier.

出于记录,我没有用多年的循环参考,因此很有可能避免那些父母/子女的关系。

For the record, I haven't written an API with a circular reference for years, so it's quite possible to avoid those Parent/Child relations.

这篇关于在AutoFixture中创建复杂子级时使用父级属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆