2009-05-06 7 views
0

나는 클래스 생성 (테이블을위한 하나의 클래스 - 상속 등은 지금 고려하지 말아야한다 ...)으로 놀고있다. 그래서 나는 here Reflection.Emit 코드에서 뻔뻔스럽게 복사했습니다. 지정된 데이터베이스의 테이블 당 생성되도록 다시 작성하고 프로젝트의 bin 폴더에 다음 일괄 처리 호출로 파일을 생성했습니다. /f "tokens = *"%% i in ('dir * .xsd/b')에 대해 for do "C : ₩ Program Files ₩ Microsoft SDKs ₩ Windows ₩ v6.0A ₩ bin ₩ xsd.exe"-c -l : C# -n : BusinessObjects % iReflection.Emit 및 xsd 파일을 사용하여 데이터베이스 테이블에서 클래스를 생성 할 때 예상되는 함정은 무엇입니까?

지금까지는 그렇게 좋았습니다. 이 아이디어는 새로운 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 

답변

1

데이터베이스 기반 객체 지향 설계 :

  • 충분하지 추상화 여기

    는 콘솔 응용 프로그램 작성의 생성 코드 어셈블리입니다 , 상속 재산 및 ADTs의 건물 없음;
  • 너무 많은 책임이있는 수업;
  • 잘못된 위치에서의 동작입니다.
  • 시간 측면을 처리하는 데 사용할 수있는 방법이 없습니다.

저는 다른 방법으로 작업하는 것을 선호합니다. oo 모델에서 데이터베이스로

[편집] 하이브리드 모델을 작동 시키려고 할 수 있습니다. 앱의 일부에서는 DB에서 OO로 이동하고 다른 부분에서는 다른 방법으로 이동합니다. 그렇게하면 천천히 리팩토링하고 OO -> DB로 마이그레이션 할 수 있습니다. 음속와

+0

... 내 문제가 실제로 DB 아직 내가 그것을 채택해야 존재한다는 것입니다 내가 당신과 동의 .. –

0

뜨아 ... oneliner :

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