KitchenPC and Ironpython

2019-07-19 15:15发布

问题:

I am attempting to work with KitchenPC using IronPython.

I am working with the Database Provisioning example here: http://blog.kitchenpc.com/2014/02/11/kitchenpc-database-provisioning-101/

I am successfully referencing and importing all the dlls and their namespaces:

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 *

I can define a PostgreSQLConfiguration like so:

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

But when I try to setup the databaseAdapter:

DatabaseAdapter.Configure.DatabaseConfiguration( DBC )

I am running into an error:

TypeError: expected IPersistenceConfigurer, got PostgreSQLConfiguration

The strange thing is that is instance(DBC, IPersistenceConfigurer) returns True, so DBC is indeed an instance of the the abstract class IPersistenceConfigurer which is a subclass of DatabaseAdapter.

Here is a sample of the original code I am attempting to transpose from 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();

Here is a sample of the DatabaseAdapter class from KitchenPC:

/// <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);
  }
}

What am I doing wrong with instantiating DatabaseAdapter in Ironpython?

回答1:

So it turns out, that the way I was importing was incorrect.

But basically, imports should be like so:

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

There is no need to import a DLL if another DLL imports it in the clr.