public BaseRepository( IDbConnection connection, IAdoDbCommandProvider <T> commandProvider, IAdoDataMapper <T> dataMapper) { _connection = connection; _commandProvider = commandProvider; _dataMapper = dataMapper; }
public BaseRepositoryTests() { _connection = new SqlConnection(@"Data Source=(localdb)\v11.0.; Initial Catalog=AdoUoWTestDb; Integrated Security=True"); #region Mock var dataMapperStup = new Mock<IAdoDataMapper<Foo>>(); dataMapperStup.Setup(f => f.Map(It.IsAny<IDataReader>())) .Returns( (IDataReader reader) => new Foo { Id = Guid.Parse(reader["Id"].ToString()), Name = reader["Name"].ToString() }); _dataMapper = dataMapperStup.Object; var commandProviderStud = new Mock<IAdoDbCommandProvider<Foo>>(); commandProviderStud.Setup(f => f.SelectByIdCommand(It.IsAny<IDbConnection>(), It.IsAny<IDbTransaction>(), It.IsAny<Guid>())) .Returns( (IDbConnection connection, IDbTransaction transaction, Guid id) => { IDbCommand command = new SqlCommand(); command.Transaction = _transaction; command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = "SELECT * FROM [Foos] WHERE Id = @Id"; command.Parameters.Add( new SqlParameter { ParameterName = "@Id", DbType = DbType.Guid, Value = id }); return command; }); commandProviderStud.Setup(f => f.DeleteCommand(It.IsAny<IDbConnection>(), It.IsAny<IDbTransaction>(), It.IsAny<Foo>())) .Returns( (IDbConnection connection, IDbTransaction transaction, Foo item) => { IDbCommand command = new SqlCommand(); command.Connection = connection; command.Transaction = _transaction; command.CommandType = CommandType.Text; command.CommandText = "DELETE FROM [Foos] WHERE Id = @Id"; command.Parameters.Add( new SqlParameter { ParameterName = "@Id", DbType = DbType.Guid, Value = item.Id }); return command; }); commandProviderStud.Setup(f => f.InsertCommand(It.IsAny<IDbConnection>(), It.IsAny<IDbTransaction>(), It.IsAny<Foo>())) .Returns( (IDbConnection connection, IDbTransaction transaction, Foo item) => { IDbCommand command = new SqlCommand(); command.Connection = connection; command.Transaction = _transaction; command.CommandType = CommandType.Text; command.CommandText = "INSERT INTO [Foos] VALUES(@Id,@Name)"; command.Parameters.Add( new SqlParameter { ParameterName = "@Id", DbType = DbType.Guid, Value = item.Id }); command.Parameters.Add( new SqlParameter { ParameterName = "@Name", DbType = DbType.String, Value = item.Name }); return command; }); commandProviderStud.Setup(f => f.UpdateCommand(It.IsAny<IDbConnection>(), It.IsAny<IDbTransaction>(), It.IsAny<Foo>())) .Returns( (IDbConnection connection, IDbTransaction transaction, Foo item) => { IDbCommand command = new SqlCommand(); command.Connection = connection; command.Transaction = _transaction; command.CommandType = CommandType.Text; command.CommandText = "UPDATE T SET T.Name = @Name FROM [Foos] T WHERE T.Id = @Id"; command.Parameters.Add( new SqlParameter { ParameterName = "@Id", DbType = DbType.Guid, Value = item.Id }); command.Parameters.Add( new SqlParameter { ParameterName = "@Name", DbType = DbType.String, Value = item.Name }); return command; }); commandProviderStud.Setup(f => f.SelectAllCommand(It.IsAny<IDbConnection>(), It.IsAny<IDbTransaction>())) .Returns( (IDbConnection connection, IDbTransaction transaction) => { IDbCommand command = new SqlCommand(); command.Connection = connection; command.Transaction = _transaction; command.CommandType = CommandType.Text; command.CommandText = "SELECT * FROM [Foos]"; return command; }); _commandProvider = commandProviderStud.Object; #endregion var repository = new BaseRepository<Foo>(_connection, _commandProvider, _dataMapper); _repositoryWriter = repository; _repositoryReader = repository; _connection.Open(); }
public BaseRepositoryTests() { _connection = new SqlConnection(@"Data Source=(localdb)\v11.0.; Initial Catalog=AdoUoWTestDb; Integrated Security=True"); #region Mock var dataMapperStup = new Mock <IAdoDataMapper <Foo> >(); dataMapperStup.Setup(f => f.Map(It.IsAny <IDataReader>())) .Returns( (IDataReader reader) => new Foo { Id = Guid.Parse(reader["Id"].ToString()), Name = reader["Name"].ToString() }); _dataMapper = dataMapperStup.Object; var commandProviderStud = new Mock <IAdoDbCommandProvider <Foo> >(); commandProviderStud.Setup(f => f.SelectByIdCommand(It.IsAny <IDbConnection>(), It.IsAny <IDbTransaction>(), It.IsAny <Guid>())) .Returns( (IDbConnection connection, IDbTransaction transaction, Guid id) => { IDbCommand command = new SqlCommand(); command.Transaction = _transaction; command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = "SELECT * FROM [Foos] WHERE Id = @Id"; command.Parameters.Add( new SqlParameter { ParameterName = "@Id", DbType = DbType.Guid, Value = id }); return(command); }); commandProviderStud.Setup(f => f.DeleteCommand(It.IsAny <IDbConnection>(), It.IsAny <IDbTransaction>(), It.IsAny <Foo>())) .Returns( (IDbConnection connection, IDbTransaction transaction, Foo item) => { IDbCommand command = new SqlCommand(); command.Connection = connection; command.Transaction = _transaction; command.CommandType = CommandType.Text; command.CommandText = "DELETE FROM [Foos] WHERE Id = @Id"; command.Parameters.Add( new SqlParameter { ParameterName = "@Id", DbType = DbType.Guid, Value = item.Id }); return(command); }); commandProviderStud.Setup(f => f.InsertCommand(It.IsAny <IDbConnection>(), It.IsAny <IDbTransaction>(), It.IsAny <Foo>())) .Returns( (IDbConnection connection, IDbTransaction transaction, Foo item) => { IDbCommand command = new SqlCommand(); command.Connection = connection; command.Transaction = _transaction; command.CommandType = CommandType.Text; command.CommandText = "INSERT INTO [Foos] VALUES(@Id,@Name)"; command.Parameters.Add( new SqlParameter { ParameterName = "@Id", DbType = DbType.Guid, Value = item.Id }); command.Parameters.Add( new SqlParameter { ParameterName = "@Name", DbType = DbType.String, Value = item.Name }); return(command); }); commandProviderStud.Setup(f => f.UpdateCommand(It.IsAny <IDbConnection>(), It.IsAny <IDbTransaction>(), It.IsAny <Foo>())) .Returns( (IDbConnection connection, IDbTransaction transaction, Foo item) => { IDbCommand command = new SqlCommand(); command.Connection = connection; command.Transaction = _transaction; command.CommandType = CommandType.Text; command.CommandText = "UPDATE T SET T.Name = @Name FROM [Foos] T WHERE T.Id = @Id"; command.Parameters.Add( new SqlParameter { ParameterName = "@Id", DbType = DbType.Guid, Value = item.Id }); command.Parameters.Add( new SqlParameter { ParameterName = "@Name", DbType = DbType.String, Value = item.Name }); return(command); }); commandProviderStud.Setup(f => f.SelectAllCommand(It.IsAny <IDbConnection>(), It.IsAny <IDbTransaction>())) .Returns( (IDbConnection connection, IDbTransaction transaction) => { IDbCommand command = new SqlCommand(); command.Connection = connection; command.Transaction = _transaction; command.CommandType = CommandType.Text; command.CommandText = "SELECT * FROM [Foos]"; return(command); }); _commandProvider = commandProviderStud.Object; #endregion var repository = new BaseRepository <Foo>(_connection, _commandProvider, _dataMapper); _repositoryWriter = repository; _repositoryReader = repository; _connection.Open(); }