TinyFrame升级之三:逻辑访问部分

时间:2023-11-12 10:02:26

在上一篇,我们打造了自己的数据访问部分,这篇,我们准备讲解如何打造逻辑访问部分。

在上一篇中,我们利用Repository模式构建了基于泛型的操作合集。由于这些操作的合集都是原子性的操作,也就是针对单表的操作,我们有必要为每个单表做增删改查操作,所以这里我们最好将泛型包装一下:

这里是IBook接口部分,它继承自IRepository接口,并承载了Book实体类

   1:  using TinyFrame.Data.DataRepository;
   2:  using TinyFrame.Data.DomainModel;
   3:   
   4:  namespace TinyFrame.Repository
   5:  {
   6:      public interface IBook:IRepository<Book>
   7:      {
   8:      }
   9:  }

这里是BookRepository实现部分,它继承自Repository<Book>和IBook接口:

   1:   
   2:  using TinyFrame.Data.DataRepository;
   3:  using TinyFrame.Data.DataContext;
   4:  using TinyFrame.Data.DomainModel;
   5:   
   6:  namespace TinyFrame.Repository
   7:  {
   8:      public class BookRepository:Repository<Book>,IBook
   9:      {
  10:          public BookRepository(IDbContext context)
  11:              : base(context)
  12:          {
  13:              this.context = context;
  14:          }
  15:   
  16:          private IDbContext context;
  17:      }
  18:  }

这样包装之后,在TinyFrame.Services中,我们就可以实现自己的业务逻辑了。

比如,对于存储的书籍来说,我们需要对其进行增删改查:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Linq.Expressions;
   6:  using TinyFrame.Data.DomainModel;
   7:   
   8:  namespace TinyFrame.Services
   9:  {
  10:      public interface IBookService
  11:      {
  12:          IList<BookPlace> GetAllPlaces();
  13:          BookPlace GetPlacesByBookID(int bookid);
  14:   
  15:          IList<BookType> GetAllTypes();
  16:          BookType GetTypesByBookID(int bookid);
  17:   
  18:          Book GetBook(int bookid);
  19:          IList<Book> GetAllBooks();
  20:          IList<Book> GetBooks(Expression<Func<Book,bool>> where);
  21:   
  22:          bool AddBook(Book book);
  23:          bool UpdateBook(Book book);
  24:          bool DeleteBook(Book book);
  25:      }
  26:  }

它的实现部分如下:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using TinyFrame.Data.DataUnitOfWork;
   6:  using TinyFrame.Repository;
   7:  using System.Linq.Expressions;
   8:  using TinyFrame.Data.DomainModel;
   9:   
  10:  namespace TinyFrame.Services
  11:  {
  12:      public class BookService:IBookService
  13:      {
  14:          public BookService(IUnitOfWork unitOfWork
  15:              , IBook bookRepository
  16:              , IBookType bookTypeRepository
  17:              , IBookPlace bookPlaceRepository
  18:              )
  19:          {
  20:              this.unitOfWork = unitOfWork;
  21:              this.bookRepository = bookRepository;
  22:              this.bookTypeRepository = bookTypeRepository;
  23:              this.bookPlaceRepository = bookPlaceRepository;
  24:          }
  25:   
  26:          private readonly IUnitOfWork unitOfWork;
  27:          private readonly IBook bookRepository;
  28:          private readonly IBookType bookTypeRepository;
  29:          private readonly IBookPlace bookPlaceRepository;
  30:   
  31:          public IList<BookPlace> GetAllPlaces()
  32:          {
  33:              return bookPlaceRepository.GetMany(m => m.ID >= 0).ToList();
  34:          }
  35:   
  36:          public BookPlace GetPlacesByBookID(int bookid)
  37:          {
  38:              int bookPlaceID = bookRepository.Get(m => m.ID == bookid).BookPlaceID;
  39:              return bookPlaceRepository.Get(m => m.ID == bookPlaceID);
  40:          }
  41:   
  42:          public IList<BookType> GetAllTypes()
  43:          {
  44:              return bookTypeRepository.GetMany(m => m.ID >= 0).ToList();
  45:          }
  46:   
  47:          public BookType GetTypesByBookID(int bookid)
  48:          {
  49:              int bookTypeID = bookRepository.Get(m => m.ID == bookid).BookTypeID;
  50:              return bookTypeRepository.Get(m => m.ID == bookTypeID);
  51:          }
  52:   
  53:          public Book GetBook(int bookid)
  54:          {
  55:              return bookRepository.Get(m=>m.ID==bookid);
  56:          }
  57:   
  58:          public IList<Book> GetAllBooks()
  59:          {
  60:              return bookRepository.GetMany(m => m.ID >= 0).ToList();
  61:          }
  62:   
  63:          public IList<Book> GetBooks(Expression<Func<Book, bool>> where)
  64:          {
  65:              return bookRepository.GetMany(where).ToList();
  66:          }
  67:   
  68:   
  69:          public bool AddBook(Book book)
  70:          {
  71:              try
  72:              {
  73:                  bookRepository.Insert(book);
  74:                  unitOfWork.Commit();
  75:                  return true;
  76:              }
  77:              catch { return false; }
  78:          }
  79:   
  80:          public bool UpdateBook(Book book)
  81:          {
  82:              try
  83:              {
  84:                  bookRepository.Update(book);
  85:                  unitOfWork.Commit();
  86:                  return true;
  87:              }
  88:              catch { return false; }
  89:          }
  90:   
  91:          public bool DeleteBook(Book book)
  92:          {
  93:              try
  94:              {
  95:                  bookRepository.Delete(book);
  96:                  unitOfWork.Commit();
  97:                  return true;
  98:              }
  99:              catch { return false; }
 100:          }
 101:      }
 102:  }

通过上面的代码,我们就能够有效地控制逻辑部分,同时融合cache和log的话,基本上就可以随心所欲的控制了。