C# 7 and .NET Core 2.0 Blueprints
上QQ阅读APP看书,第一时间看更新

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 mine eBookManager:

The project will be created and will look as follows:

Our Solution needs a Class Library project to contain the classes that drive the eBookManager application. Add a new Class Library project to your Solution and call it eBookManager.Engine:

A Class Library project is added to the Solution with the default class name. Change this class to Document:

The Document class 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.

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 called Classification 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:

Your eBookManager.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.
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...
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.