开发者

Is there a way of using NHibernate without connection to DB?

I need that Nhibernate only generate pure SQ开发者_如何学CL, which I will send to my C functions.


Custom Driver
Build a driver that calls your C layer by implementing NHibernate.Driver.IDriver.

The SQLite driver is a good place to start as that is a C library. Your IDriver will be almost the same as SQLiteDriver. The NHibernate source code will be useful for this.

You will also need a IDbConnection wrapper for your C library. Of the SQLite wrappers, csharp-sqlite has the smallest source, it should be helpful.

Odbc
NHibenate already has a driver for ODBC connections, NHibernate.Driver.OdbcDriver.


There isn't a simple way to do this. In theory, NHibernate doesn't have to produce SQL at all; it's just that your dialect happens to (for example, it could just have some text files as the data store). The interface doesn't expose that the SQL because it breaks the abstraction barrier.

However, if you use the show_sql option, NHibernate will log the SQL, and you can use that. The only way I can think of is to set up a dummy database and run the NH queries against it. Build a custom log4net appender to capture the SQL and just store it in a variable. Now you can associate that with the query. So then you'd have something like

RunQueryAndCaptureSql(session.CreateQuery("from Stuff s where s.Thing = 'dude'"));

which blocks waiting for the appender to append the SQL and then returns that. Inelegant as hell and you'll have to make sure you deal with threading issues, but it should work.

Frankly, I'd probably just hack the NH source instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜