模拟的数据上下文和外键/导航属性 [英] Mocked datacontext and foreign keys/navigation properties

查看:101
本文介绍了模拟的数据上下文和外键/导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无法解决的问题,我想知道是否有人可以提供一些建议.

I have a problem that I haven't been able to find a solution to, and I wonder if anyone could give some advice.

我有一个模拟的数据上下文/对象集,它是通过接口和t4模板完成的,带有一些ninject魔术,目的是要在内存数据集中进行单元测试.

I have a mocked datacontext/objectset, done through interfaces and t4 templates, with some ninject magic, with the intent of having in memory datasets for unit testing.

但是,您应该如何使用外键值/导航属性?

However, what should you do with the foreign key values/navigation properties?

让我们说我有酒店和客户,ctx.Hotels有一些价值,而Customer.Hotels没有.如果是一对一的关系,get就是这样的:

Lets say I have hotels and customers, ctx.Hotels has some values, but Customer.Hotels does not. The get is something like this if it is a one-to-one relationship:

return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Hotel>("HotelModel.FK_Customers_Hotels", "Hotel").Value;

和一对多:

return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<BookingRow>("HotelModel.FK_BookingRows_Customers", "BookingRow");

我的技能水平甚至不足以理解这里发生的事情.

My skill level just isn't enough to even understand what is going on here.

大师朱莉·勒曼(Julie Lerman)确认这是死胡同.您无法正确模拟实体对象,为此需要POCO.

[edit:] Great Master Julie Lerman confirms that this is a dead end. You can't properly mock entityobjects, you need POCOs for that.

推荐答案

在使用基于EntityObject的实体时模拟ObjectContext几乎是不可能的,因为例如RelationshipManager是不能用您的模拟代替的真实类.另外,您的实体严重依赖不可模拟的EF代码.

Mocking ObjectContext when you are using EntityObject based entities is mostly impossible because for example RelationshipManager is a real class which cannot be replaced with your mock. Also your entities are heavily dependent on non mockable EF code.

注意:大多数"是因为您可以模拟它,但是需要特殊的框架来拦截对真实对象的调用,并将其转发给您的方法.只有使用TypeMock隔离器或MS Moles才有可能.

Note: "Mostly" because you can mock it but you need special framework intercepting calls to real objects and forwarding them to your methods instead. That is possible only with TypeMock Isolator or MS Moles.

顺便说一句.模拟EF代码是您不想做的-通过

Btw. mocking EF code is something you don't want to do - go through this answer and linked answers. Some of them targets newer EF API but the problems are still same.

这篇关于模拟的数据上下文和外键/导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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