In-Line Props
February 28, 2012 1 Comment
I am working on a brownbag for entity framework using Northwind as my database. When coding up a POCO, I decided to use the in-line property setting available since VS2008:
Product product = new Product() { ProductId = (Int32)sqlDataReader[0], ProductName = (String)sqlDataReader[1], Supplier = new Supplier(), Category = new Category(), QuantityPerUnit = (String)sqlDataReader[4], UnitPrice = (Int16)sqlDataReader[5], UnitsInStock = (Int16)sqlDataReader[6], UnitsOnOrder = (Int16)sqlDataReader[7], ReorderLevel = (Int16)sqlDataReader[8], Discontinued = (Boolean)sqlDataReader[9] };
The problem is that when I ran it, one of the properties was cast incorrectly. However, by using in-line, I can’t tell which one is the problem.
If I did it the OFW (Old-Fashion Way) like this":
Product product = new Product(); product.ProductId = (Int32)sqlDataReader[0]; product.ProductName = (String)sqlDataReader[1]; product.Supplier = new Supplier(); product.Category = new Category(); product.QuantityPerUnit = (String)sqlDataReader[4]; product.UnitPrice = (Decimal)sqlDataReader[5]; product.UnitsInStock = (Int16)sqlDataReader[6]; product.UnitsOnOrder = (Int16)sqlDataReader[7]; product.ReorderLevel = (Int16)sqlDataReader[8]; product.Discontinued = (Boolean)sqlDataReader[9];
I would have had the break on 1 line and easily identified the offending line of code:
The morale of the story is the same as many of my other stories – separate – don’t be lazy.
I’ll try to help you spread that new acronym “OFW”, I like it! I mean to think, people used to spell stuff out, but that was the OFW! Haha!