实体框架" code气味"题 [英] Entity Framework "Code Smells" Question

查看:117
本文介绍了实体框架" code气味"题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是英孚的粉丝,想使用它,但我不一定是一个球迷协会是如何工作的每本身。如果我有一个包含在里面,也许3行简单的查找表,通常被视为code枚举,我不喜欢,我切实要做到这一点使用的缔合的EF分配一个值:

I'm a fan of Ef and want to use it but I'm not necessarily a fan of how associations work per-se. If I have a simple lookup table containing maybe 3 rows in it and would typically be treated as an enum in code, I don't like that I effectively have to do this to assign a value using the associating in EF:

myEntity.MyLookup = db.MyLookups.First(毫升=> ml.MyLookupId == 5);

myEntity.MyLookup = db.MyLookups.First(ml => ml.MyLookupId == 5);

我的意思是,它不仅看起来时髦,但它也随后再次查询该数据库只是这样我就可以有效地分配数值5。我知道这也可以通过设置通过MyLooku preference的的EntityKey但似乎连smellier。

I mean, not only does it look funky but it also then queries the db again just so I can effectively assign the value "5". I know this is also possible by setting the EntityKey via MyLookupReference but that seems even smellier.

我的问题是,是否有可能摆脱联营的商店重新presentation和公正的对待一切,对我的实体标有通过不具有导航性能,并允许我只是做联接在必要时?

My question is, is it possible to get rid of the Store representation of an associate and just treat everything as a scalar on my entities there by not having the navigation properties and allow me just to do joins when necessary?

此外,如果这是可行的,它有对谁的实体进行跟踪或类似的东西什么影响?谢谢!

Also, if this is feasible, does it have any impact on who entities are tracked or anything like that? Thanks!

推荐答案

我觉得这是另一个原因保持业务逻辑与数据访问层分开。您可能已经这样做了,这当然。

I think this is another reason for keeping Business Logic separate from data access layers. You may already do this, of course.

作为一个例子,如果有一个用户表,和一个UserTypes表。您的数据层需要解决这个问题,但在业务层并不需要了解它,假设你有某种工厂方法在你的数据适配器,需要一个用户和用户类型,并将它们转换为管理员(对于例子)。

As an example, if you have a User table, and a UserTypes table. Your data layer needs to deal with this, but the business layer doesn't need to know about it, assuming you have some kind of factory method in your data adapter that takes a user and a user type and converts them to an Administrator (for example).

不知道这会有所帮助,只是想我给意见。

Not sure if this helps, just thought i'd give an opinion.

这篇关于实体框架" code气味"题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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