Book Image

C# 7 and .NET Core 2.0 Blueprints

By : Dirk Strauss, Jas Rademeyer
Book Image

C# 7 and .NET Core 2.0 Blueprints

By: Dirk Strauss, Jas Rademeyer

Overview of this book

.NET Core is a general purpose, modular, cross-platform, and open source implementation of .NET. With the latest release of .NET Core, many more APIs are expected to show up, which will make APIs consistent across .Net Framework, .NET Core, and Xamarin. This step-by-step guide will teach you the essential .NET Core and C# concepts with the help of real-world projects. The book starts with a brief introduction to the latest features of C# 7 and .NET Core 2.0 before moving on to explain how C# 7 can be implemented using the object-oriented paradigm. You'll learn to work with relational data using Entity Framework and see how to use ASP.NET Core practically. This book will show you how .NET Core allows the creations of cross-platform applications. You'll also learn about SignalR to add real-time functionality to your application. Then you will see how to use MongoDB and how to implement MongoDB into your applications. You'll learn about serverless computing and OAuth concepts, along with running ASP.NET Core applications with Docker Compose. This project-based guide uses practical applications to demonstrate these concepts. By the end of the book, you'll be proficient in developing applications using .NET Core 2.0.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Setting up the project


Using Visual Studio 2017, we will create a simple Windows Forms App template project. You can call the application anything you like, but I called mineeBookManager:

The project will be created and will look as follows:

OurSolutionneeds a Class Libraryproject to contain the classes that drive theeBookManagerapplication. Add a new Class Library project to your Solution and call iteBookManager.Engine:

A Class Libraryproject is added to the Solution with the default class name. Change this class toDocument:

TheDocumentclass will represent a single eBook. Thinking of a book, we can have multiple properties that would represent a single book, but be representative of all books. An example of this would be the author. All books must have an author, otherwise it would not exist.

Note

I know some of you might be thinking that machines could generate documents too, but the information it generates was probably originally written by a person. Take code comments for example. A developer writes the comments in code, and a tool generates a document from that. The developer is still the author.

The properties I have added to the class are merely my interpretation of what might represent a book. Feel free to add additional code to make this your own.

Open the Document.cs file and add the following code to the class:

namespace eBookManager.Engine 
{ 
    public class Document 
    { 
        public string Title { get; set; } 
        public string FileName { get; set; } 
        public string Extension { get; set; } 
        public DateTime LastAccessed { get; set; } 
        public DateTime Created { get; set; } 
        public string FilePath { get; set; } 
        public string FileSize { get; set; } 
        public string ISBN { get; set; } 
        public string Price { get; set; } 
        public string Publisher { get; set; } 
        public string Author { get; set; } 
        public DateTime PublishDate { get; set; } 
        public DeweyDecimal Classification { get; set; } 
        public string Category { get; set; } 
    } 
} 

You will notice that I have included a property calledClassification of type DeweyDecimal. We have not added this class yet, and will do so next.

To the eBookManager.Engine project, add a class called DeweyDecimal. If you don't want to go to this level of classification for your eBooks, you can leave this class out. I have included it for the sake of completeness:

Your DeweyDecimal class must be in the same project as the Document class added earlier:

The DeweyDecimal system is quite big. For this reason, I have not catered for every book classification available. I have also only assumed that you would want to be working with programming eBooks. In reality, however, you may want to add in other classifications, such as literature, the sciences, the arts, and so on. It is up to you. So let's create a class to represent the Dewey Decimal system::

  1. Open up the DeweyDecimal class and add the following code to the class:
namespace eBookManager.Engine 
{ 
    public class DeweyDecimal 
    { 
        public string ComputerScience { get; set; } = "000"; 
        public string DataProcessing { get; set; } = "004"; 
        public string ComputerProgramming { get; set; } = "005"; 
    } 
}

Word nerds may disagree with me here, but I would like to remind them that I'm a code nerd. The classifications represented here are just so that I can catalog programming and computer science-related eBooks. As mentioned earlier, you can change this to suit your needs.

  1. We now need to add in the heart of the eBookManager.Engine Solution. This is a class called DocumentEngine and it will be a class that will contain the methods you need to work with the documents:

YoureBookManager.Engine Solution will now contain the following classes:

    • DeweyDecimal
    • Document
    • DocumentEngine
  1. We now need to add a reference to eBookManager.Engine from the eBookManager project. I'm sure that you all know how to do this:

The eBookManager.Engine project will be available under the Projects section in the Reference Manager screen:

  1. Once we have added the reference, we need a Windows Form that will be responsible for importing new books. Add a new form called ImportBooks to the eBookManager Solution:
  1. Before we forget, add an ImageList control to the ImportBooks form and call it tvImages. This will contain the images for the different types of documents we want to catalog.

Note

The ImageList is a control you add from the Toolbox on to the ImportBooks form. You can access the Images Collection Editor from the ImageList properties.

The icons can be found in the img folder in the source code downloadable from GitHub at the following URL—https://github.com/PacktPublishing/CSharp7-and-.NET-Core-2.0-Blueprints.

The icons here are catering for PDF, MS Word, and ePub file types. It also contains folder images:

  1. Now, to use tuples in C# 7, you need to add the System.ValueTuple NuGet package. Right-click on your Solution and select Manage NuGet Packages for Solution...

Note

Please note that if you are running the .NET Framework 4.7, System.ValueTuple is included in this version of the framework. You will therefore not need to get it from NuGet.

  1. Search for System.ValueTuple and add it to your Solution projects. Then click Install and let the process complete (you will see the progress in the output window in Visual Studio):

I love making use of extension methods in my projects. I usually add a separate project and/or class for this purpose. In this application, I added an eBookManager.Helper class library project:

  1. This helper class must also be added to the eBookManager Solution as a reference:

Lastly, I will be using JSON as a simple file store for my eBook catalogue. JSON is really flexible and can be consumed by various programming languages. What makes JSON so nice is the fact that it is relatively light weight and the output it generates is human-readable:

  1. Go to Manage the NuGet packages for your Solution and search for Newtonsoft.Json. Add this then to the projects in your Solution and click the Install button.

You have now set up the basics needed for your eBookManager application. Next, we will venture further into the guts of the application by writing some code.