“Horking

 

HORK Sample: Creating data with HORK

There are two ways to create data using HORK.
The second way to create data is to create instances of each entity and add them to the database table graph.
In the following example, we will create a Contact with an Address and then create an Order for the contact with 2 OrderItems:

            Contact contact = new Contact();
            contact.ContactId = Guid.NewGuid();
            contact.FirstName = "Bob";
            contact.LastName = "Roberts";
            contact.Business = false;
            contact.MainPhone = "310-700-2123";
            contact.Email = "bob@bobroberts.org";

            Address address = new Address();
            address.AddressId = Guid.NewGuid();
            address.AddressContactId = contact.ContactId;
            address.Address1 = "742 Evergreen Terrace";
            address.City = "Springfield";
            address.PostalCode = "54321";
            address.Country = "United States";

			contact.AddAddress(address);

            Order order = new Order();
            order.OrderId = Guid.NewGuid();
            order.OrderContactId = contact.ContactId;

			contact.AddOrder(order);

            OrderItem orderItem = new OrderItem();
            orderItem.OrderItemId = Guid.NewGuid();
            orderItem.OrderId = order.OrderId;
            orderItem.ItemNumber = "77765";
            orderItem.ItemDescription = "Red Socks";
            orderItem.Quantity = 24;

			order.AddOrderItem(orderItem);
			
            orderItem = new OrderItem();
            orderItem.OrderItemId = Guid.NewGuid();
            orderItem.OrderId = order.OrderId;
            orderItem.ItemNumber = "77402";
            orderItem.ItemDescription = "Technical Book";
            orderItem.Quantity = 1;
			
			order.AddOrderItem(orderItem);
			
			try
            {
			    HORKTransaction transaction = new HORKTransaction();
                transaction.BeginTransaction();
                transaction.Add(HORK.Entities.EntityOperationTypes.SQLOperation.INSERT, contact);
                transaction.Commit();
            }
            catch (SqlException sqlex)
            {
                // Any database-related exception will occur here
            }
			catch (Exception ex)
            {
                // Any other exceptions will occur here
            }
            

When working with the entity table graph it is necessary to use HORKTransaction or HORKRepository to save the data. These classes understand how to execute queries in the proper sequence when wrapped in a transaction.
If you have used many ORM's you are aware that regardless of the underlying technology, the ORM programmer has to know the table relationships to understand how to manipulate the entity table graph.