Exemplo n.º 1
0
 /// <summary>
 /// this method loads the associated book and author into the
 /// junction class writing
 /// if the item is already loaded, it does not load it a second time
 /// </summary>
 /// <param name="writing"></param>
 public void LoadBorrowingItems(TypeCBorrowing writing)
 {
     if (null != writing)
     {
         if (null == writing.theBorrower)
         {
             BorrowerDAL dal = new BorrowerDAL(_connection);
             writing.theBorrower = dal.BorrowerFindByID(writing.BorrowerID);
         }
         if (null == writing.theBook)
         {
             BookDAL bookdal = new BookDAL(_connection);
             writing.theBook = bookdal.BookFindByID(writing.BookID);
         }
     }
 }
Exemplo n.º 2
0
        // TypeC Junction table does not have updates.  to update
        // delete and re add

        public List <TypeCBorrowing> TypeCBorrowingGetBooksRelatedToBorrowerID(int borrowerID, int Skip, int Take)
        {
            List <TypeCBorrowing> actualrv = new List <TypeCBorrowing>();
            List <Book>           rv       = new List <Book>();

            try
            {
                EnsureConnected();
                using (IDbCommand command = _connection.CreateCommand())
                {
                    // configure the command object
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "TypeCbooksGetRelatedToBorrowerID";

                    IDbDataParameter p = command.CreateParameter();
                    p.ParameterName = "@Skip";
                    p.DbType        = DbType.Int32;
                    p.Direction     = ParameterDirection.Input;
                    p.Value         = Skip;
                    command.Parameters.Add(p);
                    // since p has been added to the parameters collection
                    // it is safe to reuse the same variable
                    // for a new parameter
                    p = command.CreateParameter();
                    p.ParameterName = "@Take";
                    p.DbType        = DbType.Int32;
                    p.Direction     = ParameterDirection.Input;
                    p.Value         = Take;
                    command.Parameters.Add(p);
                    // since p has been added to the parameters collection
                    // it is safe to reuse the same variable
                    // for a new parameter
                    p = command.CreateParameter();
                    p.ParameterName = "@borrowerID";
                    p.DbType        = DbType.Int32;
                    p.Direction     = ParameterDirection.Input;
                    p.Value         = borrowerID;
                    command.Parameters.Add(p);

                    // load the data from the database
                    using (IDataReader reader = command.ExecuteReader())
                    {
                        // the mapper is constructed, and this is where the shape
                        // is validated to insure it is correct
                        // if the database structure changes,
                        // an exception will be thrown
                        // the less efficient Get Ordinal methods are only
                        // invoked one time per command
                        // this less efficient (but required) code
                        // is outside the loop
                        BookMapper mapper = new BookMapper(reader);

                        while (reader.Read())
                        {
                            Book a = mapper.ToBook(reader);
                            // the mapper uses the much more efficient getXXX
                            // methods internally to avoid boxing and
                            // string manipulation.  this more efficient code is
                            // inside the loop
                            if (a != null)
                            // if the mapper returns null for some reason it will
                            // be ignored
                            {
                                rv.Add(a);
                            }
                        }
                    }
                }
                BorrowerDAL dal      = new BorrowerDAL(_connection);
                Borrower    borrower = dal.BorrowerFindByID(borrowerID);
                if (null == borrower)
                {
                    throw new Exception($"cant find borrower with id of {borrowerID}");
                }
                foreach (Book b in rv)
                {
                    TypeCBorrowing borrowing = new TypeCBorrowing();
                    borrowing.BorrowerID  = borrower.BorrowerID;
                    borrowing.theBorrower = borrower;
                    borrowing.BookID      = b.BookID;
                    borrowing.theBook     = b;
                    actualrv.Add(borrowing);
                }
            }
            catch (Exception ex) when(Logger.Log(ex, "DAL"))
            {
            }
            return(actualrv);
        }