到目前为止,我有以下功能非常有限的图书馆管理系统。
以下代码是否以正确的存储库模式编写?
如果我将其作为 WCF 服务,我应该将 MyOracleReservationRepository 和 MyOracleBookRepository 作为 DataContracts 吗?(业务层会被服务层调用)
您能否提供解释如何使其成为通用存储库模式的代码?
代码
namespace LibraryBL
{
public class ReservationManager
{
//LibraryDAL.ReservationDAL resDAL = new LibraryDAL.ReservationDAL();
//LibraryDAL.BookDAL bookDAL = new LibraryDAL.BookDAL();
LibraryRepository.IReservationRepository reservationRepository;
LibraryRepository.IBookRepository bookRepository;
public ReservationManager(LibraryRepository.IReservationRepository resRepositroy, LibraryRepository.IBookRepository bookRepositroy)
{
reservationRepository = resRepositroy;
bookRepository = bookRepositroy;
}
public List<LibraryDTO.Reservation> GetAllReservations()
{
List<LibraryDTO.Reservation> allReservations = reservationRepository.GetAllReservations();
//Book object inside allReservations has two values as NULL (author and BookTitile).
//These values need to be set using foreach loop
foreach (LibraryDTO.Reservation reservation in allReservations)
{
int bookID =reservation.ReservedBook.BookID;
LibraryDTO.Book book = bookRepository.GetBookByID(bookID);
reservation.ReservedBook = book;
}
return allReservations;
}
}
}
namespace LibraryRepository
{
public interface IReservationRepository
{
List<LibraryDTO.Reservation> GetAllReservations();
}
public interface IBookRepository
{
LibraryDTO.Book GetBookByID(int bookID);
}
public class MyOracleReservationRepository : IReservationRepository
{
public List<LibraryDTO.Reservation> GetAllReservations()
{
int databaseValueResID1 = 1;
DateTime databaseValueResDate1 = System.Convert.ToDateTime("1/1/2001");
int databaseValueResBookID1 = 101;
List<LibraryDTO.Reservation> reservations = new List<LibraryDTO.Reservation>();
LibraryDTO.Reservation res = new LibraryDTO.Reservation();
res.ReservationID = databaseValueResID1;
res.ReservedDate = databaseValueResDate1;
res.ReservedBook = new LibraryDTO.Book();
res.ReservedBook.BookID = databaseValueResBookID1;
res.ReservedBook.Author = null; //Set as null as we don't have values in Reservation DAL
res.ReservedBook.BookTitle = null; //Set as null as we don't have values in Reservation DAL
reservations.Add(res);
return reservations;
}
}
public class MyOracleBookRepository : IBookRepository
{
public LibraryDTO.Book GetBookByID(int bookID)
{
LibraryDTO.Book book = null;
if (bookID == 101)
{
book = new LibraryDTO.Book();
book.BookID = 101;
book.Author = "First Author";
book.BookTitle = "Book 1";
}
return book;
}
}
}
阅读