TinyFrame升級之二:數據底層訪問部分

在上一篇中,我列舉了框架的總體結構,下面咱們將一一說明:html

首先須要說明的是TinyFrame.Data。數據庫

它主要用於處理數據庫底層操做。包含EF CodeFirst,Repository,Unitofwork三個部分。app

其中,DomainModel主要是用來存放實體類的:框架

   1:  namespace TinyFrame.Data.DomainModel
   2:  {
   3:      public class Book
   4:      {
   5:          public int ID { get; set; }
   6:          public string Name { get; set; }
   7:          public string Author { get; set; }
   8:          public string Publishment { get; set; }
   9:          public int BookTypeID { get; set; }
  10:          public int BookPlaceID { get; set; }
  11:   
  12:          public virtual BookType BookType { get; set; }
  13:          public virtual BookPlace BookPlace { get; set; }
  14:      }
  15:  }

而後,在DomainMapper中,則主要處理數據庫字段屬性和主外鍵映射:ide

   1:  using System.Data.Entity.ModelConfiguration;
   2:  using System.ComponentModel.DataAnnotations.Schema;
   3:  using TinyFrame.Data.DomainModel;
   4:   
   5:  namespace TinyFrame.Data.DomainMapper
   6:  {
   7:      public class BookMapper:EntityTypeConfiguration<Book>
   8:      {
   9:          public BookMapper()
  10:          {
  11:              this.ToTable("Book");
  12:   
  13:              this.HasKey(c => c.ID);
  14:              this.Property(c => c.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  15:              this.Property(c => c.ID).IsRequired();
  16:   
  17:              this.Property(c => c.Name).HasMaxLength(255);
  18:              this.Property(c => c.Name).IsRequired();
  19:   
  20:              this.Property(c => c.Author).HasMaxLength(255);
  21:              this.Property(c => c.Author).IsOptional();
  22:   
  23:              this.Property(c => c.Publishment).HasMaxLength(255);
  24:              this.Property(c => c.Publishment).IsRequired();
  25:   
  26:              this.HasRequired(c => c.BookType).WithMany().HasForeignKey(s => s.BookTypeID).WillCascadeOnDelete(false);
  27:              this.HasRequired(c => c.BookPlace).WithMany().HasForeignKey(s => s.BookPlaceID).WillCascadeOnDelete(false);
  28:   
  29:          }
  30:      }
  31:  }

 

上面的代碼中,ToTable方法適用於生成數據庫新表;HasKey方法則指定主鍵;Property方法則用於設置字段屬性;至於最後兩句則主要用於設定主外鍵映射的,這裏咱們不作過多講解。ui

最後在DataContext中,咱們須要進行配置:this

   1:  using System;
   2:  using System.Data.Entity;
   3:  using System.Data.Entity.Validation;
   4:  using TinyFrame.Data.DomainModel;
   5:  using TinyFrame.Data.DomainMapper;
   6:   
   7:  namespace TinyFrame.Data.DataContext
   8:  {
   9:      public class BookContext : DbContext, IDbContext
  10:      {
  11:          public BookContext()
  12:              : base("BookConnection")
  13:          {
  14:              Configuration.ProxyCreationEnabled = false;
  15:              Configuration.LazyLoadingEnabled = true;
  16:              Database.SetInitializer(new MigrateDatabaseToLatestVersion<BookContext, BookContextMConfig>());
  17:          }
  18:   
  19:          public DbSet<Book> Books { get; set; }
  20:          public DbSet<Student> Students { get; set; }
  21:          public DbSet<BookLend> BookLends { get; set; }
  22:          public DbSet<BookType> BookTypes { get; set; }
  23:          public DbSet<BookPlace> BookPlaces { get; set; }
  24:          public DbSet<Manager> Managers { get; set; }
  25:   
  26:          protected override void OnModelCreating(DbModelBuilder modelBuilder)
  27:          {
  28:              modelBuilder.Configurations.Add(new BookMapper());
  29:              modelBuilder.Configurations.Add(new BookLendMapper());
  30:              modelBuilder.Configurations.Add(new BookTypeMapper());
  31:              modelBuilder.Configurations.Add(new BookPlaceMapper());
  32:              modelBuilder.Configurations.Add(new StudentMapper());
  33:              modelBuilder.Configurations.Add(new ManagerMapper());
  34:              base.OnModelCreating(modelBuilder);
  35:          }
  36:   
  37:          public new IDbSet<T> Set<T>() where T : class
  38:          {
  39:              return base.Set<T>();
  40:          }
  41:      }
  42:  }

這裏說明一下,OnModelCreating主要用於配置數據庫實體類映射,咱們能夠把自定義的配置添加進來,最後經過調用OnModelCreating方法來觸發數據庫表建立事件。編碼

至於Repository模式,則主要提供數據操做集合:spa

   1:  using System;
   2:  using System.Linq;
   3:  using System.Linq.Expressions;
   4:   
   5:  namespace TinyFrame.Data.DataRepository
   6:  {
   7:      public interface IRepository<T> where T:class
   8:      {
   9:          T GetByID(long id);
  10:          T GetByID(int id);
  11:          T GetByID(Guid id);
  12:          T GetByID(string id);
  13:          T Get(Expression<Func<T, bool>> where);
  14:          IQueryable<T> GetMany(Expression<Func<T, bool>> where);
  15:   
  16:          void Insert(T entity);
  17:          void Update(T entity);
  18:          void Delete(T entity);
  19:          void Delete(Expression<Func<T, bool>> where);
  20:      }
  21:  }

其具體的實現以下:code

   1:  using System;
   2:  using System.Linq;
   3:  using System.Data.Entity;
   4:  using System.Linq.Expressions;
   5:  using System.Data.Entity.Validation;
   6:  using TinyFrame.Data.DataContext;
   7:   
   8:  namespace TinyFrame.Data.DataRepository
   9:  {
  10:      public class Repository<T>:IRepository<T> where T:class
  11:      {
  12:          public Repository(IDbContext context)
  13:          {
  14:              this.context = context;
  15:          }
  16:   
  17:          private IDbContext context;
  18:   
  19:          private IDbSet<T> dbset;
  20:          public virtual IDbSet<T> DbSet
  21:          {
  22:              get
  23:              {
  24:                  if (dbset == null)
  25:                      dbset = context.Set<T>();
  26:                  return dbset;
  27:              }
  28:          }
  29:   
  30:          public virtual T GetByID(long id)
  31:          {
  32:              return DbSet.Find(id);
  33:          }
  34:   
  35:          public virtual T GetByID(int id)
  36:          {
  37:              return DbSet.Find(id);
  38:          }
  39:   
  40:          public virtual T GetByID(Guid id)
  41:          {
  42:              return DbSet.Find(id);
  43:          }
  44:   
  45:          public virtual T GetByID(string id)
  46:          {
  47:              return DbSet.Find(id);
  48:          }
  49:   
  50:          public virtual T Get(Expression<Func<T, bool>> where)
  51:          {
  52:              return DbSet.Where(where).FirstOrDefault<T>();
  53:          }
  54:   
  55:          public virtual IQueryable<T> GetMany(Expression<Func<T, bool>> where)
  56:          {
  57:              return DbSet.Where(where);
  58:          }
  59:   
  60:          public virtual void Insert(T entity)
  61:          {
  62:              try
  63:              {
  64:                  if (entity == null)
  65:                      throw new ArgumentException("實體類爲空");
  66:                  DbSet.Add(entity);
  67:                  context.SaveChanges();
  68:              }
  69:              catch (DbEntityValidationException dbex)
  70:              {
  71:                  var msg = string.Empty;
  72:                  foreach(var validationErrors in dbex.EntityValidationErrors)
  73:                      foreach(var validateionError in validationErrors.ValidationErrors)
  74:                          msg+=string.Format("Property:{0} Error:{1}",validateionError.PropertyName,validateionError.ErrorMessage);
  75:   
  76:                  var fail = new Exception(msg,dbex);
  77:                  throw fail;
  78:              }
  79:          }
  80:   
  81:          public virtual void Update(T entity)
  82:          {
  83:              try
  84:              {
  85:                  if (entity == null)
  86:                      throw new ArgumentNullException("實體類爲空");
  87:                  context.SaveChanges();
  88:              }
  89:              catch (DbEntityValidationException dbex)
  90:              {
  91:                  var msg = string.Empty;
  92:                  foreach (var validationErrors in dbex.EntityValidationErrors)
  93:                      foreach (var validateionError in validationErrors.ValidationErrors)
  94:                          msg += string.Format("Property:{0} Error:{1}", validateionError.PropertyName, validateionError.ErrorMessage);
  95:   
  96:                  var fail = new Exception(msg, dbex);
  97:                  throw fail;
  98:              }
  99:          }
 100:   
 101:          public virtual void Delete(T entity)
 102:          {
 103:              try
 104:              {
 105:                  if (entity == null)
 106:                      throw new ArgumentNullException("實體類爲空");
 107:                  DbSet.Remove(entity);
 108:                  context.SaveChanges();
 109:              }
 110:              catch (DbEntityValidationException dbex)
 111:              {
 112:                  var msg = string.Empty;
 113:                  foreach (var validationErrors in dbex.EntityValidationErrors)
 114:                      foreach (var validateionError in validationErrors.ValidationErrors)
 115:                          msg += string.Format("Property:{0} Error:{1}", validateionError.PropertyName, validateionError.ErrorMessage);
 116:   
 117:                  var fail = new Exception(msg, dbex);
 118:                  throw fail;
 119:              }
 120:          }
 121:   
 122:          public virtual void Delete(Expression<Func<T, bool>> where)
 123:          {
 124:              try
 125:              {
 126:                  var entities = DbSet.Where(where);
 127:                  foreach (var entity in entities.ToList())
 128:                      DbSet.Remove(entity);
 129:                  context.SaveChanges();
 130:              }
 131:              catch (DbEntityValidationException dbex)
 132:              {
 133:                  var msg = string.Empty;
 134:                  foreach (var validationErrors in dbex.EntityValidationErrors)
 135:                      foreach (var validateionError in validationErrors.ValidationErrors)
 136:                          msg += string.Format("Property:{0} Error:{1}", validateionError.PropertyName, validateionError.ErrorMessage);
 137:   
 138:                  var fail = new Exception(msg, dbex);
 139:                  throw fail;
 140:              }
 141:          }
 142:      }
 143:  }

因爲在編碼中,咱們使用了泛型對象,因此讓數據庫中有表變更的時候,不會影響到咱們這層。

最後是Unitofwork,這個比較簡單,主要用於事務控制:

   1:  namespace TinyFrame.Data.DataUnitOfWork
   2:  {
   3:      public interface IUnitOfWork
   4:      {
   5:          void Commit();
   6:      }
   7:  }

 

 

一旦有批量的數據CRUD的時候,咱們能夠調用此方法進行批量提交,這樣可以保證CRUD的準確性。

相關文章
相關標籤/搜索