現在、機能が非常に制限された次のライブラリ管理システムがあります。
次のコードは正しいリポジトリ パターンで記述されていますか?
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;
}
}
}
読む