Introduction
This project demonstrates an ASP.NET Core Web API with a 3-tier architecture featuring CRUD operations for managing a collection of library books. The application is designed with a data access layer (DAL) employing the Iterator Design Pattern to encapsulate the iteration logic for the library book collection. The solution includes a data layer defining the LibraryBook model, a data access layer with a LibraryBookRepository implementing CRUD operations and the Iterator pattern, and a business logic layer (BLL) with a LibraryBookService. The API controllers in the presentation layer enable HTTP endpoints for adding, updating, deleting, and retrieving library books.
Implementing a complete CRUD functionality with the Iterator Design Pattern in an ASP.NET Core Web API with a 3-tier architecture involves several steps. I'll provide a simplified example to guide you through the process.
Step 1. Define the Model (Data Layer)
// Sardar Mudassar Ali Khan
// Models/LibraryBook.cs
public class LibraryBook
{
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
}
Step 2. Create Data Access Layer (DAL)
// DataAccessLayer/LibraryBookRepository.cs
using System.Collections;
using System.Collections.Generic;
public interface ILibraryBookIterator
{
LibraryBook Next();
bool HasNext();
}
public interface ILibraryBookRepository : IEnumerable<LibraryBook>
{
void Add(LibraryBook book);
void Update(LibraryBook book);
void Delete(int id);
LibraryBook GetById(int id);
ILibraryBookIterator CreateIterator();
}
public class LibraryBookRepositoryIterator: ILibraryBookIterator
{
private readonly List<LibraryBook> books;
private int position = 0;
public LibraryBookRepositoryIterator(List<LibraryBook> books)
{
this.books = books;
}
public LibraryBook Next()
{
var book = books[position];
position++;
return book;
}
public bool HasNext()
{
return position < books.Count;
}
}
public class LibraryBookRepository: ILibraryBookRepository
{
private List<LibraryBook> books;
public LibraryBookRepository()
{
books = new List<LibraryBook>();
}
public void Add(LibraryBook book)
{
books.Add(book);
}
public void Update(LibraryBook book)
{
var existingBook = GetById(book.Id);
if (existingBook != null)
{
// Update properties
existingBook.Title = book.Title;
existingBook.Author = book.Author;
// Update other properties as needed
}
}
public void Delete(int id)
{
var bookToRemove = GetById(id);
if (bookToRemove != null)
{
books.Remove(bookToRemove);
}
}
public LibraryBook GetById(int id)
{
return books.FirstOrDefault(b => b.Id == id);
}
public IEnumerator<LibraryBook> GetEnumerator()
{
return books.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public ILibraryBookIterator CreateIterator()
{
return new LibraryBookRepositoryIterator(books);
}
}
Cristopher CastilloPosted Nov 29, 2023, 8:21 AM
Hi Sardar. I am currently replicating your example, but in step 3, in : private readonly ILibraryBookRepository book repository; VS does not recognize. My question is: is there some more code missing from line 5 to line 10???I remain attentive to your comments. Greetings from Peru.