2009-11-14 143 views
0

在vb .net winforms應用程序中,我嘗試了某些記錄的「寬和淺」子元素。我使用強類型的業務對象(Strataframe)。將數據集序列化爲強類型業務對象屬性

我的計劃是在表單上放置一個數據集並收集大量「子表」。他們在持久數據中沒有對應關係,所以數據集是無類型的,我通過表格的屬性表創建模式。每個表格都是界面中datagridview的數據源。

在我的概念證明示例中,我的主要業務對象(CustomerBO)與SQL Server 2008表中的字段(pk,name,)進行交互,第三列當前爲varchar(max),因爲我認爲XML可能只是很容易變成varbinary(max),如果這對保存序列化數據集更好。

bo.bigfield將是強類型的道具我想持有byte()數組或XML或任何表示該記錄的數據集的內容。

所以,問題簡而言之 - 我如何將數據集轉換爲單個數據並從該數據中重現數據集。這是我第一次進入數據集,datagridviews,所以如果有更好的方法來完成我正在聽的任何。

代碼和建議非常讚賞 - VB或C#

回答

0

有許多的方式來大卸八塊。你在談論的是對象關係映射。有許多的工具/框架在那裏,這裏是我的最愛:

的快速應用開發,亞音速是我最喜歡的手下來。使用最新的3.x版本,你所要做的就是創建一個連接字符串,調用SubSonic持久層,並傳入你的對象。它將創建DDL,用於將對象保存在數據庫中,而不必非常非常容易地使用這些東西。以下是SubSonic的一個示例:

public class BaseDAO 
    { 
     private const String CONNECTION_STRING = "Repository"; 
     private static BaseDAO dao = new BaseDAO(); 
     private SimpleRepository repository; 

     protected BaseDAO() 
     { 
      if (repository == null) 
      { 
       repository = new SimpleRepository(CONNECTION_STRING, SimpleRepositoryOptions.RunMigrations); 
      } 
     } 

     public BaseDAO Instance() 
     { 
      return dao; 
     } 

     public SubSonic.Repository.SimpleRepository Repository 
     { 
      get { return this.repository; } 
      set { this.repository = value; } 
     } 
    } 

public class ProductDAO : BaseDAO 
{ 
    public void save(ProductVO product) 
    { 
     if (product != null) 
     { 
      if (product.ID == null) 
      { 
       product.ID = Guid.NewGuid(); 
      } 

      this.Repository.Add<ProductVO>(product); 
     } 
     else 
     { 
      throw new ArgumentException("The product passed in was null!"); 
     } 
    } 

    public void update(ProductVO product) 
    { 
     if (product != null) 
     { 
      this.Repository.Update<ProductVO>(product); 
     } 
     else 
     { 
      throw new ArgumentException("The product passed in was null!"); 
     } 
    } 

    public List<ProductVO> fetchAll() 
    { 
     return fetchAll(null); 
    } 

    public List<ProductVO> fetchAll(String name) 
    { 
     IEnumerable<ProductVO> list = this.Repository.All<ProductVO>(); 

     if (name != null && name.Length > 0) 
     { 
      var output = 
       from products in list 
       where products.Name.ToLower().Contains(name.Trim().ToLower()) 
       select products; 

      list = output.ToList(); 
     } 

     return list.OrderBy(product => product.Name).ToList(); 
    } 

    public ProductVO fetchByID(object ID) 
    { 
     return this.Repository.Single<ProductVO>(ID); 
    } 

    public void delete(ProductVO product) 
    { 
     this.Repository.Delete<ProductVO>(product.ID); 
    } 
} 

就是這麼簡單。

相關問題