• 悲观并发控制


        public abstract class EntityBase
        {
            private int Version { get; set; }
        }
        /// <summary>
        /// 当从数据库中检索出Person实体时设置Version属性。
        /// </summary>
        public class Person : EntityBase
        {
            public Guid Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int Version { get; set; }
        }
    Person
        public interface IPersonRepository
        {
            void Add(Person person);
            void Save(Person person);
            Person FindBy(Guid Id);
        }
    IPersonRepository
        public class PersionRepository : IPersonRepository
        {
            private string _connectionString;
            private string _findByIdSql = "select * from people where PersonId=@PersonId";
            private string _insertSql = "insert people (FristName,LastName,PersonId,Version) values (@FirstName,@LastName,@PersonId,@Version)";
            private string _updateSql = "update people SET FirstName=@FirstName,LastName=@LastName,Version=@Version+1 where PersonId=@PersonId AND Version=@Version";
    
            public PersionRepository(string connectionString)
            {
                _connectionString = connectionString;
            }
    
            public void Add(Person person)
            {
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = _insertSql;
                    command.Parameters.Add(new SqlParameter("@PersonId", person.Id));
                    command.Parameters.Add(new SqlParameter("@Version", person.Version));
                    command.Parameters.Add(new SqlParameter("@FirstName", person.FirstName));
                    command.Parameters.Add(new SqlParameter("@LastName", person.LastName));
                    connection.Open();
                    command.ExecuteNonQuery();
                }
            }
    
            public void Save(Person person)
            {
                int numberOfRecordsAffected = 0;
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = _updateSql;
                    command.Parameters.Add(new SqlParameter("@PersonId", person.Id));
                    command.Parameters.Add(new SqlParameter("@Version", person.Version));
                    command.Parameters.Add(new SqlParameter("@FirstName", person.FirstName));
                    command.Parameters.Add(new SqlParameter("@LastName", person.LastName));
                    connection.Open();
                    numberOfRecordsAffected = command.ExecuteNonQuery();
                }
                if (numberOfRecordsAffected == 0)
                    throw new ApplicationException("No changes....");
                else
                {
                    person.Version++;
                }
            }
    
            public Person FindBy(Guid Id)
            {
                Person person = default(Person);
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = _findByIdSql;
                    command.Parameters.Add(new SqlParameter("@PersonId", Id));
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            person = new Person
                            {
                                FirstName = reader["FristName"].ToString(),
                                LastName = reader["LastName"].ToString(),
                                Id = new Guid(reader["PersonId"].ToString()),
                                Version = int.Parse(reader["Version"].ToString())
                            };
                        }
                    }
                }
                return person;
            }
        }
    PersionRepository
  • 相关阅读:
    SLAM基础知识
    标准的机器学习问题
    LAS的数据格式
    Python中的多线程和多进程
    【go】log
    【亲密关系】3-吸引力
    【mysql】做 mariadb 的备库无法启动
    【zabbix-server】Supervising process xxxx which is not our child....exits
    【win10】添加程序自启动
    【Oralce】数据去重,限制某个字段的数据长度,替换空格符
  • 原文地址:https://www.cnblogs.com/vichin/p/13092112.html
Copyright © 2020-2023  润新知