KitchenPC 和 Ironpython

KitchenPC and Ironpython

我正在尝试使用 IronPython 与 KitchenPC 一起工作。

我正在使用此处的数据库配置示例:http://blog.kitchenpc.com/2014/02/11/kitchenpc-database-provisioning-101/

我成功引用并导入了所有 dll 及其命名空间:

import clr
clr.AddReference("System")

from System import *
from System.Reflection import *
from System.Reflection import Assembly

L4N = Assembly.LoadFrom('C://KitchenPC//DLL//log4net.dll')  
clr.AddReference(L4N)

NU= Assembly.LoadFrom('C://KitchenPC//DLL//nunit.framework.dll')  
clr.AddReference(NU)

LC= Assembly.LoadFrom('C://KitchenPC//DLL//Iesi.Collections.dll')  
clr.AddReference(LC)

PGSQL = Assembly.LoadFrom('C://KitchenPC//DLL//Npgsql.dll')  
clr.AddReference(PGSQL)

NH = Assembly.LoadFrom('C://KitchenPC//DLL//NHibernate.dll') 
clr.AddReference(NH)

FNH = Assembly.LoadFrom('C://KitchenPC//DLL//FluentNHibernate.dll')  
clr.AddReference(FNH)

KPC = Assembly.LoadFrom('C://KitchenPC//DLL//KitchenPC.dll')  
clr.AddReference(KPC)

KPCDB = Assembly.LoadFrom('C://KitchenPC//DLL//KitchenPC.DB.dll')  
clr.AddReference(KPCDB)

clr.AddReferenceToFileAndPath('C://KitchenPC//DLL//FluentNHibernate.dll')

from Npgsql import *
from log4net import *
from Iesi.Collections import *

from FluentNHibernate import *
from FluentNHibernate.Cfg import *
from FluentNHibernate.Cfg.Db import *

from NHibernate import *
from NHibernate.Cfg import Configuration

from KitchenPC import *
from KitchenPC.DB import *
from KitchenPC.Context import DBContext
from KitchenPC.DB import DatabaseAdapter

from NUnit.Framework import *

我可以这样定义 PostgreSQLConfiguration

connString = "Server=localhost;Port=5432;User Id=postgres;Password=password;Database=KPCSample"
# Context connected to local database
DBC = PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connString).ShowSql()

但是当我尝试设置数据库适配器时:

DatabaseAdapter.Configure.DatabaseConfiguration( DBC )

我遇到了错误:

TypeError: expected IPersistenceConfigurer, got PostgreSQLConfiguration

奇怪的是instance(DBC,IPersistenceConfigurer)returnsTrue,所以DBC确实是抽象的实例class IPersistenceConfigurerDatabaseAdapter.

的子class

这是我尝试从 C# 转置的原始代码示例:

// Context connected to local database
var dbConfig = Configuration.Build
   .Context(DBContext.Configure
      .Adapter(DatabaseAdapter.Configure
         .DatabaseConfiguration(
            PostgreSQLConfiguration.PostgreSQL82
               .ConnectionString(@"Server=localhost;Port=5432;User Id=Website;Password=password;Database=KPCSample")
               .ShowSql()
         )
      )
   ).Create();

// Context connected to local data store
var staticConfig = Configuration.Build
   .Context(StaticContext.Configure
      .DataDirectory(@"C:\KitchenPC\ConsoleTest\LocalStore\")
   )
   .Create();

这是来自 KitchenPC 的 DatabaseAdapter class 示例:

/// <summary>A database adapter that uses NHibernate to connect to an underlying database.</summary>
public class DatabaseAdapter : IDBAdapter, IDisposable
{
  ISessionFactory sessionFactory;
  Configuration nhConfig;
  readonly DatabaseAdapterBuilder builder;

  public IPersistenceConfigurer DatabaseConfiguration { get; set; }
  public List<IConvention> DatabaseConventions { get; set; }
  public ISearchProvider SearchProvider { get; set; }

  public static DatabaseAdapterBuilder Configure
  {
     get
     {
        return new DatabaseAdapter().builder;
     }
  }

  DatabaseAdapter()
  {
     builder = new DatabaseAdapterBuilder(this);
  }
}

在 Ironpython 中实例化 DatabaseAdapter 我做错了什么?

事实证明,我导入的方式不正确。

但基本上,导入应该是这样的:

import clr
import sys
sys.path.append(os.path.abspath('./DLL')) #where your dlls are
clr.AddReference('System')
clr.AddReference('FluentNHibernate')
from FluentNHibernate.Cfg.Db import PostgreSQLConfiguration

如果clr中有其他DLL导入,则无需导入该DLL