使用nhibernate和sql server进行域驱动设计(domain driven design with nhibernate and sql server)

编程入门 行业动态 更新时间:2024-10-28 03:21:33
使用nhibernate和sql server进行域驱动设计(domain driven design with nhibernate and sql server)

我有一个参考应用程序,我用它来解决DDD问题,我目前的重点是持久性。 这篇文章的另一个标题可能是DDD / TDD持久性工具和方法,但这是一个(非常)广泛的主题,我确实在SQL Server上有一个特定的问题。

我将在本段中概述我当前的工具集作为背景,所以请随意跳过它来回答问题。 我正在使用NHibernate,Fluent NHibernate,Rhino,Visual Studio 2008和SQL Server。 我目前的想法是在任何数据映射更改后使用NHibernate生成数据库,查看VS 2008中生成的表,并查看我的持久性映射测试是否全部通过。 我本来想用SQLite作为测试数据库,但我怀疑它是在某些方面(如参照完整性)准确衡量正确的映射。 然后我尝试使用SQL Ce,但发现使用VS生成的表眼球令人沮丧。 这就是为什么我现在想要首先使用SQL Server。

以下命令对象生成db:

public class GenerateNewDb_SqlServer : ICommand { public GenerateNewDb_SqlServer(Configuration cfg) : base(string.Format("Update a Sql Server Database")) { _cfg = cfg; } public override void Execute() { try { var schema = new SchemaExport(_cfg); schema.Create(true, true); } catch (Exception ex) { Console.WriteLine("Schema Export Error Message: " + ex); } } }

我可以使用NHibernate.Cfg.Configuration对象删除数据库吗? 代码会是什么样的?

干杯,Berryl

I have a reference application that I use to work through DDD issues, and my current focus is on persistence. An alternate title for this post could have been DDD / TDD persistence tools and methods, but that is a (very) broad topic and I do have a specific question on SQL Server.

I'm going to outline my current tool set in this paragraph as background, so feel free to skip it to just answer the question. I'm using NHibernate, Fluent NHibernate, Rhino, Visual Studio 2008 and SQL Server. My current thinking is to use NHibernate to generate the db after any data mapping change, look at the generated tables in VS 2008, and see if my persistence mapping tests all pass. I had originally wanted to use SQLite as the test db, but I am skeptical that it is an accurate gauge of correct mapping in some ways (like referential integrity). I then tried using SQL Ce, but found it frustrating to eye ball the generated tables with VS. That's why I'm now figuring to just use SQL Server in the first place.

The following command object generates the db:

public class GenerateNewDb_SqlServer : ICommand { public GenerateNewDb_SqlServer(Configuration cfg) : base(string.Format("Update a Sql Server Database")) { _cfg = cfg; } public override void Execute() { try { var schema = new SchemaExport(_cfg); schema.Create(true, true); } catch (Exception ex) { Console.WriteLine("Schema Export Error Message: " + ex); } } }

Is there something off the NHibernate.Cfg.Configuration object I can use to drop the db? What would the code look like?

Cheers, Berryl

最满意答案

值得庆幸的是,它已经是SchemaExport的一部分。 我不确定它是否会删除整个数据库,但它会删除所有引用的SQL对象。 像这样的东西?

private void DropSchema(Configuration _cfg) { new SchemaExport(_cfg) .Drop( false, //script to the console true, //execute against db ); }

PS通常,您会看到此命令直接在SchemaExport.Create之前运行,因此您将获得一个新的数据库...

PPS为NHibernate提供了很棒的模型驱动的东西(这个截屏系列真棒!)

Thankfully, it's already part of SchemaExport. I'm not sure if it will drop the entire database, but it will drop all the referenced SQL objects. Something like this?

private void DropSchema(Configuration _cfg) { new SchemaExport(_cfg) .Drop( false, //script to the console true, //execute against db ); }

P.S. Usually, you'll see this command run directly before SchemaExport.Create so you get a fresh database...

P.P.S. bunch of great model-driven stuff for NHibernate here (this screencast series is amazing!)

更多推荐

本文发布于:2023-08-04 21:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:server   sql   nhibernate   design   driven

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!