以ä¸ã¡ã¢æ¸ãã§ãã
db4o(http://www.db4o.com/default.aspx)ãããªããé¢ç½ãããªã®ã§ã¡ãã£ã¨è§¦ã£ã¦ã¿ã¾ããã
確ãã«ã¡ããã£ã¨ä½¿ãåã«ã¯ç°¡åã§ãããçµã¿è¾¼ã¿ã®ãã¼ã¿ãã¼ã¹ã«ã¯ãã£ã¦ããã§ãã
以ä¸ã¯select,insert,update,deleteããã£ã¦ã¿ããµã³ãã«ã§ãã
ãµã³ãã«ãä½æããåã«ãä¸å¿Utilç³»ã¯ã©ã¹ãä½æãã¦ã¿ã¾ããã
db4oã§ã¯ããã¼ã¿ãã¼ã¹ãéãéã«ä»¥ä¸ã®ããã«é常ãã¾ãã
IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName); try { // do something with db4o } finally { db.Close(); }
ã§ããusingã使ãããã®ã§ä»¥ä¸ã®ãããªãããã©ããã¼ãä½æãã¾ããã
// vim:set ts=4 sw=4 et ws is nowrap ft=cs: using System; using Db4objects.Db4o; namespace Gsf.Samples.Tmp{ public class DisposableObjectContainerWrapper : IDisposable{ IObjectContainer _container; public DisposableObjectContainerWrapper(IObjectContainer container){ _container = container; } public IObjectContainer Container{ get{ return _container; } } public void Dispose(){ _container.Close(); } } }
ããã§ã以ä¸ã®ããã«å®è¡ã§ãã¾ãã
using(DisposableObjectContainerWrapper wrapper = new DisposableObjectContainerWrapper(Db4oFactory.OpenFile(DB_FILE_NAME))){ }
ãã§ã以ä¸ãä»å使ç¨ããã¢ãã«ãªãã¸ã§ã¯ãã§ãã
// vim:set ts=4 sw=4 et ws is nowrap ft=cs: using System; namespace Gsf.Samples.Tmp.Models{ public class Person{ string _id; string _name; public Person() : this(null, null){ // nop; } public Person(string id, string name){ _id = id; _name = name; } public string Id{ get{ return _id; } set{ _id = value; } } public string Name{ get{ return _name; } set{ _name = value; } } public override string ToString(){ return string.Format("Id:{0}, Name:{1}", Id, Name); } } }
ãã§ãå®éã®ãµã³ãã«ã§ãã
// vim:set ts=4 sw=4 et ws is nowrap ft=cs: using System; using Db4objects.Db4o; using Gsf.Samples.Tmp.Models; namespace Gsf.Samples.Tmp{ public class SimpleCRUDSample : IExecutor{ const string DB_FILE_NAME = "mydb.db"; public void Execute(){ // // ãã¼ã¿ãã¼ã¹ãªã¼ãã³. // using(DisposableObjectContainerWrapper wrapper = new DisposableObjectContainerWrapper(Db4oFactory.OpenFile(DB_FILE_NAME))){ IObjectContainer db = wrapper.Container; // // select and delete. // foreach(object o in db.Get(typeof(Person))){ db.Delete(o); } db.Commit(); // // select. // Console.WriteLine("============ DELETEå¾ ================"); Console.WriteLine(db.Get(typeof(Person)).Size()); // // insert. // db.Set(new Person("1", "gsf_zero1")); db.Commit(); // // select. // Console.WriteLine("============ INSERTå¾ ================"); Console.WriteLine(db.Get(typeof(Person)).Size()); Console.WriteLine(db.Get(new Person("1", null)).Next()); // // update. // Person aPerson = db.Get(new Person("1", null)).Next() as Person; aPerson.Name = "gsf_zero2"; db.Set(aPerson); db.Commit(); // // select. // Console.WriteLine("============ UPDATEå¾ ================"); Console.WriteLine(db.Get(typeof(Person)).Size()); Console.WriteLine(db.Get(new Person("1", null)).Next()); } } } }