Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical Test-Driven Development using C# 7

You're reading from   Practical Test-Driven Development using C# 7 Unleash the power of TDD by implementing real world examples under .NET environment and JavaScript

Arrow left icon
Product type Paperback
Published in Feb 2018
Publisher
ISBN-13 9781788398787
Length 442 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
John Callaway John Callaway
Author Profile Icon John Callaway
John Callaway
Clayton Hunt Clayton Hunt
Author Profile Icon Clayton Hunt
Clayton Hunt
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Why TDD is Important FREE CHAPTER 2. Setting Up the .NET Test Environment 3. Setting Up a JavaScript Environment 4. What to Know Before Getting Started 5. Tabula Rasa – Approaching an Application with TDD in Mind 6. Approaching the Problem 7. Test-Driving C# Applications 8. Abstract Away Problems 9. Testing JavaScript Applications 10. Exploring Integrations 11. Changes in Requirements 12. The Legacy Problem 13. Unraveling a Mess 14. A Better Foot Forward 15. Other Books You May Enjoy

Genericizing the repository


This repository is great, but do we really want to repeat this logic and all these tests for every single data model that we need to be retrieved from some data source? The answer is no; if you find yourself doing the same thing over and over as a developer, you are doing something wrong.

So, how can we protect ourselves from that drudgery?

One way is to use generics. Let's refactor the SpeakerRepository to use generics, this will also involve refactoring many of the tests to make them apply to the GenericRepository instead of the concrete SpeakerRepository.

Step one – abstract interface

In the IRepository, everywhere we use Speaker we need to replace it with C# generics:

public interface IRepository<T>
{
  T Create(T item);
  T Get(int id);
  IQueryable<T> GetAll();
  T Update(T item);
  void Delete(T item);
}

This change will cause a break in the SpeakerRepository that we need to fix. Right now, we are chasing the compiler and leaning on it to tell us...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image