【问题标题】:Nhibernate data base independent error handlingNhibernate 数据库独立错误处理
【发布时间】:2015-04-14 12:31:57
【问题描述】:

试图为我的最后一个问题 (https://stackoverflow.com/questions/29539767/sql-nhibernate-exceptions-with-sqlite-and-c-sharp-and-known-error-codes) 找到解决方案,我决定用我自己的自定义错误来包装所有异常。到目前为止,这是我的代码:

    public Exception Convert(NHibernate.Exceptions.AdoExceptionContextInfo exInfo)
    {
        var sqle = (System.Data.SQLite.SQLiteException)exInfo.SqlException;
        var errorDescription = exInfo.SqlException.Message.ToString();
        if (sqle != null)
        {
            //Definition of error codes (for SQLite) to catch and handle
            switch (sqle.ErrorCode)
            {
                //Most possible error in our project: case 19
                case 19:
                    return new ConstraintViolationException(exInfo.Message,
                        sqle.InnerException);
                case 5:
                    return new DbLockedException();
                case 6:
                    return new TableLockedException();
                case 8:
                    return new ReadOnlyDbException();
                case 9:
                    return new InterruptException();
                case 10:
                    return new IOException();
                case 11:
                    return new DbCorruptException();

我从这里遵循了自定义异常的想法:Returning a custom exception

现在我面临一个问题:

因为我们正在使用 NHibernate。我们希望保持我们的代码数据库独立。使用您可以看到的代码,它只适用于 SQLite。

  • 有谁知道或知道如何管理错误以 无论我使用哪个数据库,都能抓住它们?

如果你想让你的程序独立于数据库,代码错误不是一个好主意,因为每个数据库都有自己的代码错误和异常。

感谢您的提醒!

【问题讨论】:

    标签: c# exception nhibernate error-handling


    【解决方案1】:

    我将依赖于数据库的设置封装在专用程序集中:

    • MyApplication.Data.SqlLite
    • MyApplication.Data.Oracle

    每个程序集都包含一组实现公共接口的类以放入一个公共程序集中。在您的情况下,我将创建一个名为 DbExceptionConverter 的接口并在它们的程序集中实现。在应用程序开始时,我会在容器中注册正确的实现。

    您也可以将它们全部放在抽象工厂中。

    【讨论】:

    • 感谢您的回答!听起来很好,我采用的方法有点复杂。我认为这将是最好的方法。我正在考虑诸如捕获常见异常然后进行翻译之类的事情,但我认为这并不像听起来那么容易。
    【解决方案2】:

    好吧,我最后做了如下:

    1- 我保留了 ORM 项目,因为它实际上并不需要任何特定的数据库引用或配置(驱动程序,...等)(程序集将知道 hibernate.cfg.xml 文件使用哪个驱动程序)

    2- 正如@onof 所建议的,我创建了一个DbExceptionConverter 程序集,其中我拥有所有与数据库相关的设置以及我想要使用的每个数据库的例外情况。每个类都用自己的代码包装异常。

    我在这里遇到的问题是:“现在,如何加载自定义 DbExceptionHandler?” 好吧,我只是使用“hibernate.cfg.xml”文件中的sql_exception_converter属性字段如下:

    对于 SQLite:(注意 sql_exception_converter 属性)

    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
      <session-factory>
        <property name="adonet.batch_size">5000</property>
        <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
        <property name="connection.connection_string">Data Source=DBFile.db;Version=3;New=True;UseUTF8Encoding=True;</property>
        <property name="sql_exception_converter">DbExceptionConverter.SQLiteExceptionHandler.SqlExceptionConverter, DbExceptionConverter</property>
        <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
      </session-factory>
    </hibernate-configuration>
    

    对于 Postgres:(注意 sql_exception_converter 属性)

    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
      <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
        <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
        <property name="connection.connection_string">Server=127.0.0.1;Port=5432;Database=DbName;User Id=postgres;Password=postgres;</property>
        <property name="sql_exception_converter">DbExceptionConverter.PostgresExceptionHandler.SqlExceptionConverter, DbExceptionConverter</property>
      </session-factory>
    </hibernate-configuration>
    

    3- 在每个 sql_exception_converter 属性中,您可以看到我使用类的命名空间和类名来告诉程序集使用哪个异常处理程序。

    结果很简单,我希望我可以用我的经验帮助其他人。

    如果有人有任何想法、建议或认为这不是一个好主意,我会很高兴听到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 2018-04-18
      相关资源
      最近更新 更多