Dapper+UOW+Repository示例代码

DataAccess

IRepositories

    public interface IGenericRepository<TEntity> where TEntity : class
    {
        TEntity Get(int Id);
        IEnumerable<TEntity> GetAll();
        void Add(TEntity entity);
        void Delete(TEntity entity);
        void Update(TEntity entity);
    }
    public abstract class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
    {
        protected IDbConnection Connection;

        protected GenericRepository(IDbConnection connection)
        {
            Connection = connection;
        }

        public virtual void Add(TEntity entity)
        {
            throw new System.NotImplementedException();
        }

        public virtual void Delete(TEntity entity)
        {
            throw new System.NotImplementedException();
        }

        public virtual TEntity Get(int Id)
        {
            throw new System.NotImplementedException();
        }

        public virtual IEnumerable<TEntity> GetAll()
        {
            throw new System.NotImplementedException();
        }

        public virtual void Update(TEntity entity)
        {
            throw new System.NotImplementedException();
        }
    }
    public interface IPatientRepository : IGenericRepository<Patient>
    {
    }
     public interface IStudyRepository: IGenericRepository<Study>
    {
    }

Repositories

    public class PatientRepository : GenericRepository<Patient>, IPatientRepository
   {
       public PatientRepository(IDbConnection connection) : base(connection)
       {
       }

       public void Add(Patient entity)
       {
           Connection.Execute(
               "insert into Patient(PatientID,PatientName, PatientDescription) values(@PatientID, @PatientName,@PatientDescription)",
               entity);
       }

       public void Delete(Patient entity)
       {
           throw new System.NotImplementedException();
       }

       public Patient Get(int Id)
       {
           return Connection.Query<Patient>("select * from Patient where PatientID=@id", new {id = Id}).SingleOrDefault();
       }

       public IEnumerable<Patient> GetAll()
       {
           throw new System.NotImplementedException();
       }

       public void Update(Patient entity)
       {
           throw new System.NotImplementedException();
       }
   }
     public class StudyRepository : GenericRepository<Study>, IStudyRepository
   {
       public StudyRepository(IDbConnection connection):base(connection)
       {
       }

       public void Add(Study entity)
       {
           Connection.Execute(
               "insert into Study(StudyID,StudyDescription,PatientID) values(@StudyID,@StudyDescription,@PatientID)",
               new
               {
                   StudyID = entity.StudyID,
                   StudyDescription = entity.StudyDescription,
                   PatientID = entity.Patient.PatientID
               });
       }

       public void Delete(Study entity)
       {
           throw new NotImplementedException();
       }

       public Study Get(int Id)
       {
           throw new NotImplementedException();
       }

       public IEnumerable<Study> GetAll()
       {
           throw new NotImplementedException();
       }

       public void Update(Study entity)
       {
           throw new NotImplementedException();
       }
   } 

UnitOfWork

    public interface IUnitOfWork : IDisposable
   {
       IPatientRepository PatientRepository { get; }
       IStudyRepository StudyRepository { get; }

       void Commit();
   }
   public class UnitOfWork : IUnitOfWork
   {
       private IDbConnection _connection;
       private IDbTransaction _transaction;

       private IPatientRepository _patientRepository;

       public IPatientRepository PatientRepository =>
           _patientRepository ?? (_patientRepository = new PatientRepository(_connection));

       private IStudyRepository _studyRepository;
       public IStudyRepository StudyRepository =>
           _studyRepository ?? (_studyRepository = new StudyRepository(_connection));

       public UnitOfWork(IConnectionFactory connectionFactory)
       {
           _connection = connectionFactory.GetConnection();
           _connection.Open();
           _transaction = _connection.BeginTransaction();
       }

       public void Commit()
       {
           try
           {
               _transaction.Commit();
           }
           catch
           {
               _transaction.Rollback();
               throw;
           }
           finally
           {
               _transaction.Dispose();
               _transaction = _connection.BeginTransaction();
           }
       }

       private bool _disposed = false;

       public void Dispose()
       {
           Dispose(true);
           GC.SuppressFinalize(this);
       }

       private void Dispose(bool disposing)
       {
           if (!_disposed)
           {
               if (disposing)
               {
                   if (_transaction != null)
                   {
                       _transaction.Dispose();
                       _transaction = null;
                   }
                   if (_connection != null)
                   {
                       _connection.Dispose();
                       _connection = null;
                   }
               }

               _disposed = true;
           }
       }
   }

Model

    public class Patient
    {
        public int PatientID { get; set; }
        public string PatientName { get; set; }
        public string PatientDescription { get; set; }
    }
    public class Study
    {
        public string StudyID { get; set; }
        public string StudyDescription { get; set; }

        public Patient Patient { get; set; }
    }

TestConsole

    class Program
    {
        static void Main(string[] args)
        {
            using (var uow = new UnitOfWork(new ConnectionFactory()))
            {
                var patient = new Patient { PatientID = 7, PatientDescription = "hello", PatientName = "xdd" };
                var study = new Study { Patient = patient, StudyID = "6", StudyDescription = "ggd" };

                uow.PatientRepository.Add(patient);
                uow.StudyRepository.Add(study);
                uow.Commit();
            }
        }
    }

你可能感兴趣的:(.net)