推荐的asp.net MVC模型设计方法
当涉及到我的模型设计时(我正在使用 Dapper.net),我正在尝试为我即将开始的新项目确定最佳方法。
我喜欢让我的模型具有对象而不是外键属性的想法,即
public Post LastPost { get; set; }
vs
public int LastPostId { get; set; }
但是,如果我实现这种干净的方法,我必须多重映射到所有对象(这会导致对象内对象的潜在循环引用,(或者必须在某个点停止多重映射,因此在对象树中的某个点最终会出现 NULL 对象)另外,如果我在一定程度上进行多重映射,那么我可能会导致不必要的工作,执行连接。等等,当他们并不总是会这样的时候 。
或者,如果我决定使用多重映射“根据需要”在对象中填充对象(在我的某些存储库方法中执行多重映射,因为它需要,而在其他存储库方法中,不必费心填充) 对象),那么感觉有点脏,因为我不能总是确定一个对象(对象内)是否为空,
我在其中使用了NHibernate(或至少是它的一些更基本的功能)。过去并没有陷入困境因为我的模型中总是有对象,如果/当需要它们时,我可以依靠延迟加载来获取它们 - 但是,没有使用 Dapper.net 进行延迟加载,我真的不确定最好的方法?
I'm trying to decide the best approach for a new project I'm about to start on, when it comes to my model design (and I'm using Dapper.net).
I like the idea of having my models with objects rather than Foreign Key properties, i.e.
public Post LastPost { get; set; }
vs
public int LastPostId { get; set; }
However, if I implement this sort of nice clean approach, I have to multi-map to all objects (which leads onto potential circular referencing of objects within objects, (or have to stop multi-mapping at a certain point and therefore end up with NULL objects at some point down the object tree). Also, if I do multi-map to an extent, then Im perhaps causing unnecessary work, performing joins etc when they're not always going to be needed.
Or, if I decide to use multi-mapping to populate my objects within objects on a 'as needed' basis (in some of my repos methods perform multi mapping because its needed, and in other repos methods, don't bother populating the objects), then it feels kind of dirty in that I can't always be sure if an object (within an object) is null or not.
I've used NHibernate (or at least some of its more basic functionality) in the past and not had the dilemma as I always had objects within my models and if/when they were needed, I could rely on lazy loading to go get them - However, not having that lazy loading with Dapper.net I'm really unsure of the best approach to go with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不两全其美呢?
这允许您在需要时使用多重映射进行急切加载,并在您懒惰时进行延迟加载;
Why not have the best of both worlds?
This allows you to eager load when needed with multi mapping and lazy load, when you are lazy;
很好,这就是延迟加载代理模式。
good, It is Lazy loading proxy pattern.