-1

I have the following library management system with very limited functionalities, as of now.

  1. Is the following code written in correct Repository Pattern?

  2. Should I make MyOracleReservationRepository and MyOracleBookRepository as DataContracts if I am making it as a WCF service? (Business Layer will be called by service layer)

  3. Can you please provide code that explains how to make it a Generic repository pattern?

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


}

READING

  1. Advantage of creating a generic repository vs. specific repository for each object?
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

2 Answers2

0

You have to learn repository pattern and convert yourself and ask questions/problems here to get the resolution.

Please go through following url for your reference,

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

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
0

You can't "convert" a DAL to repository as like magic.

You need read more about this pattern and beggin to do the code in this patter since the start.

Read more about Repository Pattern in MSDN http://msdn.microsoft.com/en-us/library/ff649690.aspx

MayogaX
  • 469
  • 3
  • 18