具有自动生成功能的 .NET ORM 解决方案:Subsonic、Castle AR,...?
我曾经使用自定义数据映射库,目前我正在尝试切换到更广泛的 ORM 解决方案。
经过一些实验,我将我的要求细化为以下几点:
- 能够从数据库模式生成可用的类(SQL Server 支持就足够了),
- 支持 ActiveRecord 模式,
- 以编程方式配置(通过代码或属性,没有 HBM 文件),
- 免费。
可以请您推荐一款吗?
到目前为止我已经尝试过:
Subsonic 3.0
我目前最喜欢的一个,因为它感觉在功能性和简单性之间取得了很好的平衡。
我不喜欢的是:
- 使用 IQueryable<>以及一对多关系两端的复数名称 - 这对我来说似乎相当违反直觉;
- 为所有类生成一个文件 - 就像每个类一千行一样,我对代码文件这么大有一种不好的感觉;
T4 处理是自动调用的,因此具有最新架构的数据库必须始终可用。
Castle ActiveRecord
CastleAR 与 ActiveWriter 插件几乎是我所需要的,但 ActiveWriter 的代码并不完美(似乎不支持 Nullable<> 属性,并且默认的一对多实现不支持)不起作用)并且我找不到如何手动修复此代码。
Darkside GeneratorStudio 生成更好的代码 - 正确的定义,每个实体一个文件 - 尽管它使用像 RefclassIdRefclass 这样的神秘名称。就我个人而言,我更喜欢 Studio 插件而不是独立应用程序,但这肯定是一个小问题。
CastleAR 发行版中捆绑了大约 20 个文件;虽然它本身不是问题,但感觉像是超重了。我不需要太多关于 NHibernate 和其他东西的深奥知识吗?
Fluent NHibernate
广泛推荐,但我无法找到它的代码生成器。
我是不是错过了什么重要的事情?
I used to work with a custom data mapping library, and curently I'm trying to switch to a more widespread ORM solution.
After some experimentation, I refined my requirements to the following:
- able to generate usable classes from database schema (SQL Server support is enough),
- support for ActiveRecord pattern,
- programmaticaly configurable (via code or attributes, no HBM files),
- free.
Could you please recommend one?
So far I have tried:
Subsonic 3.0
The one I currently like most, as it feels like a good balance between functionality and simplicity.
What I don't like:
- uses IQueryable<> and plural names for both ends of one-to-many relationship - that seems rather counter-intuitive to me;
- generates one file for all classes - like a thousand lines per class, I have a bad feeling about code files being that large;
T4 processing is invoked automatically, so a database with up-to-date schema must be available all the time.
Castle ActiveRecord
CastleAR with ActiveWriter add-in is almost what I need, but ActiveWriter's code isn't perfect (it seems, Nullable<> properties are not supported, and default one-to-many implementation doesn't work) and I couldn't find how to fix this code manually.
Darkside GeneratorStudio produces better code - correct definitions, one file per entity - though it uses cryptic names like RefclassIdRefclass. Personally I like Studio addin more than standalone app, but this certainly is a minor issue.
There're about twenty files bundled in the CastleAR distribution; though not a problem by itself, it feels like overweight. Won't I need too much esoteric knowledge about NHibernate and other stuff?
Fluent NHibernate
Widely recommended, but I wasn't able to find a code generator for it.
Have I missed something important?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
NHibernate 不遵循活动记录模式,它遵循存储库模式。
FluentNHibernate 的目标是不再需要维护映射文件、代码生成等。它支持 Fluently 定义的 NHibernate HBM 文件的 C# 等效项,这非常好,并且比 XML 更易于维护,但它更强大的方面是它的自动映射功能。如果您的数据库是按照严格定义的约定设计的,则可以配置 FNH,它将生成所有域对象到数据库结构的正确映射。
如果您对设计数据库并让它决定您的域模型更感兴趣(这会让我感觉很肮脏),这里有一个专门针对 NHibernate 生成器
回答您的评论: NHibernate 本身不会更改数据库本身,但是其中包含一个名为 SchemaExport 的实用程序类,它提供了一个名为 Create 的工厂方法。您可以使用它来生成 SQL 语句,以 NHibernate 查看关系结构的方式创建数据库。并且可以选择直接针对数据库运行或写入控制台。
该实用程序对于将域向前生成到数据库非常有用,这就是我目前为所有新应用程序开发编写数据库的方式。我仍在努力找出维护数据库版本的最佳方法。最坏的情况只是 SC sql 输出,并且需要对更改架构的每一行进行注释,以使其具有特定的更新/删除语句来实现结果。这将使按版本向前/向后滚动变得容易,或者只需执行整个脚本即可从头开始创建数据库。
如果您纯粹进行反向映射,则应该有工具可用于生成数据库需要映射到等效类的相应 HBM(即,如果您最初在域模型中编写了这些类,它将导出当前的匹配模式)数据库)
NHibernate does not follow the active record pattern, it follows the repository pattern.
FluentNHibernate's goal is the end of needing to maintain mapping files, code generation etc. It supports Fluently defined C# equivalents of NHibernate HBM files which is very nice and alot more maintainable than XML however it's much stronger aspect is its auto mapping capabilities. If your database is designed with strongly defined conventions FNH can be configured that it will generate the correct mapping of all of your domain objects correctly to your database structure.
If you're more interested in designing the database and having that dictate your domain model (this would make me feel very dirty) there is a question here devoted to NHibernate Generators
Answer to your comment: NHibernate itself does not alter the database itself however there is a utility class included called SchemaExport that provides a factory method called Create. You can use this to generate the SQL statements that it would take to create your database the way NHibernate sees your relationship structure. And can be optionally either directly run against the database or write to the console.
This utility is very useful for forward generation of your domain to your database, it's currently how I am writing my database for all my new application development. I am still working on figuring out the best way to maintain versions of the database. Worst case scenario would to be just SC the sql output and require comments for each line that changes the schema to have it's specific update/delete statements to achieve the result. This would make it easy to roll forward/backwards by version or just execute the entire script to make the db from scratch.
If you go purely for reverse mapping, there should be tools available to generate the corresponding HBM your database needs to map to the equivalent classes (ie if you would have written those classes originally in your domain model it would export a matching schema of your current database)
Linq-to-SQL 从数据库架构自动生成。一个大问题是属性名称是小写的,因此您需要像 这个 这样的工具来重命名它们。 它不限于 SQL Server。
它有点支持 ActiveRecord 模式 - 它是非常接近。
这些类可通过 dbml 文件进行配置,该文件只是一个 XML 文件。 Visual Studio 生成一个
designer.cs
文件,允许您添加到它生成的实体,因为它们是作为partial
类生成的。除了 NH 和 Subsonic 之外,还有其他一些值得一看:
Linq-to-SQL autogenerates from the database schema. One big problem is the property names are lowercase so you need a tool like this one to rename them. It isn't restricted to SQL Server.
It sort of supports the ActiveRecord pattern - it's very close.
The classes are configurable via the dbml file which is simply an XML file. Visual Studio generates a
designer.cs
file that allows you to add to the entities it produces as they're produced aspartial
classes.A few others to have a look at besides NH and Subsonic:
关于您不喜欢 SubSonic 的一些想法:
对我来说相当违反直觉;
确实,我同意这是非常违反直觉的,而且我从来没有弄清楚为什么会这样,但是您可以很容易地修改模板来解决这个问题。
你可能会认为这可能会导致问题,但我还没有遇到过任何问题,也没有在 stackoverflow 上看到过相关报道。
仅当您编辑 t4 文件时它才会自动调用,因此实际上您只需要在更改模板时可用的最新架构。
A few thoughts on the things you don't like about SubSonic:
rather counter-intuitive to me;
True, I agree this is very counter intuitive and I've never got to the bottom of why it is that way, but you can pretty easily modify the templates to fix this.
You'd think this might cause problems but I've yet to come across any and I've yet to see one reported on stackoverflow.
It's only invoked automatically if you edit the t4 files so really you only need an up-to-date schema available when you're making changes to the templates.
我们使用 CodeSmith 进行代码生成,并强烈推荐它作为生成工具。
如果您查看 CodeSmith,它支持各种 ORM/业务对象模板。
几年前,我们实施了自己的 CSLA(请参阅 http://www.lhotka.net/)模板掌握 CSLA,它不仅仅是 ORM。 CSLA 很棒,工作得很好,而且具有高度可扩展性。我们现在创建了自己的框架,并放弃了核心 CSLA 框架,因为我们不需要所有功能,并使事情变得更加轻量级。
We use CodeSmith for Code Generation and can highly recommend it as a generation tool.
If you look at CodeSmith it supports various ORM / Business Object Templates.
We implemented our own CSLA (see http://www.lhotka.net/) templates a few years ago to get to grips with CSLA which is more than just ORM. CSLA is great and works very well and its highly scalable. We have now created our own framework and dropped the core CSLA framework as we didnt need all the features and made things a bit more lightweight.
我想知道较旧的 SubSonic 2.2 是否更适合您的要求?它确实为每个表生成单独的类,仅在您告诉它时重新生成,并且支持 ActiveRecord 模式。请参阅文档中的更多详细信息 http://subsonicproject.com/docs/Main_Page
I wonder whether the older SubSonic 2.2 may be a better fit for your requirements? It does generate individual classes per table, only regenerates when you tell it to, and supports the ActiveRecord pattern. See more details in the docs at http://subsonicproject.com/docs/Main_Page
关于之前的帖子 CSLA 不是 ORM(您手动进行从数据库到业务对象的映射)。 CSLA、NHibernate、subsonic 等不支持活动记录模式。
如果您正在考虑不支持活动记录模式的框架,那么我会考虑 Habanero (http://www.habanerolabs. com/)这是开源的,允许您通过 XML 配置或代码构建业务对象。
Habanero 不仅仅是一个 ORM,它还具有丰富的域(业务对象)支持以及强大的对象模型代表您的域对象(业务对象)。
Regarding previous Posts CSLA is not an ORM (You do the mapping from the DB to your Business Objects manually). CSLA, NHibernate, subsonic etc do not support the active records pattern.
If you are considering Frameworks that do not support the Active Records pattern then I would consider Habanero (http://www.habanerolabs.com/) this is open source and allows you to build your Business Objects either via an XML configuration or via code.
Habanero is far more than an ORM and has a rich domain (Business Object) support with a powerfull object model for representing your Domain objects (Business Objects).