简体   繁体   English

将ORM与没有定义关系的数据库一起使用?

[英]Using an ORM with a database that has no defined relationships?

Consider a database(MSSQL 2005) that consists of 100+ tables which have primary keys defined to a certain degree. 考虑一个由100多个表组成的数据库(MSSQL 2005),这些表在一​​定程度上定义了主键。 There are 'relationships' between tables, however these are not enforced with foreign key constraints. 表之间存在“关系”,但是这些关系没有使用外键约束来强制执行。

Consider the following simplified example of typical types of tables I am dealing with. 考虑以下我正在处理的典型表类型的简化示例。 The are clear relations between the User and City and Province tables. 用户表与城市表和省表之间的关系很明确。 However, they key issues is the inconsistent data types in the tables and naming conventions. 但是,它们的关键问题是表和命名约定中的数据类型不一致。

User:
    UserRowId [int] PK
    Name [varchar(50)]
    CityId [smallint]
    ProvinceRowId [bigint]

City:
    CityRowId [bigint] PK
    CityDescription [varchar(100)]

Province:
    ProvinceId [int] PK
    ProvinceDesc [varchar(50)]

I am considering a rewrite of the application (in ASP.net MVC) that uses this data source as is similar in design to MVC storefront. 我正在考虑改写使用此数据源的应用程序(在ASP.net MVC中),其设计与MVC店面的设计类似 However I am going through a proof of concept phase and this is one of the stumbling blocks I have come across. 但是,我正在经历概念验证阶段,这是我遇到的绊脚石之一。

  1. What are my options in terms of ORM choice that can be easily used and why? 关于可以轻松使用的ORM选择,我有哪些选择?为什么?

  2. Should I even be considering an ORM? 我是否应该考虑使用ORM? (The reason I ask this is that most explanations and tutorials all work with relatively cleanly designed existing databases, or newly created ones when compared to mine. I am thus having a very hard time trying to find a way forward with this problem) (我之所以这样问,是因为大多数解释和教程都可以在相对干净设计的现有数据库中使用,或者与我的数据库相比,它们都可以在新创建的数据库中使用。因此,我很难找到解决该问题的方法)

  3. There is a huge amount of existing SQL queries, would a datamappper(eg IBatis.net) be more suitable since we could easily modify them to work and reuse the investment already made? 现有大量的SQL查询,datamappper(例如IBatis.net)是否会更合适,因为我们可以轻松地对其进行修改以使其工作并重新使用已进行的投资?

I have found this question on SO which indicates to me that an ORM can be used - however I get the impression that this a question of mapping? 我在SO上发现了这个问题 ,这向我表明可以使用ORM,但是我给人的印象是这是映射问题?

Note: at the moment, the object model is not clearly defined as it was non-existent. 注意:目前,对象模型尚不明确,因为它不存在。 The existing system pretty much did almost everything in SQL or consisted of overly complicated, and numerous queries to complete functionality. 现有的系统几乎可以执行SQL的几乎所有操作,或者由过于复杂和众多查询来完成功能。 I am pretty much a noob and have zero experience around ORMs and MVC - so this an awesome learning curve I am on. 我几乎是一个菜鸟,对ORM和MVC的经验为零,所以我的学习曲线很棒。

I agree with Ben. 我同意本。

I was in this situation with a LAMP stack. 我在这种情况下使用LAMP堆栈。 An old dirty, bady coded website needed bringing up to scratch. 一个旧的,肮脏的,坏的编码网站需要重新启动。 It was literally the worst database I have seen, coupled with line after line of blind SQL execution. 从字面上看,这是我见过的最糟糕的数据库,再加上一行又一行的盲目SQL执行。

Job? 工作? Get rid of all that SQL very quickly and replace it with an abstraction. 快速消除所有SQL,并用抽象代替它。 Which ORM? 哪个ORM? I found that using an existing ORM to fit over a bad database (most databases really) retrospectively is bad news. 我发现,回顾性地使用现有的ORM来覆盖不良数据库(实际上是大多数数据库)是一个坏消息。 I think this is a problem with ORMs, they move database/storage concerns closer to the application ... not further away. 我认为这是ORM的问题,它们将数据库/存储问题移到了应用程序的附近,而不是更远的地方。

My Solution: A reflective ORM that used only the existing database state to work out what was going on. 我的解决方案:一个反射式ORM,它仅使用现有数据库状态来确定正在发生的事情。 All selects, inserts, updates and what-not used views/stored proceedures to mask the cruddy database. 所有选择,插入,更新和不使用的视图/存储过程都掩盖了原始数据库。 It is powered by a linq-esque API just rewrite the grim SQL with. 它由linq-esque API提供支持,只需重写严峻的SQL。 Boiled around 100klocs SQL statements down to less than 2klocs. 将大约100klocs的SQL语句煮沸,直到少于2klocs。

pros : I can gradually port the database to a better structure behind the views and proceedures. 优点 :我可以逐步将数据库移植到视图和过程后面的更好结构中。 IMHO this is how all databases should be organised , taking full advantage of the abstraction that SPs and views provide. 恕我直言,这是应该如何组织所有数据库的方式 ,充分利用SP和视图提供的抽象。 I never want to see a single SQL statement (or an ORM masquerading as SQL) directly against a table. 我从不希望直接针对表看到单个SQL语句(或伪装为SQL的ORM)。

That's my story. 那是我的故事。 An overengineered way to slot a nice abstraction above an existing and crap database, without rewriting the database first, and without crowbaring an ORM into the mix making things much more complex. 一种过度设计的方法,可以在现有数据库和废话数据库之上放置漂亮的抽象,而无需先重写数据库,也无需在混合中使用ORM,这使事情变得更加复杂。

a hack, no doubt , but it works so well I am using it in projects where I can design the database from scratch anyway ;) 毫无疑问 ,这是一个hack,但是它工作得很好,我在我可以从头开始设计数据库的项目中使用它;)

The amount of work involved in trying to keep the existing schema and then crowbaring it into a much more structured orm pattern would probably be large and complex. 尝试保留现有模式,然后将其撬入结构化得多的orm模式中涉及的工作量可能很大且很复杂。 If you are rewriting the whole system and retiring the old one then i would devise my data model create a new db and set of classes,maybe using linq2sql, then write a data migration script to move the data from the old schema to the new one. 如果您要重写整个系统并淘汰旧系统,那么我会设计数据模型创建一个新的数据库和一组类,也许使用linq2sql,然后编写一个数据迁移脚本以将数据从旧模式移至新系统。 That way your complex fiddly code is all in the migration and you don't have to deal with maintining and managing a complex mapping between a structured class model and a badly designed db. 这样一来,您的复杂代码就已全部包含在迁移中,而您不必处理和管理结构化类模型与设计不良的db之间的复杂映射。

We've just faced this problem with an awful schema design (randomly has primary keys, no foreign keys at all, badly designed tables - just a mess). 我们只是通过糟糕的架构设计来面对这个问题(随机具有主键,根本没有外键,设计不良的表-只是一团糟)。

We had the luxury of technology choice, and went MVC2 front end (irrelevant to your question), and had 2 devs split off - one try to model using NHibernate, the other using Entity Framework 4. 我们拥有丰富的技术选择,并且选择了MVC2前端(与您的问题无关),并拆分了2个开发人员-一种尝试使用NHibernate进行建模,另一种尝试使用Entity Framework 4。

I hasten to add that we had a strong idea of what we wanted from our domain model, and modelled that first (not wanting to be constrained by the database), so our 'User' object from a schema point of view actually spanned 5 tables, we encapsulated a lot of the business logic so that the domain model wasn't aneamic, and once we were happy with our User object, we started the process of trying to plugin the ORM. 我必须补充一点,我们对域模型的需求有很强的想法,并首先对其建模(不希望受到数据库的约束),因此从架构角度来看,我们的“用户”对象实际上跨越了5个表,我们封装了很多业务逻辑,以至于域模型不是动态的,一旦我们对User对象感到满意,我们便开始尝试插入ORM的过程。

I can say without hesitation in both cases (NH and EF4) the compromises we had to make on our model in order to shoe-horn the implementation in was phenomenal. 我可以毫不犹豫地说,在这两种情况下(NH和EF4),我们不得不在模型上做出折衷,以勉强完成其中的实现。 I'll give you the examples from EF4 as that's the one I was most closely involved in, others may be able to relate these to other ORMs. 我将给您提供EF4中的示例,因为这是我最密切参与的示例,其他示例可能将这些示例与其他ORM相关联。

private setters 私人二传手

Nope - not on your life with EF4. 不,不是您使用EF4的生活。 Your properties must be public. 您的财产必须是公开的。 There are workarounds (for example, creating wrappers around properties that were coming in from your DB) 有一些解决方法(例如,围绕来自数据库的属性创建包装器)

enums 枚举

Again, no - there was a wrapper concept and a 'mapping' to try to get a lookup int out of the DB into the models enum types. 再一次,没有-有一个包装器概念和一个“映射”,试图将查询int从数据库中移到模型枚举类型中。

outcomes 结果

We persevered for a while with both approaches to get to a point where we'd completed the mapping of a user, and the outcome was that we had to compromise our domain model in too many ways. 我们用这两种方法坚持了一段时间,以至于我们已经完成了用户映射,结果是我们不得不以太多方式折衷我们的领域模型。

where did we go after that? 那之后我们去了哪里?

Linq to SQL with our own mapping layer. Linq to SQL具有我们自己的映射层。 And we've never looked back - absolutely fantastic - we've written the mapping layer ourselves once that takes the Dto object down at the Dal layer and maps it (as we specify it) into our Domain model. 而且我们再也没有回过头来-绝对很棒-我们自己编写了映射层,它将Dto对象放到Dal层并将其映射(按我们的指定)到我们的Domain模型中。

Good luck with any investigation of ORMs, I'd certainly re-investigate them if I had a decent schema to base them off, but as it stood, with an awful schema, it was easier to roll our own. 如果对ORM有任何调查,祝您好运,如果我有一个不错的架构来作为它们的基础,我当然会重新进行调查,但是就目前而言,如果采用可怕的架构,则更容易推出自己的架构。

Cheers, Terry 干杯,特里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM