2009年9月1日火曜日

Monoで他のDBも使ってみたよ!+(db4o、NHibernate編)3


動作確認

基本的なDB操作(登録、更新、削除)を確認するために、次のコード(C#)を使用します。db4o DBはEmbeded Serverモードでオープンします。

 作業する上でのディレクトリ構造は、下表の通りとします。

ディレクトリファイル
~/src/cs/db4oExampleIProductRepository.cs
~/src/cs/db4oExample/Testclient.cs, client.exe.config
~/src/cs/db4oExample/TestDataProductRepository.cs, TestDataHelper.cs
IProductRepositoryインターフェース、ProductRepository、TestDataHelperクラス

[IProductRepository.cs]
  1. using System;  
  2.   
  3. namespace db4oExample  
  4. {  
  5.   public interface IProductRepository : IDisposable  
  6.   {  
  7.     void Add(Product product);  
  8.     void Update(Product product);  
  9.     void Remove(Product product);  
  10.     void GetAll();  
  11.   }  
  12. }  

[ProductRepository.cs]
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using Db4objects.Db4o;  
  5. using Db4objects.Db4o.Linq;  
  6.   
  7. using db4oExample;  
  8.   
  9. namespace db4oExample.TestData  
  10. {  
  11.   public class ProductRepository : IProductRepository  
  12.   {  
  13.     private static IList<Product> _cache;  
  14.   
  15.     public void Add(Product product)  
  16.     {  
  17.       using (IObjectContainer client = TestDataHelper.OpenClient())  
  18.       {  
  19.         Product r1 = _getByIdFromCache(product.Id);  
  20.         if (r1 != null)  
  21.         {  
  22.           Console.WriteLine("Add:既にデータあり #1");  
  23.           return;  
  24.         }  
  25.   
  26.         Product r2 = _getById(client, product.Id);  
  27.         if (r2 != null)  
  28.         {  
  29.           Console.WriteLine("Add:既にデータあり #2");  
  30.           return;  
  31.         }  
  32.   
  33.         try  
  34.         {  
  35.           client.Store(product);  
  36.           client.Commit();  
  37.         }  
  38.         catch(Exception ex)  
  39.         {  
  40.           Console.WriteLine("例外: {0}", ex.Message);  
  41.           client.Rollback();  
  42.         }  
  43.       }  
  44.     }  
  45.   
  46.     public void Update(Product product)  
  47.     {  
  48.       using (IObjectContainer client = TestDataHelper.OpenClient())  
  49.       {  
  50.         Product r1 = _getByIdFromCache(product.Id);  
  51.         if (r1 == null)  
  52.         {  
  53.           Console.WriteLine("Update:該当データなし #1");  
  54.           return;  
  55.         }        
  56.   
  57.         Product r2 = _getByProduct(client, r1);  
  58.         if (r2 == null)  
  59.         {  
  60.           Console.WriteLine("Update:該当データなし #2");  
  61.           return;  
  62.         }  
  63.   
  64.         r2.Name        = product.Name;  
  65.         r2.Price       = product.Price;  
  66.         r2.Description = product.Description;  
  67.   
  68.         try  
  69.         {  
  70.           client.Store(r2);  
  71.           client.Commit();  
  72.         }  
  73.         catch(Exception ex)  
  74.         {  
  75.           Console.WriteLine("例外: {0}", ex.Message);  
  76.           client.Rollback();  
  77.         }  
  78.       }  
  79.     }  
  80.   
  81.     public void Remove(Product product)  
  82.     {  
  83.       using (IObjectContainer client = TestDataHelper.OpenClient())  
  84.       {  
  85.         Product r1 = _getByIdFromCache(product.Id);  
  86.         if (r1 == null)  
  87.         {  
  88.           Console.WriteLine("Remove:該当データなし #1");  
  89.           return;  
  90.         }  
  91.   
  92.         Product r2 = _getByProduct(client, r1);  
  93.         if (r2 == null)  
  94.         {  
  95.           Console.WriteLine("Remove:該当データなし #2");  
  96.           return;  
  97.         }  
  98.   
  99.         try  
  100.         {  
  101.           client.Delete(r2);  
  102.           client.Commit();  
  103.         }  
  104.         catch(Exception ex)  
  105.         {  
  106.           Console.WriteLine("例外: {0}", ex.Message);  
  107.           client.Rollback();  
  108.         }  
  109.       }  
  110.     }  
  111.   
  112.     public void GetAll()  
  113.     {  
  114.       using (IObjectContainer client = TestDataHelper.OpenClient())  
  115.       {  
  116.         _cache = (from Product p in client  
  117.           orderby p.Id  
  118.           select p).ToList();  
  119.   
  120.         foreach (Product p in _cache)  
  121.         {  
  122.           Console.WriteLine(p);  
  123.         }  
  124.       }  
  125.     }  
  126.   
  127.     public void Dispose()  
  128.     {  
  129.       TestDataHelper.CloseServer();  
  130.     }  
  131.   
  132.     private Product _getById(IObjectContainer client, int id)  
  133.     {  
  134.       return (from Product p in client  
  135.         where p.Id == id  
  136.         select p).SingleOrDefault();  
  137.     }  
  138.   
  139.     private Product _getByIdFromCache(int id)  
  140.     {  
  141.       return (from Product p in _cache  
  142.         where p.Id == id  
  143.         select p).SingleOrDefault();  
  144.     }  
  145.   
  146.     private Product _getByProduct(IObjectContainer client, Product product)  
  147.     {  
  148.       return (from Product p in client  
  149.         where p.Id == product.Id &&  
  150.           p.Name == product.Name &&  
  151.           p.Price == product.Price &&  
  152.           p.Description == product.Description  
  153.         select p).SingleOrDefault();  
  154.     }  
  155.   }  
  156. }  

[TestDataHelper.cs]
  1. using System;  
  2. using System.Configuration;  
  3. using Db4objects.Db4o;  
  4.   
  5. namespace db4oExample.TestData  
  6. {  
  7.   public class TestDataHelper  
  8.   {  
  9.     private static IObjectServer _server;  
  10.   
  11.     private static IObjectServer Server  
  12.     {  
  13.       get  
  14.       {  
  15.         if (_server == null)  
  16.         {  
  17.           string dbfile = ConfigurationManager.AppSettings["dbfile"];  
  18.           _server = Db4oFactory.OpenServer(dbfile, 0);  
  19.         }  
  20.         return _server;  
  21.       }  
  22.     }  
  23.   
  24.     public static IObjectContainer OpenClient()  
  25.     {  
  26.       return Server.OpenClient();  
  27.     }  
  28.   
  29.     public static void CloseServer()  
  30.     {  
  31.       if (Server != null)  
  32.       {  
  33.         Server.Close();  
  34.       }  
  35.     }  
  36.   }  
  37. }  
ライブラリとしてビルドします。

[ビルド実行例]
$ cd ~/src/cs/db4oExample
$ gmcs -t:library -r:System.Configuration.dll,Db4objects.Db4o.dll,Db4objects.Db4o.Linq.dll -out:./Test/db4oExample.dll Product.cs IProductRepository.cs -recurse:./TestData/*.cs
動作確認用プログラム
ProductRepositoryインスタンスを使用したDB操作を行います。

[client.cs]
  1. using System;  
  2.   
  3. using db4oExample;  
  4. using db4oExample.TestData;  
  5.   
  6. namespace db4oExample.Test  
  7. {  
  8.   public class Client  
  9.   {  
  10.     public static void Main(string[] args)  
  11.     {  
  12.       using (IProductRepository products = new ProductRepository())  
  13.       {  
  14.         products.GetAll();  
  15.   
  16.         string s = "";  
  17.         while(s != "5")  
  18.         {  
  19.           Console.WriteLine();  
  20.           Console.Write("1:ADD\n2:UPDATE\n3:REMOVE\n4:RELOAD\n5:EXIT\n?> ");  
  21.           s = Console.ReadLine();  
  22.           switch(s)  
  23.           {  
  24.             case "1": AddProduct(products);    goto case "4"// 追加  
  25.             case "2": UpdateProduct(products); goto case "4"// 更新  
  26.             case "3": RemoveProduct(products); goto case "4"// 削除  
  27.             case "4": products.GetAll();       goto default// 再読込  
  28.             case "5"goto default// 終了  
  29.             default : break;  
  30.           }  
  31.         }  
  32.       }  
  33.     }  
  34.   
  35.     private static void AddProduct(IProductRepository products)  
  36.     {  
  37.       try  
  38.       {  
  39.         products.Add(CreateProduct());  
  40.       }  
  41.       catch(Exception ex)  
  42.       {  
  43.         Console.WriteLine("例外: {0}", ex.Message);  
  44.       }  
  45.     }  
  46.   
  47.     private static void UpdateProduct(IProductRepository products)  
  48.     {  
  49.       try  
  50.       {  
  51.         products.Update(CreateProduct());  
  52.       }  
  53.       catch(Exception ex)  
  54.       {  
  55.         Console.WriteLine("例外: {0}", ex.Message);  
  56.       }  
  57.     }  
  58.   
  59.     private static void RemoveProduct(IProductRepository products)  
  60.     {  
  61.       Console.Write("ID(int)?> ");  
  62.       string id = Console.ReadLine();  
  63.   
  64.       try  
  65.       {  
  66.         Product product = new Product();  
  67.         product.Id = Int32.Parse(id);  
  68.         products.Remove(product);  
  69.       }  
  70.       catch(Exception ex)  
  71.       {  
  72.         Console.WriteLine("例外: {0}", ex.Message);  
  73.       }  
  74.     }  
  75.   
  76.     private static Product CreateProduct()  
  77.     {  
  78.       Console.Write("ID(int)?> ");  
  79.       string id = Console.ReadLine();  
  80.       Console.Write("NAME(string)?> ");  
  81.       string name = Console.ReadLine();  
  82.       Console.Write("PRICE(num)?> ");  
  83.       string price = Console.ReadLine();  
  84.       Console.Write("DESCRIPTION(string)?> ");  
  85.       string description = Console.ReadLine();  
  86.   
  87.       Product product = new Product();  
  88.       product.Id   = Int32.Parse(id);  
  89.       product.Name = name;  
  90.   
  91.       if (price != "")  
  92.       {  
  93.         product.Price = Decimal.Parse(price);  
  94.       }  
  95.   
  96.       product.Description = description;  
  97.   
  98.       return product;  
  99.     }  
  100.   }  
  101. }  
  102. /* 
  103. * ビルド: 
  104. * 
  105. *   gmcs -r:db4oExample.dll client.cs 
  106. * 
  107. * 実行: 
  108. * 
  109. *   mono client.exe 
  110. * 
  111. */  

[client.exe.config]
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <appSettings>  
  4.     <add key="dbfile" value="/home/sta/data/db4o/TestData.db4o" />  
  5.   </appSettings>  
  6. </configuration>  

[実行例]
$ cd ~/src/cs/db4oExample/Test
$ mono client.exe
ID:0 NAME:db4o PRICE: DESCRIPTION:db4o 7.4
ID:1 NAME:Clamp PRICE:12.48 DESCRIPTION:Workbench clamp
ID:50 NAME:Flat Head Screwdriver PRICE:3.17 DESCRIPTION:Flat head
ID:75 NAME:Tire Bar PRICE: DESCRIPTION:Tool for changing tires.
ID:3000 NAME:3mm Bracket PRICE:0.52 DESCRIPTION:

1:ADD
2:UPDATE
3:REMOVE
4:RELOAD
5:EXIT
?> 1
ID(int)?> 2
NAME(string)?> test2
PRICE(num)?> 2.2
DESCRIPTION(string)?> for test2
ID:0 NAME:db4o PRICE: DESCRIPTION:db4o 7.4
ID:1 NAME:Clamp PRICE:12.48 DESCRIPTION:Workbench clamp
ID:2 NAME:test2 PRICE:2.2 DESCRIPTION:for test2
ID:50 NAME:Flat Head Screwdriver PRICE:3.17 DESCRIPTION:Flat head
ID:75 NAME:Tire Bar PRICE: DESCRIPTION:Tool for changing tires.
ID:3000 NAME:3mm Bracket PRICE:0.52 DESCRIPTION:

1:ADD
2:UPDATE
3:REMOVE
4:RELOAD
5:EXIT
?> 2
ID(int)?> 2
NAME(string)?> Test2
PRICE(num)?> 2.22
DESCRIPTION(string)?> テスト2
ID:0 NAME:db4o PRICE: DESCRIPTION:db4o 7.4
ID:1 NAME:Clamp PRICE:12.48 DESCRIPTION:Workbench clamp
ID:2 NAME:Test2 PRICE:2.22 DESCRIPTION:テスト2
ID:50 NAME:Flat Head Screwdriver PRICE:3.17 DESCRIPTION:Flat head
ID:75 NAME:Tire Bar PRICE: DESCRIPTION:Tool for changing tires.
ID:3000 NAME:3mm Bracket PRICE:0.52 DESCRIPTION:

1:ADD
2:UPDATE
3:REMOVE
4:RELOAD
5:EXIT
?> 3
ID(int)?> 2
ID:0 NAME:db4o PRICE: DESCRIPTION:db4o 7.4
ID:1 NAME:Clamp PRICE:12.48 DESCRIPTION:Workbench clamp
ID:50 NAME:Flat Head Screwdriver PRICE:3.17 DESCRIPTION:Flat head
ID:75 NAME:Tire Bar PRICE: DESCRIPTION:Tool for changing tires.
ID:3000 NAME:3mm Bracket PRICE:0.52 DESCRIPTION:

1:ADD
2:UPDATE
3:REMOVE
4:RELOAD
5:EXIT
?> 5

 Monoで他のDBも使ってみたよ!+(db4o、NHibernate編)4 へ続く。

0 件のコメント:

コメントを投稿