public IActionResult Get(int customerId)
        {
            var custId = CustomerId.Create(customerId);
            var cust   = _dao.GetById(custId);
            var dto    = DtoConverter.CustomerToDto(cust);

            return(Ok(dto));
        }
Пример #2
0
 /// <summary>
 /// Checks if a customer already exists with the same customer ID.
 /// </summary>
 private bool IsDuplicateOfExisting(Customer newCustomer, ICustomerDao customerDao)
 {
     // Whenever possible, I *really* don't like using assigned IDs.  I think they
     // should only be used when working with a legacy database.  Among other ugliness,
     // assigned IDs force us to try/catch when checking for duplicates because NHibernate
     // will throw an ObjectNotFoundException if no entity with the provided ID is found.
     // Consequently, we also have to have a reference to the NHibernate assembly from within
     // our business object.
     // To overcome these drawbacks, I'd recommend adding a DoesEntityExist(string assignedId)
     // method to the DAO to check for the existence of entities by its assigned ID.  This would remove the
     // ugly try/catch from this method and it would also remove the local dependency on the NHibernate
     // assembly.  I've chosen not to go ahead and do this because my assumption is that
     // the use of assigned IDs will be the exception rather than the norm...so I want to keep
     // the generic DAO as clean as possible for the example demo.
     try {
         Customer duplicateCustomer = customerDao.GetById(newCustomer.ID, false);
         return(duplicateCustomer != null);
     }
     // Only catch ObjectNotFoundException, throw everything else.
     catch (NHibernate.ObjectNotFoundException) {
         // Since the duplicate we were looking for wasn't found, then, through difficult
         // logical deduction, this object isn't a duplicat
         return(false);
     }
 }
Пример #3
0
 /// <summary>
 /// Checks if a customer already exists with the same customer ID.
 /// </summary>
 private bool IsDuplicateOfExisting(Customer newCustomer, ICustomerDao customerDao)
 {
     // Whenever possible, I *really* don't like using assigned IDs.  I think they
     // should only be used when working with a legacy database.  Among other ugliness,
     // assigned IDs force us to try/catch when checking for duplicates because NHibernate
     // will throw an ObjectNotFoundException if no entity with the provided ID is found.
     // Consequently, we also have to have a reference to the NHibernate assembly from within
     // our business object.
     // To overcome these drawbacks, I'd recommend adding a DoesEntityExist(string assignedId)
     // method to the DAO to check for the existence of entities by its assigned ID.  This would remove the
     // ugly try/catch from this method and it would also remove the local dependency on the NHibernate
     // assembly.  I've chosen not to go ahead and do this because my assumption is that
     // the use of assigned IDs will be the exception rather than the norm...so I want to keep
     // the generic DAO as clean as possible for the example demo.
     try {
         Customer duplicateCustomer = customerDao.GetById(newCustomer.ID, false);
         return duplicateCustomer != null;
     }
     // Only catch ObjectNotFoundException, throw everything else.
     catch (NHibernate.ObjectNotFoundException) {
         // Since the duplicate we were looking for wasn't found, then, through difficult
         // logical deduction, this object isn't a duplicat
         return false;
     }
 }
Пример #4
0
        public void TestGetById()
        {
            IDaoFactory  daoFactory  = new NHibernateDaoFactory();
            ICustomerDao customerDao = daoFactory.GetCustomerDao();

            Customer foundCustomer = customerDao.GetById(TestGlobals.TestCustomer.ID, false);

            Assert.AreEqual(TestGlobals.TestCustomer.CompanyName, foundCustomer.CompanyName);
        }
Пример #5
0
        public IActionResult GetById(int id, [FromServices] CustomerDb db)
        {
            Customer customer = _dao.GetById(id, db);

            if (customer == null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound, new { error = $"no entity found by id: {id}" }));
            }
            return(Ok(customer));
        }
Пример #6
0
    private void UpdateCustomer()
    {
        IDaoFactory  daoFactory  = new NHibernateDaoFactory();
        ICustomerDao customerDao = daoFactory.GetCustomerDao();

        // Now that we're about to update the customer, be sure to lock the entity
        Customer customerToUpdate = customerDao.GetById(hidCustomerID.Value, true);

        // Changes to the customer object will be automatically committed at the end of the HTTP request
        customerToUpdate.CompanyName = txtCompanyName.Text;
        customerToUpdate.ContactName = txtContactName.Text;
    }
Пример #7
0
    private void DisplayCustomerToEdit()
    {
        IDaoFactory  daoFactory  = new NHibernateDaoFactory();
        ICustomerDao customerDao = daoFactory.GetCustomerDao();

        // No need to lock the customer since we're just viewing the data
        Customer customerToEdit = customerDao.GetById(Request.QueryString["customerID"], false);

        ShowCustomerDetails(customerToEdit);
        ShowPastOrders(customerToEdit);
        ShowProductsOrdered(customerToEdit);
    }
Пример #8
0
 /// <summary>
 /// Checks if a customer already exists with the same customer ID.
 /// </summary>
 private bool IsDuplicateOfExisting(Customer customer)
 {
     try
     {
         Customer duplicateCustomer = customerDao.GetById(customer.ID, false);
         return(duplicateCustomer != null);
     }
     catch (ObjectNotFoundException)
     {
         return(false);
     }
 }
Пример #9
0
        public void CanGetOrdersShippedTo()
        {
            IDaoFactory  daoFactory  = new NHibernateDaoFactory();
            ICustomerDao customerDao = daoFactory.GetCustomerDao();
            IOrderDao    orderDao    = daoFactory.GetOrderDao();

            Customer customer = customerDao.GetById(TestGlobals.TestCustomer.ID, false);
            // Give the customer its DAO dependency via a public setter
            IList <Order> ordersMatchingDate = orderDao.GetOrdersOrderedOn(customer, new DateTime(1998, 3, 10));

            Assert.AreEqual(1, ordersMatchingDate.Count);
            Assert.AreEqual(10937, ordersMatchingDate[0].ID);
        }
Пример #10
0
 public void setCustomer(string customerId)
 {
     customer = customerDao.GetById(customerId, false);
 }