/// <summary>
        /// Creation of a service instance.
        /// </summary>
        /// <param name="bookStorage">Book storage.</param>
        /// <exception cref="ArgumentNullException">
        /// The exception that is thrown when a null argument <paramref name="bookStorage"/> is passed.
        /// </exception>
        public BookListService(IBookStorage bookStorage)
        {
            _bookStorage = bookStorage ?? throw new ArgumentNullException(nameof(bookStorage));
            _logger      = LoggerCreater.GetLogger(nameof(BookListService));

            try
            {
                _books.AddRange(_bookStorage.GetBooks().Select(bookDal => bookDal.ToBook()));
            }
            catch (BookStorageException e)
            {
                throw new BookServiceException("An error occurred while reading from the repository.", e);
            }

            _logger.Info("Service was created");
        }
예제 #2
0
        public static void Main()
        {
            _logger = LoggerCreater.GetLogger(nameof(Program));

            try
            {
                IBookStorage bookRepository = new BookBinaryStorage(FilePath);
                IBookService bookService    = new BookListService(bookRepository);

                Console.WriteLine("AddTest");
                Console.WriteLine(new string('-', 80));
                AddTest(bookService);

                Console.WriteLine("FindTest");
                FindTest(bookService);

                Console.WriteLine("DeleteTest");
                DeleteTest(bookService);

                Console.WriteLine("SortTest");
                SortTest(bookService);

                Console.WriteLine("FormatTest");
                FormatTest(bookService);

                Console.WriteLine("FormatExtensionTest");
                FormatExtensionTest(bookService);

                var books = bookService.GetBooks();
                foreach (var book in books)
                {
                    bookService.RemoveBook(book);
                }

                bookService.AddBook(new Book("ISBN7", "Author1", "Title - 5", "PublishHouse 1", "2020", 140, 5m));
            }
            catch (Exception e)
            {
                _logger.Error("An error has occurred. See details", e);
            }

            Console.ReadLine();
        }
예제 #3
0
 /// <summary>
 /// Creation of a service instance.
 /// </summary>
 /// <param name="filePath">The path to the data file.</param>
 /// <exception cref="ArgumentNullException">
 /// The exception that is thrown when a null argument <paramref name="filePath"/> is passed.
 /// </exception>
 public BookBinaryStorage(string filePath)
 {
     _filePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
     _logger   = LoggerCreater.GetLogger(nameof(BookBinaryStorage));
     _logger.Info("Storage was created");
 }