“Horking

 

HORK Sample: Creating data with HORK

There are two ways to create data using HORK.
The first way to create data is to create instances of each entity, and assign values.
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";
            try
            {
                contact.Create();
            }
            catch (SqlException)
            {
                // Any database-related exception will occur here
            }
            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";
            try
            {
                address.Create();
            }
            catch (SqlException)
            {
                // Any database-related exception will occur here
            }
            Order order = new Order();
            order.OrderId = Guid.NewGuid();
            order.OrderContactId = contact.ContactId;
            try
            {
                order.Create();
            }
            catch (SqlException)
            {
                // Any database-related exception will occur here
            }
            OrderItem orderItem = new OrderItem();
            orderItem.OrderItemId = Guid.NewGuid();
            orderItem.OrderId = order.OrderId;
            orderItem.ItemNumber = "77765";
            orderItem.ItemDescription = "Red Socks";
            orderItem.Quantity = 24;
            try
            {
                orderItem.Create();
            }
            catch (SqlException)
            {
                // Any database-related exception will occur here
            }
            orderItem = new OrderItem();
            orderItem.OrderItemId = Guid.NewGuid();
            orderItem.OrderId = order.OrderId;
            orderItem.ItemNumber = "77402";
            orderItem.ItemDescription = "Technical Book";
            orderItem.Quantity = 1;
            try
            {
                orderItem.Create();
            }
            catch (SqlException)
            {
                // Any database-related exception will occur here
            }
            

You may notice that we created the entities in a certain order. This was done to honor the foreign keys and prevent INSERT errors.
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 make everything work properly.
When we look in the database we can see all the data we just created: