0

我正在玩类生成(表的一个类 - 继承等。暂时不考虑......)。所以我从这里无耻地复制了 Reflection.Emit 代码。将其重新设计为在给定数据库中的每个表中生成,并在项目的 bin 文件夹中使用以下批处理调用创建文件: for /f "tokens=*" %%i in ('dir *.xsd /b') do " C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\xsd.exe" -c -l:c# -n:BusinessObjects %i

到现在为止还挺好。这个想法是每次新的数据库版本到来时重新生成类并将它们复制到“真实项目”中(我不需要任何运行时生成)并且也想享受 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 
4

2 回答 2

1

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

  • 没有足够的抽象,没有继承,也没有构建 ADT;
  • 职责太多的类;
  • 在错误的地方行为;
  • 没有可用的方法来处理时间方面。

我更喜欢反过来工作。从oo模型到数据库

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

于 2009-05-06T19:38:19.007 回答
0

Duh ... 带有 SubSonic 的 oneliner:

sonic generate /override /server HOMIE /db BlogEngine /userid OMMITED /password OMMITED /out C:\code\out /generatedNamespace BlogEngine.Core.Providers.SubSonic /stripTableText be_
于 2009-05-28T05:58:28.180 回答