C# 使用Reflection.Emit和xsd文件从数据库表生成类时会遇到什么陷阱?

C# 使用Reflection.Emit和xsd文件从数据库表生成类时会遇到什么陷阱?,c#,code-generation,business-objects,reflection.emit,C#,Code Generation,Business Objects,Reflection.emit,我正在玩类生成(一个表一个类-继承等,现在不考虑…)。所以我无耻地复制了反射。发射代码。对其进行了重新处理,以便在给定数据库中按表生成,并在项目的bin文件夹中使用以下批处理调用创建了文件: 对于('dir*.xsd/b')中的/f“tokens=*”%%i,请执行“C:\Program Files\Microsoft SDK\Windows\v6.0A\bin\xsd.exe”-C-l:C#-n:BusinessObjects%i 到目前为止还不错。这个想法是每当新的db版本到来时,重新生成类

我正在玩类生成(一个表一个类-继承等,现在不考虑…)。所以我无耻地复制了反射。发射代码。对其进行了重新处理,以便在给定数据库中按表生成,并在项目的bin文件夹中使用以下批处理调用创建了文件: 对于('dir*.xsd/b')中的/f“tokens=*”%%i,请执行“C:\Program Files\Microsoft SDK\Windows\v6.0A\bin\xsd.exe”-C-l:C#-n:BusinessObjects%i

到目前为止还不错。这个想法是每当新的db版本到来时,重新生成类并将它们复制到“真正的项目”(我不需要任何运行时生成)中,并且希望享受Intellisense。这种方法可能会带来什么陷阱、困难和问题,对于那些描述松散的需求有什么更好的建议吗

以下是创建程序集的控制台应用程序的生成代码:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using log4net;
    using log4net.Config;
    using System.Data;
    using System.Data.SqlClient;
    using System.Threading;
    using System.Reflection;
    using System.Reflection.Emit;

    namespace GenerateAssemblies
    {

      class Program
      {

        private static readonly ILog logger =
             LogManager.GetLogger ( typeof ( Program ) );


        static void Main ( string[] args )
        {
          DOMConfigurator.Configure();  //tis configures the logger 
          logger.Debug ( "APP START" );

          DataTable dtTables = Program.GetTablesFromDb ( "POC" ) ;
          foreach (DataRow dr in dtTables.Rows)
          {
            string strTableName = dr[0].ToString () ;
            CodeEmitGeneratingAssemblies.DllGenerator.WriteXmlAndTxtFileOutOfDataTableByName (  strTableName);
            CodeEmitGeneratingAssemblies.DllGenerator.CreateAssembly ( strTableName );
          }


          Console.WriteLine ( " Should have now all the dll's " );
          Console.ReadLine ();
        } //eof method 



        static DataTable GetTablesFromDb ( string strDbName )
        {


          DataTable dt = new DataTable ( "tables" );

          string connectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" + strDbName + ";Data Source=ysg";

          using (SqlConnection connection = new SqlConnection ( connectionString ))
          {
            SqlCommand command = connection.CreateCommand ();

            command.CommandText = string.Format ( "SELECT name from sys.tables" );

            connection.Open ();
            dt.Load ( command.ExecuteReader ( CommandBehavior.CloseConnection ) );
          }
          return dt;
        } //eof method 


      } //eof class 


    namespace CodeEmitGeneratingAssemblies
    {
      public class DllGenerator
      {
        private static readonly ILog logger =
             LogManager.GetLogger ( typeof ( DllGenerator ) );




        public static void WriteXmlAndTxtFileOutOfDataTableByName (string strDataTableName)
        {
          DOMConfigurator.Configure ();  //tis configures the logger 
          DataTable tableData = new DataTable ( strDataTableName );

          string connectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=POC;Data Source=ysg";

          using (SqlConnection connection = new SqlConnection ( connectionString ))
          {
            SqlCommand command = connection.CreateCommand ();

            command.CommandText = string.Format ( "SELECT * FROM [" + strDataTableName + "]");
            logger.Debug ( "command.CommandText is " + command.CommandText );
            connection.Open ();
            tableData.Load ( command.ExecuteReader ( CommandBehavior.CloseConnection ) );
          }

          tableData.WriteXml ( strDataTableName + ".xml" );
          tableData.WriteXmlSchema ( strDataTableName + ".xsd" );
        } //eof method 


        public static void CreateAssembly ( string strDataTableName )
        {
          AppDomain currentDomain = Thread.GetDomain ();

          AssemblyName myAssemblyName = new AssemblyName ( );
          myAssemblyName.Name = strDataTableName;

          AssemblyBuilder builder = currentDomain.DefineDynamicAssembly (
                              myAssemblyName,
                              AssemblyBuilderAccess.RunAndSave );

          builder.AddResourceFile ( "TableXml", strDataTableName + ".xml" );
          builder.AddResourceFile ( "TableXsd", strDataTableName + ".xsd" );

          builder.Save ( strDataTableName + ".dll" );
        }

      } //eof class 
    } //eof namespace 

    } //eof namespace 

使用(关系)数据库驱动的oo设计会遇到所有问题:

  • 没有足够的抽象,没有继承,没有ADT的构建
  • 责任太多的班级
  • 在错误的地方的行为
  • 没有处理时间方面的可用方法
我更喜欢用另一种方式工作。从oo模型到数据库

[编辑]
你可以试着让混合模型工作。应用程序的一部分从DB转到OO,另一部分从DB转到OO。这允许您缓慢地重构并迁移到OO->DB

Duh。。。带亚音速的单线飞机:

sonic generate /override /server HOMIE /db BlogEngine /userid OMMITED /password OMMITED /out C:\code\out /generatedNamespace BlogEngine.Core.Providers.SubSonic /stripTableText be_

我同意你。。。我的问题是,实际上db已经存在,所以我不得不接受它。。