Lazycoder

12Dec/08Off

Episode 28: Miguel de Icaza (part 1)

This week we talk with Miguel de Icaza about Mono, Moonlight, and other fun stuff. Topics Overview and update on Mono Mono’s roots as a tool for desktop applications on Gnome / Linux The need for a package manager in Windows Managed operating systems (like Microsoft Research Singularity) New areas of focus for Mono – [...]
12Dec/082

Mocking IDataReader using Rhino.Mocks 3.5

The other day I was writing unit tests for some legacy code and I needed to mock IDataReader. I really just wanted to populate the reader with a single row of data, then the Read() method should return false. Using Rhino.Mocks it was a piece of cake.

CODE:
  1. IDataReader reader = MockRepository.GenerateStub<IDataReader>();
  2.  
  3.             reader.Stub(x => x.Read()).Return(true).Repeat.Times(1);
  4.  
  5.             reader.Stub(x => x.Read()).Return(false);
  6.  
  7.             reader.Stub(x => x["ID"]).Return(Guid.Empty);
  8.  
  9.             reader.Stub(x => x["FullName"]).Return("Test User");
  10.  
  11.  
  12.  
  13.             List<UserDTO> list = SearchProvider.ParseUserData(reader);
  14.  
  15.             Assert.IsNotNull(list);

The magic happens in the Repeat.Times(1) statement. This tells Rhino.Mocks that when the method is called, the mock should Return the given value that number of times. After that it can return a different value.

Update: Jeremy Miller told me about the DataTableReader class in the BCL and that you can use it to stub out IDataReader. I like this better because it removes the dependency the test had on Rhino.Mocks.

CODE:
  1. DataTable table = new DataTable();
  2.             DataRow row = table.NewRow();
  3.             table.Columns.Add(new DataColumn("ID"));
  4.             table.Columns.Add(new DataColumn("FullName"));
  5.             row["DirectoryUserID"] = Guid.Empty;
  6.             row["FullName"] = "Test User";
  7.             table.Rows.Add(row);
  8.             DataTableReader reader = new DataTableReader(table);

Filed under: .NET, C#, Database 2 Comments
10Dec/082

What is the primary purpose of an ORM?

ORM stands for Object-Relational Mapper. It's mostly used to define a relationship between an object/class and the database table where it's data should be saved.

I asked the question on Twitter/Facebook "The primary purpose of an ORM is to decouple the domain model from the database schema. True || False? Discuss." and got some interesting answers.

orm_answers

What do you think the primary purpose of an ORM is?

Filed under: General 2 Comments