可以的LINQ to SQL生成的对象脱钩? [英] Can LINQ to SQL generated objects be decoupled?

查看:103
本文介绍了可以的LINQ to SQL生成的对象脱钩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢LINQ to SQL的,但现在看来似乎产生紧密耦合到它们存储在数据库中,这似乎是一个坏事类。

I like LINQ to SQL, but it seems like the classes it generates are tightly coupled to the database they are stored in, which seems like a Bad Thing.

例如,使用叶奥尔德Northwind数据库,如果我创建Products表中的DBML,生成一个产品类。我可以在任何其他层,这是一个好主意使用这个类,但如果我决定我宁愿使用普通的旧式ADO.NET(或切换数据库),我将不得不重新创建产品类,与所有其他一起的模式。

For example, using ye olde Northwind database, if I create the dbml with the Products table, a Product class is generated. I can use this class in any other tier, which is all well and good, but if I decide I'd rather use plain old ADO.NET (or switch databases), I'll have to recreate the Product class, along with every other "model."

有没有办法解决?或单独创建对象模型,然后映射到它们的表?我打得周围提供的各种映射类,但还没有找到一个满意的答案。

Is there a way around this? Or to create your object models separately, and then have the tables mapped to them? I've played around with the various mapping classes provided, but haven't found a satisfactory answer yet.

推荐答案

所有这些问题的答案,也没有联系!也许我可以帮助:

All these answers and no links! Maybe I can help:

的damieng

局部类事情,马库斯国王提到

我走过这个困难了几次,我终于实现了我的最后一个项目是使用接口,这是在解决方案中所有不同的项目之间共享的合同,并且具有部分类实现它冷落。

I have languished through this difficulty a couple of times, what I ended up doing on my last project was using interfaces as the contract that's shared between all of the different projects in the solution, and having the partial classes implement it.

[Table(Name="Products")]
public partial class Product: IProduct { }

是的,不幸的是它采取了一些魔法反射,使之成为了POCO实施工作。

And yes, unfortunately it took some reflection magic to make it work for the POCO implementation.

在最后,如果你是真正关心它,我会与NHibernate去(我真的不喜欢它要么),这不正是加里Shulter似乎描述。

In the end, if you are truly concerned about it, I'd go with NHibernate (I don't really like it either), which does exactly what Garry Shulter seems to be describing.

希望帮助!

这篇关于可以的LINQ to SQL生成的对象脱钩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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