Create a library of my books or books I want to read...
Properties of a book
Let's start...
Program.cs
file accordingly
BookModel.cs
class into it
namespace AbcItNetFramework.Exercises.Models
{
internal class BookModel
{
public string Name { get; set; }
public string Author { get; set; }
public string Genre { get; set; }
public string Price { get; set; }
public string ImgUrl { get; set; }
public string About { get; set; }
}
}
using System;
using AbcItNetFramework.Exercises.Models;
namespace AbcItNetFramework.Exercises
{
internal class MyLibrary
{
public void Run()
{
var book = new BookModel();
Console.WriteLine("Enter the name of the Book:");
book.Name = Console.ReadLine();
Console.WriteLine("Enter the author:");
book.Author = Console.ReadLine();
Console.WriteLine("Enter the genre:");
book.Genre = Console.ReadLine();
Console.WriteLine("Enter the price:");
book.Price = Console.ReadLine();
Console.WriteLine("Enter the imgage:");
book.ImgUrl = Console.ReadLine();
Console.WriteLine("Enter the description:");
book.About = Console.ReadLine();
Console.WriteLine(book);
Console.ReadKey(true);
}
}
}
namespace AbcItNetFramework.Exercises.Models
{
internal class BookModel
{
public string Name { get; set; }
public string Author { get; set; }
public string Genre { get; set; }
public string Price { get; set; }
public string ImgUrl { get; set; }
public string About { get; set; }
public override string ToString()
{
return $"Name: {Name}, Author: {Author}, Genre: {Genre}, Price: { Price}, Image: {ImgUrl}, About: {About}";
}
}
}
using System;
using System.Collections.Generic;
using AbcItNetFramework.Exercises.Models;
namespace AbcItNetFramework.Exercises
{
internal class MyLibrary
{
public void Run()
{
var books = new List<BookModel>();
var stopEnteringBooks = false;
while (!stopEnteringBooks)
{
var book = new BookModel();
Console.WriteLine("Enter the name of the Book:");
book.Name = Console.ReadLine();
Console.WriteLine("Enter the author:");
book.Author = Console.ReadLine();
Console.WriteLine("Enter the genre:");
book.Genre = Console.ReadLine();
Console.WriteLine("Enter the price:");
book.Price = Console.ReadLine();
Console.WriteLine("Enter the imgage:");
book.ImgUrl = Console.ReadLine();
Console.WriteLine("Enter the description:");
book.About = Console.ReadLine();
books.Add(book);
Console.WriteLine("Continue entering books? Y/N");
var continueEntering = Console.ReadLine();
stopEnteringBooks = continueEntering == "N";
Console.WriteLine("--------------");
}
Console.WriteLine("My Library:");
foreach (var book in books)
{
Console.WriteLine();
Console.WriteLine(book);
}
Console.ReadKey(true);
}
}
}
[
{
"Name": "Armagedon",
"Author": "Henry O'Neil",
"Genre": "Fantasy",
"Price": "$73",
"About": "Life after the end of civilization"
},
{
"Name": "Paradise",
"Author": "Marvin O'Harra",
"Genre": "Romance",
"Price": "$15",
"About": "The book about perfect relationship"
},
{
"Name": "ABC IT",
"Author": "ACTUM company",
"Genre": "Education",
"Price": "$120",
"About": "Teach yourself how to create applications"
}
]
private List<BookModel> LoadBooks()
{
if (!File.Exists("MyLibrary.json"))
{
return new List<BookModel>();
}
var booksJson = File.ReadAllText("MyLibrary.json");
var books = JsonConvert.DeserializeObject<List<BookModel>>(booksJson);
return books;
}
all occurrences
- name it
SerializationPath
using System;
using System.Collections.Generic;
using System.IO;
using AbcItNetFramework.Exercises.Models;
using Newtonsoft.Json;
namespace AbcItNetFramework.Exercises
{
internal class MyLibrary
{
private const string SerializationPath = "MyLibrary.json";
public void Run()
{
List<BookModel> books = LoadBooks();
var stopEnteringBooks = false;
while (!stopEnteringBooks)
{
var book = new BookModel();
Console.WriteLine("Enter the name of the Book:");
book.Name = Console.ReadLine();
Console.WriteLine("Enter the author:");
book.Author = Console.ReadLine();
Console.WriteLine("Enter the genre:");
book.Genre = Console.ReadLine();
Console.WriteLine("Enter the price:");
book.Price = Console.ReadLine();
Console.WriteLine("Enter the description:");
book.About = Console.ReadLine();
books.Add(book);
Console.WriteLine("Continue entering books? Y/N");
var continueEntering = Console.ReadLine();
stopEnteringBooks = continueEntering == "N";
Console.WriteLine("--------------");
}
Console.WriteLine("My Library:");
foreach (var book in books)
{
Console.WriteLine();
Console.WriteLine(book);
}
SaveBooks(books);
Console.ReadKey(true);
}
private List<BookModel> LoadBooks()
{
if (!File.Exists(SerializationPath))
{
return new List<BookModel>();
}
var booksJson = File.ReadAllText(SerializationPath);
var books = JsonConvert.DeserializeObject<List<BookModel>>(booksJson);
return books;
}
private void SaveBooks(List<BookModel> books)
{
var result = JsonConvert.SerializeObject(books, Formatting.Indented);
File.WriteAllText(SerializationPath, result);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using AbcItNetFramework.Exercises.Models;
using Newtonsoft.Json;
namespace AbcItNetFramework.Exercises
{
internal class MyLibrary
{
private const string SerializationPath = "MyLibrary.json";
public void Run()
{
List<BookModel> books = LoadBooks();
ListAllBooks(books);
var stopEnteringBooks = false;
while (!stopEnteringBooks)
{
CreateNewBook(books);
Console.WriteLine("Continue entering books? Y/N");
var continueEntering = Console.ReadLine();
stopEnteringBooks = continueEntering == "N";
Console.WriteLine("--------------");
}
ListAllBooks(books);
SaveBooks(books);
Console.ReadKey(true);
}
private static void ListAllBooks(List<BookModel> books)
{
Console.WriteLine("My Library:");
foreach (var book in books)
{
Console.WriteLine();
Console.WriteLine(book);
}
Console.WriteLine();
}
private static void CreateNewBook(List<BookModel> books)
{
var book = new BookModel();
Console.WriteLine("Enter the name of the Book:");
book.Name = Console.ReadLine();
Console.WriteLine("Enter the author:");
book.Author = Console.ReadLine();
Console.WriteLine("Enter the genre:");
book.Genre = Console.ReadLine();
Console.WriteLine("Enter the price:");
book.Price = Console.ReadLine();
Console.WriteLine("Enter the description:");
book.About = Console.ReadLine();
books.Add(book);
}
private List<BookModel> LoadBooks()
{
if (!File.Exists(SerializationPath))
{
return new List<BookModel>();
}
var booksJson = File.ReadAllText(SerializationPath);
var books = JsonConvert.DeserializeObject<List<BookModel>>(booksJson);
return books;
}
private void SaveBooks(List<BookModel> books)
{
var result = JsonConvert.SerializeObject(books, Formatting.Indented);
File.WriteAllText(SerializationPath, result);
}
}
}
void Run()
- we
call it interface
interfaces
and focus on classes and
inheritance
virtual
keyword
using System;
namespace AbcItNetFramework.Exercises
{
internal class ExerciseBase
{
public virtual void Run()
{
Console.WriteLine("Hello ABC IT!");
Console.ReadKey(true);
}
}
}
inherit
this class in all other exercise classes
like:
using AbcItNetFramework.Exercises;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
ExerciseBase exercise;
exercise = new ExerciseBase();
//exercise = new TimeCounter();
//exercise = new MathChallange();
//exercise = new MyLibrary();
exercise.Run();
}
}
}
nuget
Data
Db Browser for SQLite
and create new DB
MyLibrary
and add table Books
and properties
from the BookModel
Id
into the Model class
serialization
with database
JsonStorage
under the new folder Storage
serialization
logic into the new class
public
(to be visible outside)
using System.Collections.Generic;
using System.IO;
using AbcItNetFramework.Exercises.Models;
using Newtonsoft.Json;
namespace AbcItNetFramework.Exercises.Storage
{
internal class JsonStorage
{
private const string SerializationPath = "MyLibrary.json";
public List<BookModel> LoadBooks()
{
if (!File.Exists(SerializationPath))
{
return new List<BookModel>();
}
var booksJson = File.ReadAllText(SerializationPath);
var books = JsonConvert.DeserializeObject<List<BookModel>>(booksJson);
return books;
}
public void SaveBooks(List<BookModel> books)
{
var result = JsonConvert.SerializeObject(books, Formatting.Indented);
File.WriteAllText(SerializationPath, result);
}
}
}
using System;
using System.Collections.Generic;
using AbcItNetFramework.Exercises.Models;
using AbcItNetFramework.Exercises.Storage;
namespace AbcItNetFramework.Exercises
{
internal class MyLibrary : ExerciseBase
{
public override void Run()
{
var storage = new JsonStorage();
List<BookModel> books = storage.LoadBooks();
ListAllBooks(books);
var stopEnteringBooks = false;
while (!stopEnteringBooks)
{
CreateNewBook(books);
Console.WriteLine("Continue entering books? Y/N");
var continueEntering = Console.ReadLine();
stopEnteringBooks = continueEntering == "N";
Console.WriteLine("--------------");
}
ListAllBooks(books);
storage.SaveBooks(books);
Console.ReadKey(true);
}
private static void ListAllBooks(List<BookModel> books)
{
Console.WriteLine("My Library:");
foreach (var book in books)
{
Console.WriteLine();
Console.WriteLine(book);
}
Console.WriteLine();
}
private static void CreateNewBook(List<BookModel> books)
{
var book = new BookModel();
Console.WriteLine("Enter the name of the Book:");
book.Name = Console.ReadLine();
Console.WriteLine("Enter the author:");
book.Author = Console.ReadLine();
Console.WriteLine("Enter the genre:");
book.Genre = Console.ReadLine();
Console.WriteLine("Enter the price:");
book.Price = Console.ReadLine();
Console.WriteLine("Enter the description:");
book.About = Console.ReadLine();
books.Add(book);
}
}
}
DbStorage
should behave just the same way as the
current JsonStorage
from outside - meaning both storages have
to have the same interface
- so extract it.
IStorage
(We can see that inteface
is similar to class - but it has
methods without
implementation)
DbStorage
class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbcItNetFramework.Exercises.Models;
namespace AbcItNetFramework.Exercises.Storage
{
internal class DbStorage : IStorage
{
public List<BookModel> LoadBooks()
{
throw new NotImplementedException();
}
public void SaveBooks(List<BookModel> books)
{
throw new NotImplementedException();
}
}
}
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using AbcItNetFramework.Exercises.Models;
namespace AbcItNetFramework.Exercises.Storage
{
internal class DbStorage : IStorage
{
const string dbPath = @"..\..\Data\MyLibrary.db";
readonly string connectionString = $@"Data Source={dbPath};Version=3;";
public List<BookModel> LoadBooks()
{
var result = new List<BookModel>();
try
{
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
var command = new SQLiteCommand("SELECT id, Name, Author, Genre, Price, About FROM Books", connection);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var id = (int)reader["id"];
var name = reader["Name"].ToString();
var author = reader["Author"].ToString();
var genre = reader["Genre"].ToString();
var price = reader["Price"].ToString();
var about = reader["About"].ToString();
result.Add(
new BookModel
{
Id = id,
Name = name,
Author = author,
Genre = genre,
Price = price,
About = about
});
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return result;
}
public void SaveBooks(List<BookModel> books)
{
try
{
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
var query = "INSERT INTO Books (Name, Author, Genre, Price, About) VALUES (@Name, @Author, @Genre, @Price, @About)";
foreach (var book in books)
{
if (book.Id != 0)
{
continue;
}
using (var command = new SQLiteCommand(query, connection))
{
// Use parameters to prevent SQL injection
command.Parameters.AddWithValue("@Name", book.Name);
command.Parameters.AddWithValue("@Author", book.Author);
command.Parameters.AddWithValue("@Genre", book.Genre);
command.Parameters.AddWithValue("@Price", book.Price);
command.Parameters.AddWithValue("@About", book.About);
// Execute the command
command.ExecuteNonQuery();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
constructor
long
(two places)
Now you can switch between sorages by creating different instances in constructor. You can create as many as you want of other implementations of the IStorage