Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / C#

Improving LINQ Code Reusability: Select Method

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
27 Nov 2012CPOL3 min read 9.8K   41   8  
A simple approach that will allow you to reuse the code used in the Select method
namespace Tests
{
	using System;

	using NUnit.Framework;

	using Domain;
	using DataContracts;
	using Mapper;


	[TestFixture]
	public class ObjectMapperTests
	{
		[Test]
		public void Given_a_non_existing_mapping_when_mapping_object_then_should_throw_InvalidOperationException()
		{
			// arrange
			IObjectMapper mapper = new ObjectMapper();
			var albumDetail = new AlbumDetail();

			// act/assert
			Assert.Throws<InvalidOperationException>(() => 
				// non-existing mapping
				mapper.Map<AlbumDetail, AlbumSummary>(albumDetail)
			);
		}

		[Test, TestCaseSource("AlbumSummaryTestCases")]
		public void Given_an_album_when_mapping_to_album_summary_should_equals_expected_album_summary(Album album, AlbumSummary expectedAlbumSummary)
		{
			// arrange
			IObjectMapper mapper = new ObjectMapper();

			// act
			AlbumSummary albumSummary = mapper.Map<Album, AlbumSummary>(album);

			// assert
			Assert.AreEqual(albumSummary, expectedAlbumSummary);
		}


		[Test, TestCaseSource("AlbumDetailTestCases")]
		public void Given_an_album_when_mapping_to_album_detail_should_equals_expected_album_detail(Album album, AlbumDetail expectedAlbumDetail)
		{
			// arrange
			IObjectMapper mapper = new ObjectMapper();

			// act
			AlbumDetail albumDetail = mapper.Map<Album, AlbumDetail>(album);

			// assert
			Assert.AreEqual(albumDetail, expectedAlbumDetail);
		}

		#region Test cases

		private static object[] AlbumDetailTestCases = 
		{
			new object[] {
			    null,
			    null
			},

			new object[] {
			    new Album(),
			    new AlbumDetail()
			}

			// TO-DO: Add more test cases
		};


		private static object[] AlbumSummaryTestCases = 
		{
			new object[] {
			    null,
			    null
			},

			new object[] {
			    new Album(),
			    new AlbumSummary()
			},


			new object[] {
				new Album {
					AlbumId = 1,
					Price = 10.0m,
					Title = "The Dreamer",

					Artist = new Artist {
						ArtistId = 1,
						Name = "José James"
					},

					Genre = new Genre {
						GenreId = 1,
						Name = "Jazz"
					}
				},

				// expected mapped object
				new AlbumSummary{
					AlbumId = 1,
					ArtistName = "José James",
					Title = "The Dreamer"
				}
			},


			new object[] {
				new Album {
					AlbumId = 2,
					Price = 12.0m,
					Title = "Final Conversation of Kings",

					Artist = new Artist {
						ArtistId = 2,
						Name = "The Butterfly Effect"
					},

					Genre = new Genre {
						GenreId = 2,
						Name = "Progressive Rock"
					}
				},

				// expected mapped object
				new AlbumSummary {
					AlbumId = 2,
					ArtistName = "The Butterfly Effect",
					Title = "Final Conversation of Kings"
				}
			},

			
			new object[] {
				new Album {
					AlbumId = 4,
					Price = 10.0m,
					Title = "Heritage",

					Artist = new Artist {
						ArtistId = 4,
						Name = "Opeth"
					},

					Genre = new Genre {
						GenreId = 4,
						Name = "Metal"
					}
				},

				// expected mapped object
				new AlbumSummary {
					AlbumId = 4,
					ArtistName = "Opeth",
					Title = "Heritage"
				}
			}
		};

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Italy Italy
My name is Rui Jarimba and I was born in Madeira island, Portugal and I currently live in Rome, Italy.

I have more than 10 years of experience developing software using the .NET Framework and other technologies (Web development, Databases, ...).

Some of my professional interests are: software development best practices, software architecture, cloud computing, Continuous Integration (CI), Continuous Delivery (CD) and agile methodologies such as Scrum, Kanban, Lean and any other methodology that can help me to become a better and more productive software engineer.

I believe in good code - code that is readable, maintainable, reusable, testable and deployable. This means that I'm not the "quick and dirty" type, I write code for the medium/long term whenever possible.

Something else about me - I love music, I am an amateur photographer, not a big fan of gyms (I prefer to do some outdoor activity such as walking/hiking), big foodie (I love Mediterranean cuisine and my glass of wine!).

Comments and Discussions