I altered an application that used procedural programming and changed it to an object oriented program. The application would load orders from a file and then compare those orders from a database. If the order was found in one and not the other then it would show that order. The problem with this is the orders from the file have to be modified a little bit to ensure that the data is able to correctly match up with the databases data. The file gets loaded into a datatable object just like the orders from the database. However, the fields aren't the exact same. The name of the commodity on one doesn't match the name of the commodity on the other. An option order comes from the file with a field "OPTION" and from the database it's a 1. Also, the data from the database comes in a different column and so on.
So the goal is to match them off, but to make it match it off in the easiest way possible. With the procedural programming it would work, however there would be column movements just before they get matched off. Some of these were coming as the field number and others were movements with column names. It really ended up becoming a huge hassle and a mess to make any corrections. A movement of one column made all of the other columns move and so it was kind of like a Voodoo programmer stepped in and made it work.
I'm not that smart of a guy to keep track of all of these moving pieces. So in comes object oriented programming. The first thing I did was to create an interface called IPrelimOrder. This interface was nothing, but a collection of properties.
Code Snippet
- Public Interface IPrelimOrder
- Inherits IComparable(Of IPrelimOrder)
- <DisplayName("Trade Date")>
- ReadOnly Property TradeDate As String
- <DisplayName("Commodity")>
- ReadOnly Property Commodity As String
- <Browsable(False)>
- ReadOnly Property CommodityConversion As String
- ReadOnly Property Contract As String
- Property Price As String
- ReadOnly Property Buys As Integer
- ReadOnly Property Sells As Integer
- <DisplayName("Option Type")>
- ReadOnly Property OptionType As String
- Property Strike As Double
- <DisplayName("Account")>
- ReadOnly Property AccountNumber As String
- <DisplayName("Source")>
- ReadOnly Property DataSource As String
- <DisplayName("Ticket #")>
- ReadOnly Property TicketNumber As String
- Property Checked As Boolean
- <Browsable(False)>
- Property DataRow As DataRow
- <Browsable(False)>
- ReadOnly Property OMNI As String
- <Browsable(False)>
- ReadOnly Property FirmID As String
- End Interface
Then I created a class called DatabaseOrder and FileOrder that implemented my newly created IPrelimOrder. No more column moves. The other benefit to doing it this way is now I can store all of this data into a list(Of IPrelimOrder). If I need to know whether this order came from the file or from the database I simply check the type. I no longer have to guess and move things around and pray it doesn't create another bug. When I get the contract name from a file, I can do a quick query to get the correct name. I don't have to worry about getting the correct name and then moving the column to a different spot to only find out it got the name of the wrong column which threw an exception.
Later on I do internal queries on this same list(Of IPrelimOrder) where I only want to lookup orders from the file. I can do the following.
Code Snippet
- Orders.OfType("FileOrder")
You can separate the two using procedural programming, but it's so much simpler this way.
If you have any questions or problems or see something wrong, please leave a message.
No comments:
Post a Comment