public void ShouldGetNameAndDirectionOfParameter() { //Given var databaseSessionStub = new DatabaseSessionStub {UpdateResultParameter = new SqlParameter {Value = 12}}; var databaseSessionFactoryStub = new DatabaseSessionFactoryStub {SessionValue = databaseSessionStub}; //When var contact = new Contact { FirstName = "David", Surname = "Miranda", Telephone = "666" }; var contactId = new ContactRepository(databaseSessionFactoryStub).UpdateContact(contact); var dbQueries = databaseSessionStub.Queries; //Then Assert.That(dbQueries[0].Parameters[0].Direction, Is.EqualTo(ParameterDirection.Output)); Assert.That(dbQueries[0].Parameters[0].Name, Is.EqualTo("@ContactId")); Assert.That(dbQueries[0].StoredProcName, Is.EqualTo("proc_name")); Assert.That(dbQueries[0].Parameters[1].Value, Is.EqualTo(contact.FirstName)); Assert.That(dbQueries[0].Parameters[2].Value, Is.EqualTo(contact.Surname)); Assert.That(dbQueries[0].Parameters[3].Value, Is.EqualTo(contact.Telephone)); Assert.That(contactId, Is.EqualTo(12)); }
//SIMPLE INSERT - using Stored Proc (note the DataQueryBuilder method BuildStoredQuery()) public int Create(Contact contact) { //In this example it would be the Sproc responsibility to pass back ID, prob using SCOPE_IDENTITY() return (int)_dbSession.ExecuteScalar(_dbSession.CreateQuery() .WithStoredProc("CreateContact") .WithParam("@FirstName", contact.FirstName) .WithParam("@Surname", contact.Surname) .WithParam("@Telephone", contact.Telephone)); }
public void SetUp() { _contact = new Contact() { FirstName = "David", Surname = "Miranda", Telephone = "666" }; _dataReader = new Mock<IDatabaseReader>(); _dataReader.Setup(x => x.Read()).Returns(true); _dataReader.Setup(x => x.Get<string>("FirstName")).Returns("David"); _dataReader.Setup(x => x.Get<string>("Surname")).Returns("Miranda"); _dataReader.Setup(x => x.Get<string>("Telephone")).Returns("666"); _databaseSession = new Mock<IDatabaseSession>(); _databaseSession.Setup(x => x.CreateQuery()).Returns(new DataQuery()); _databaseSessionFactory = new Mock<IDatabaseSessionFactory>(); _databaseSessionFactory.Setup(x => x.CreateSession(It.IsAny<string>())).Returns(_databaseSession.Object); _contactRepository = new ContactRepository(_databaseSessionFactory.Object); }
//CREATE QUERY - Using stored proc and output param (only caters for one paramter at the moment) public Contact CreateContact(Contact contact) { var contactIdOutputParam = new DataParam //TODO: make an SqlInputOutputParameter class { Value = 0, Direction = ParameterDirection.InputOutput, Size = 4 }; var dataQuery = _dbSession.CreateQuery() .WithQueryText("create_contact") .WithParam("@ContactId", contactIdOutputParam) .WithParam("@FirstName", contact.FirstName) .WithParam("@Surname", contact.Surname) .WithParam("@Telephone", contact.Telephone); //Need to cast it to an SqlParameter as the Mapping of data types to the generic DataParameter is whack. contact.Id = (int)((SqlParameter)_dbSession.ExecuteUpdate(dataQuery, "@ContactId")).Value; return contact; }
public int UpdateContact(Contact contact) { var dataQuery = _dbSession.CreateQuery(); var executeUpdate = _dbSession.ExecuteUpdate(dataQuery.WithStoredProc("proc_name") .WithParam("@ContactId", 0, ParameterDirection.Output) .WithParam("@Firstname", contact.FirstName) .WithParam("@Surname", contact.Surname) .WithParam("@Telephone", contact.Telephone), "@ContactId"); return (int)executeUpdate.Value; }
public void Save(Contact contact) { //This is where I would save my Contact using update(has Id) or create (no Id) }
public void BeforeEachTest() { _contact = new Contact {FirstName = "David", Surname = "Miranda", Telephone = "999"}; _contactId = UnitOfWork.Repository<ContactRepository>().Create(_contact); }