A Quick Code Review with the Benevolent Orchard Dictator Emeritus
I spent an hour with Bertrand Le Roy on the phone today. This cost me some money and was worth it. His facial expressions alone told me much of value about my code. I’m a lone developer. As such, I work mostly in a vacuum, so what I wanted was:
- Code Review
- Development Workflow Review
- Three or so areas of improvement on which to focus for the next three months or so.
Here are some notes on my take home message from the call. These notes may well only be useful to me. They are here for future reference:
There are three valid ways to render a projection/query in a custom shape layout.
-
render the content item
- increased control
- decreased flexibility
- increased reusability through alternates
- clean inversion of control through placement.info
- this is the canonical though not necessarily the best way
- if we’re going to do it this way, though, then why not just use one of the build in query layouts instead of a custom shape layout.
- this is the middle ground
- it has less flexibility than rendering the contentItem
- it is easier to understand, because of it has less inversion of control than does using @Display(shape)
Orchard uses deferred execution to create shapes from DriverResults.
- drivers use lambdas
- this delays execution
- Orchard will only execute the lambda if needed (e.g. placement)
- I.e. the driver result contains instructions for how to build a shape, and Orchard will later decide whether to run those instructions.
- so, just because we call a driver, doesn’t necessarily mean that we’re going to be making a database call
Ways to handle the limitations of projections & queries.
- whenever possible, create queries in the Orchard UI and leverage the built-in layouts
-
if you have a more complex query with unusual sorts/groupings, then take one of two approaches
- for smaller numbers of items, it’s just barely acceptable to group/sort them in memory – try to do the sorting/grouping in a controller, though, not in a view, and do it with as few database calls as possible
- for larger numbers of items, use a controller action and build the query with HQL – this will make things like pagination more effecient
Here are some areas for code improvement.
- My code is pretty clean. It is factored well. I have short methods.
- Focus on using Mini Profiler to refactor and reduce network calls. Projections, for instance, can be network heavy.
- Don’t uses Ticks as a uniqueId. Rather, increment a counter. Ticks is not unique – don’t do it.
- Err on the side of using var – you repeat yourself less.
- If I need a transient object (i.e. a bag of properties) to only use in the view, use anonymous objects (or ViewModels) instead of ExpandoObjects.
- Create shapes on the fly instead of using @helper methods in Orchard.
@Display.MyShape(MyProperty: 42, OtherProperty:43) @{ var shape = New.MyShape(...) } @Display(shape)