如果你需要一个简单的磁盘文件索引数据库,这篇文章可以帮助你。
文件数据库描述:
- 每个文档对象保存为一个独立文件,例如一篇博客。
- 文件内容序列化支持XML或JSON。
- 支持基本的CRUD操作。
文件数据库抽象类实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
XML文件数据库实现
1 /// <summary> 2 /// XML文件数据库 3 /// </summary> 4 public class XmlDatabase : FileDatabase 5 { 6 /// <summary> 7 /// XML文件数据库 8 /// </summary> 9 /// <param name="directory">数据库文件所在目录</param> 10 public XmlDatabase(string directory) 11 : base(directory) 12 { 13 FileExtension = @"xml"; 14 } 15 16 /// <summary> 17 /// 将指定的文档对象序列化至字符串 18 /// </summary> 19 /// <param name="value">指定的文档对象</param> 20 /// <returns> 21 /// 文档对象序列化后的字符串 22 /// </returns> 23 protected override string Serialize(object value) 24 { 25 if (value == null) 26 throw new ArgumentNullException("value"); 27 28 using (StringWriterWithEncoding sw = new StringWriterWithEncoding(Encoding.UTF8)) 29 { 30 XmlSerializer serializer = new XmlSerializer(value.GetType()); 31 serializer.Serialize(sw, value); 32 return sw.ToString(); 33 } 34 } 35 36 /// <summary> 37 /// 将字符串反序列化成文档对象 38 /// </summary> 39 /// <typeparam name="TDocument">文档类型</typeparam> 40 /// <param name="data">字符串</param> 41 /// <returns> 42 /// 文档对象 43 /// </returns> 44 protected override TDocument Deserialize<TDocument>(string data) 45 { 46 if (string.IsNullOrEmpty(data)) 47 throw new ArgumentNullException("data"); 48 49 using (StringReader sr = new StringReader(data)) 50 { 51 XmlSerializer serializer = new XmlSerializer(typeof(TDocument)); 52 return (TDocument)serializer.Deserialize(sr); 53 } 54 } 55 }
JSON文件数据库实现
1 /// <summary> 2 /// JSON文件数据库 3 /// </summary> 4 public class JsonDatabase : FileDatabase 5 { 6 /// <summary> 7 /// JSON文件数据库 8 /// </summary> 9 /// <param name="directory">数据库文件所在目录</param> 10 public JsonDatabase(string directory) 11 : base(directory) 12 { 13 FileExtension = @"json"; 14 } 15 16 /// <summary> 17 /// 将指定的文档对象序列化至字符串 18 /// </summary> 19 /// <param name="value">指定的文档对象</param> 20 /// <returns> 21 /// 文档对象序列化后的字符串 22 /// </returns> 23 protected override string Serialize(object value) 24 { 25 if (value == null) 26 throw new ArgumentNullException("value"); 27 28 return JsonConvert.SerializeObject(value, OutputIndent); 29 } 30 31 /// <summary> 32 /// 将字符串反序列化成文档对象 33 /// </summary> 34 /// <typeparam name="TDocument">文档类型</typeparam> 35 /// <param name="data">字符串</param> 36 /// <returns> 37 /// 文档对象 38 /// </returns> 39 protected override TDocument Deserialize<TDocument>(string data) 40 { 41 if (string.IsNullOrEmpty(data)) 42 throw new ArgumentNullException("data"); 43 44 return JsonConvert.DeserializeObject<TDocument>(data); 45 } 46 }
Test Double
1 [Serializable] 2 public class Cat 3 { 4 public Cat() 5 { 6 Id = ObjectId.NewObjectId().ToString(); 7 } 8 9 public Cat(string id) 10 { 11 Id = id; 12 } 13 14 public string Name { get; set; } 15 public int Legs { get; set; } 16 17 public string Id { get; set; } 18 19 public override string ToString() 20 { 21 return string.Format("DocumentId={0}, Name={1}, Legs={2}", Id, Name, Legs); 22 } 23 }
使用举例
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 TestJsonDatabase(); 6 TestXmlDatabase(); 7 8 Console.ReadKey(); 9 } 10 11 private static void TestJsonDatabase() 12 { 13 JsonDatabase db = new JsonDatabase(@"C:\tmp"); 14 db.OutputIndent = true; 15 16 Cat origin = new Cat() { Name = "Garfield", Legs = 4 }; 17 db.Save<Cat>(origin); 18 19 db.Save<Cat>(origin.Id, origin); 20 db.Delete<Cat>(origin.Id); 21 } 22 23 private static void TestXmlDatabase() 24 { 25 XmlDatabase db = new XmlDatabase(@"C:\tmp"); 26 db.OutputIndent = true; 27 28 Cat origin = new Cat() { Name = "Garfield", Legs = 4 }; 29 db.Save<Cat>(origin); 30 31 db.Save<Cat>(origin.Id, origin); 32 db.Delete<Cat>(origin.Id); 33 } 34 }
本文转自匠心十年博客园博客,原文链接:http://www.cnblogs.com/gaochundong/archive/2013/04/24/csharp_file_database.html,如需转载请自行联系原作者