public static void CreateOrder(DTO.OrderDTO orderDTO) { var db = new pbDBEntities(); // orderDTO down to the next layer, and convert it to an entity. var order = new Order(); // Map the entities here. order.OrderId = orderDTO.OrderId; order.Size = orderDTO.Size; order.Crust = orderDTO.Crust; order.Sausage = orderDTO.Sausage; order.Pepperoni = orderDTO.Pepperoni; order.Green_Peppers = orderDTO.Green_Peppers; order.Onions = orderDTO.Onions; order.Name = orderDTO.Name; order.Address = orderDTO.Address; order.Phone = orderDTO.Phone; order.Zip = orderDTO.Zip; order.PaymentType = orderDTO.PaymentType; order.TotalCost = orderDTO.TotalCost; order.Completed = orderDTO.Completed; // try to add the new order values, and save the changes. try { db.Orders.Add(order); db.SaveChanges(); } catch (Exception ex) { throw ex; } }
/* * /// <summary> * /// * /// An order repository should contain appropriate referencse to the database. * /// The way it accomplishes this by storing those references to a variable. * /// For clarity's sake: In said variable is an instance of the pbDBEntities: * /// Being, precisely that; A "Model" of the entities in my PapaBobs database. * /// * /// This repository is where new orders are created. It accomplishes this by calling * /// an instance of the papa bobs data base entity model, converting those entities to * /// something that can be passed along (the DTO). Then add the newly converted values to Orders, * /// and finish by saving those chanegs to be printed later in the Order Manager (PapaBobs.Domain). * /// NOTE: Adding the newly converted values to "Orders" (which is the Database Context!) means to * /// update them to whatever the customer's real order is. Then they (newly converted values) can * /// be referenced by the Order Manager, which displays them each time. So the PB.Domain is not * /// receiving orders and updating databases, that is done here, in convertToEntity. PB.Domain just * /// establishes ties to lower layers to reach into and call up information, to pass along. * /// * /// * /// This repository should also connect things between layers. * /// A.K.A. using a DTO (Data Transfer Object). * /// * /// My DTO contains a definition for all of the properties found in an instance of my * /// Entity Data Model (called "pbDBEntities"). The ADO.NET Technology provides communication * /// between relational and non-relational systems through a common set of components, * /// (en.wikipedia.org/wiki/ADO.NET). * /// So, my DTO acts as a middle-man between layers to pass things up / down, depending on concerns. * /// * /// The way this repository should connect things between layers, is by the convertToEntity() method. * /// Wherein, I have a new instance of Order(an object generated by my EDM, w/ references to my Orders * /// database). The new Order instance's values, are then passed to (or stored to) my data transfer object. * /// * /// </summary> */ // Function that takes the "Orders" data base values and organizes them, // and converts / stores them to a list. public static List <DTO.OrderDTO> GetOrders() { // instance of the PapaBobs DB Entities pbDBEntities db = new pbDBEntities(); // Get those DB entities, put them in dbOrders variable, and sort them. var dbOrders = db.Orders.OrderBy(p => p.Name).ToList(); // Make a new list of what order info is stored in the DTO. var dtoOrders = new List <DTO.OrderDTO>(); // Iterate through that list, for each DTO order item, // store it in this local instance of the database's entities. // For clarity: for each entity in the pbDBEntities, // get the pbDBEntity value, give it to the iterater variable (dbOrder) // and then the iterater variable stores that value to a new instance of // the DTO, called dtoOrder! This is where values are passed up the "spike". foreach (var dbOrder in dbOrders) { var dtoOrder = new DTO.OrderDTO(); dtoOrder.OrderId = dbOrder.OrderId; dtoOrder.Name = dbOrder.Name; dtoOrder.Address = dbOrder.Address; dtoOrder.Sausage = dbOrder.Sausage; dtoOrder.Size = dbOrder.Size; dtoOrder.Zip = dbOrder.Zip; dtoOrder.Phone = dbOrder.Phone; dtoOrder.TotalCost = dbOrder.TotalCost; dtoOrder.Completed = dbOrder.Completed; dtoOrder.PaymentType = dbOrder.PaymentType; dtoOrder.Crust = dbOrder.Crust; dtoOrder.Pepperoni = dbOrder.Pepperoni; dtoOrder.Onions = dbOrder.Onions; dtoOrder.Green_Peppers = dbOrder.Green_Peppers; // Finish by, for each DTO.OrderDTO list item, add the value of dtoOrder. dtoOrders.Add(dtoOrder); } return(dtoOrders); }