-1

到目前为止,我有以下功能非常有限的图书馆管理系统。

  1. 以下代码是否以正确的存储库模式编写?

  2. 如果我将其作为 WCF 服务,我应该将 MyOracleReservationRepository 和 MyOracleBookRepository 作为 DataContracts 吗?(业务层会被服务层调用)

  3. 您能否提供解释如何使其成为通用存储库模式的代码?

代码

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;
    }
 }


}

阅读

  1. 为每个对象创建通用存储库与特定存储库的优势?
4

2 回答 2

0

您必须学习存储库模式并转换自己并在这里提出问题/问题以获得解决方案。

请通过以下网址供您参考,

http://www.remondo.net/repository-pattern-example-csharp/

于 2012-06-19T12:10:42.047 回答
0

您不能像魔术一样将 DAL“转换”为存储库。

您需要阅读有关此模式的更多信息,并从一开始就开始编写此模式中的代码。

在 MSDN http://msdn.microsoft.com/en-us/library/ff649690.aspx中阅读有关存储库模式的更多信息

于 2012-06-19T12:34:32.587 回答