Let’s practice today a series of concepts in this simple Console Application. But beware that it won’t be as simple as it looks 🙂
This exercise will put your hands on a book library solution, which needs to have a UI using a simple Console application. But in the guts of the application, the expectation is to have a layered architecture, a Service that will interact with these books, and a valid database connection (using an InMemory database this time) through Repositories. We will publish and deploy this Application into a Docker container, and lastly, we’ll push this code into a Git repository.
Challenges in this exercise:
- Set up a Console Application that will act as the UI of the solution
- Layered Architecture (Clean Architecture)
- Use of the Repository Pattern for adding/updating/listing/removing books
- Connection to an InMemory Database using Entity Framework.
- Seed 5 books to this database
- Correct usage of IEnumerable, ICollection, IList, and Lists wherever applicable
- Setting up a Docker image, so we deploy the App into a container, and run it there
- Push your code into a new Git repository
Requirements:
Entities Layer:
Create a Book
class with properties: Id
, Title
, Author
, and Rating
.
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public double Rating { get; set; }
}
Data Access Layer:
- Create an
IBookRepository
interface with methods:Add(Book item)
,Remove(Book item)
,GetAll()
, andGetById(int id)
. - Create a LibraryDbContext to set up the In-Memory Database. Also handle the Seeding of Books with a few examples.
Application Layer:
- Create a
BookService
class that uses theIBookRepository
to manage the books. Implement methods toAddBook
,RemoveBook
,ListAllBooks
, andFindBookById
.
Console UI Layer:
- Create a menu-driven console application.
- Options should include: Add a book, Remove a book, List all books, Find a book by ID, and Exit.
- PRO Tip: the menu selection can be easily be implemented with a switch statement. However, we want to follow best coding practices right? 🙂
- Think of a way to improve this part of the code. Using the Command Pattern could be a good way to go.
- PRO Tip 2: Instead of using the Main method of Program.cs for doing all the work, you can also think of a way to implement Dependency Injection here, decoupling the different services that you will be using in the application. Not ideal for Console Apps but this is an exercise and we can enhance our practice session including more stuff 🙂
Docker
Now, once you have the Console App working properly, publish it in release mode into a folder, and use the app binaries to configure the dockerfile.
If you never used Docker before, you can read the official documentation Here. You can also browse on the internet or ask Chat-GPT, there are a ton of resources to learn the basics of Docker.
Once you have everything up and running, the main idea is to create a Docker image, and then run this image, deploying the application in a container. After running the container, you will be able to interactively use the menu to perform the different CRUD operations, all inside the container, completely isolated from your local environment. 😉
The Solution
There are multiple ways to achieve the resolution of this exercise, but as usual I’ll share with you a sample implementation which can be found under my Github profile:
Repo: https://github.com/marcelomusza/LibraryExerciseWithDocker
In order to maximize your learning opportunities throughout the course of this exercise, make sure to invest time in research whatever concept you are not familiar with, Google is your friend, and also Chat-GPT 😉
If you get completely stuck, then take a look to the working sample you’ll find under my Github profile.