转换为.Net 2.0中的通用存储库模式

时间:2012-06-19 11:56:39

标签: c# .net oop design-patterns domain-driven-design

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

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

  2. 如果我将MyOracleReservationRepository和MyOracleBookRepository设为DataContracts,我是否应该将其作为WCF服务? (业务层将由服务层调用)

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

  4. CODE

    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. Advantage of creating a generic repository vs. specific repository for each object?

2 个答案:

答案 0 :(得分:0)

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

请仔细阅读以下网址,

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

答案 1 :(得分:0)

你无法将DAL“转换”为存储库,就像魔法一样。

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

详细了解MSDN http://msdn.microsoft.com/en-us/library/ff649690.aspx

中的存储库模式