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

Main eBookManager form


Start off by renaming Form1.cs to eBookManager.cs. This is the start-up form for the application and it will list all the existing storage spaces previously saved:

Design your eBookManager form as follows:

  • ListView control for existing storage spaces
  • ListView for eBooks contained in selected storage space
  • Button that opens the file location of the eBook
  • A menu control to navigate to the ImportBooks.cs form
  • Various read-only fields to display the selected eBook information

When you have added the controls, your eBook Manager form will look as follows:

Looking at the code we used earlier, you need to ensure that the following using statements are imported:

using eBookManager.Engine; 
using eBookManager.Helper; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Windows.Forms; 
using System.Linq; 
using System.Diagnostics; 

The constructor is quite similar to the ImportBooks.cs form's constructor. It reads any available storage spaces and populates the storage spaces list view control with the previously saved storage spaces:

private string _jsonPath; 
private List<StorageSpace> spaces; 
 
public eBookManager() 
{ 
    InitializeComponent(); 
    _jsonPath = Path.Combine(Application.StartupPath, 
    "bookData.txt"); 
    spaces = spaces.ReadFromDataStore(_jsonPath); 
} 
 
private void Form1_Load(object sender, EventArgs e) 
{             
    PopulateStorageSpaceList(); 
} 
 
private void PopulateStorageSpaceList() 
{ 
    lstStorageSpaces.Clear(); 
    if (!(spaces == null)) 
    { 
        foreach (StorageSpace space in spaces) 
        { 
            ListViewItem lvItem = new ListViewItem(space.Name, 0); 
            lvItem.Tag = space.BookList; 
            lvItem.Name = space.ID.ToString(); 
            lstStorageSpaces.Items.Add(lvItem); 
        } 
    } 
} 

If the user clicks on a storage space, we need to be able to read the books contained in that selected space:

private void lstStorageSpaces_MouseClick(object sender, MouseEventArgs e) 
{ 
    ListViewItem selectedStorageSpace = 
    lstStorageSpaces.SelectedItems[0]; 
    int spaceID = selectedStorageSpace.Name.ToInt(); 
 
    txtStorageSpaceDescription.Text = (from d in spaces 
                                       where d.ID == spaceID 
                                       select 
                                       d.Description).First(); 
 
    List<Document> ebookList = 
     (List<Document>)selectedStorageSpace.Tag; 
     PopulateContainedEbooks(ebookList); 
}

We now need to create the method that will populate the lstBooks list view with the books contained in the selected storage space:

private void PopulateContainedEbooks(List<Document> ebookList) 
{ 
    lstBooks.Clear(); 
    ClearSelectedBook(); 
 
    if (ebookList != null) 
    { 
        foreach (Document eBook in ebookList) 
        { 
            ListViewItem book = new ListViewItem(eBook.Title, 1); 
            book.Tag = eBook; 
            lstBooks.Items.Add(book); 
        } 
    } 
    else 
    { 
        ListViewItem book = new ListViewItem("This storage space 
        contains no eBooks", 2); 
        book.Tag = ""; 
        lstBooks.Items.Add(book); 
    } 
} 

You will notice that each ListViewItem is populated with the title of the eBook and the index of an image in an ImageList control that I added to the form. To find the images in the GitHub repository, browse to the following path:

https://github.com/PacktPublishing/CSharp7-and-.NET-Core-2.0-Blueprints/tree/master/eBookManager/eBookManager/img

Looking at the Images Collection Editor, you will see that I have added them as follows:

We also need to clear the selected book's details when the selected storage space is changed. I have created two group controls around the file and book details. This code just loops through all the child controls, and if the child control is a textbox, it clears it:

private void ClearSelectedBook() 
{ 
    foreach (Control ctrl in gbBookDetails.Controls) 
    { 
        if (ctrl is TextBox) 
            ctrl.Text = ""; 
    } 
 
    foreach (Control ctrl in gbFileDetails.Controls) 
    { 
        if (ctrl is TextBox) 
            ctrl.Text = ""; 
    } 
 
    dtLastAccessed.Value = DateTime.Now; 
    dtCreated.Value = DateTime.Now; 
    dtDatePublished.Value = DateTime.Now; 
} 

The MenuStrip that was added to the form has a click event on the ImportEBooks menu item. It simply opens up the ImportBooks form:

private void mnuImportEbooks_Click(object sender, EventArgs e) 
{ 
    ImportBooks import = new ImportBooks(); 
    import.ShowDialog(); 
    spaces = spaces.ReadFromDataStore(_jsonPath); 
    PopulateStorageSpaceList(); 
} 

The following method wraps up the logic to select a specific eBook and populate the file and eBook details on the eBookManager form:

private void lstBooks_MouseClick(object sender, MouseEventArgs e) 
{ 
    ListViewItem selectedBook = lstBooks.SelectedItems[0]; 
    if (!String.IsNullOrEmpty(selectedBook.Tag.ToString())) 
    { 
        Document ebook = (Document)selectedBook.Tag; 
        txtFileName.Text = ebook.FileName; 
        txtExtension.Text = ebook.Extension; 
        dtLastAccessed.Value = ebook.LastAccessed; 
        dtCreated.Value = ebook.Created; 
        txtFilePath.Text = ebook.FilePath; 
        txtFileSize.Text = ebook.FileSize; 
        txtTitle.Text = ebook.Title; 
        txtAuthor.Text = ebook.Author; 
        txtPublisher.Text = ebook.Publisher; 
        txtPrice.Text = ebook.Price; 
        txtISBN.Text = ebook.ISBN; 
        dtDatePublished.Value = ebook.PublishDate; 
        txtCategory.Text = ebook.Category; 
    } 
} 

Lastly, when the book selected is the one you wish to read, click on the Read eBook button to open the file location of the selected eBook:

private void btnReadEbook_Click(object sender, EventArgs e) 
{ 
    string filePath = txtFilePath.Text; 
    FileInfo fi = new FileInfo(filePath); 
    if (fi.Exists) 
    { 
        Process.Start(Path.GetDirectoryName(filePath)); 
    } 
} 

This completes the code logic contained in the eBookManager application.

Note

You can further modify the code to open the required application for the selected eBook instead of just the file location. In other words, if you click on a PDF document, the application can launch a PDF reader with the document loaded. Lastly, note that the classification has not been implemented in this version of the application.

It is time to fire up the application and test it out.