Program.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. using Newtonsoft.Json;
  6. namespace FsDb.Test
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. var db = new FsDbEngine(
  13. AppDomain.CurrentDomain.BaseDirectory
  14. .CombineLocalPath("data")
  15. );
  16. var colBook = db.GetCollection<Book>();
  17. var colTag = db.GetCollection<Tag>();
  18. var colBookTag = db.GetCollection<BookTag>();
  19. var book = new Book();
  20. var tag = new Tag();
  21. var booktag = new BookTag();
  22. colBook.Add(book);
  23. colTag.Add(tag);
  24. booktag.Book = book;
  25. booktag.Tag = tag;
  26. colBookTag.Add(booktag);
  27. colBook.Compress();
  28. colTag.Compress();
  29. colBookTag.Compress();
  30. }
  31. }
  32. class Book : FsDbEntityBase
  33. {
  34. public string Name { get; set; }
  35. public string Author { get; set; }
  36. public decimal Price { get; set; }
  37. public float Rank { get; set; }
  38. [JsonIgnore]
  39. public FsDbEntitySet<BookTag> BookTag { private set; get; }
  40. public Book()
  41. {
  42. BookTag = new FsDbEntitySet<BookTag>(
  43. this, p => p.BookID == this.ID
  44. , p => p.BookID = this.ID
  45. );
  46. }
  47. }
  48. class Tag : FsDbEntityBase
  49. {
  50. public int Name { get; set; }
  51. public float Rank { get; set; }
  52. [JsonIgnore]
  53. public FsDbEntitySet<BookTag> BookTag { private set; get; }
  54. public Tag()
  55. {
  56. BookTag = new FsDbEntitySet<BookTag>(
  57. this, p => p.TagID == this.ID
  58. , p => p.TagID = this.ID
  59. );
  60. }
  61. }
  62. class BookTag : FsDbEntityBase
  63. {
  64. public int BookID { get; set; }
  65. public int TagID { get; set; }
  66. public float Rank { get; set; }
  67. private FsDbEntityRef<Book> m_Book;
  68. private FsDbEntityRef<Tag> m_Tag;
  69. public BookTag()
  70. {
  71. m_Book = new FsDbEntityRef<Book>(
  72. this,
  73. p => p.ID == this.BookID
  74. , p => this.BookID = p.ID
  75. );
  76. m_Tag = new FsDbEntityRef<Tag>(
  77. this,
  78. p => p.ID == this.TagID
  79. , p => this.TagID = p.ID
  80. );
  81. }
  82. [JsonIgnore]
  83. public Book Book
  84. {
  85. get { return m_Book.Entity; }
  86. set { m_Book.Entity = value; }
  87. }
  88. [JsonIgnore]
  89. public Tag Tag
  90. {
  91. get { return m_Tag.Entity; }
  92. set { m_Tag.Entity = value; }
  93. }
  94. }
  95. }