<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2629608947246129428</atom:id><lastBuildDate>Sat, 10 Oct 2026 09:12:01 +0000</lastBuildDate><category>Data Access Patterns</category><category>ADO.Net</category><category>Data Transfer Objects</category><category>Angry-Coders</category><category>Business Objects</category><category>DAL</category><category>DTO</category><category>BLL</category><category>C#</category><category>Business Layer</category><category>Custom Validation</category><category>Entity Framework</category><category>POCO</category><category>Clean Code</category><category>Domain Driven Design</category><category>High Performance</category><category>Refactoring</category><category>Fluent Interface</category><category>SOLID</category><category>TDD</category><category>DAO</category><category>Data Access Object</category><category>Null Coalescing Operator</category><category>Repository</category><category>Test Driven Development</category><category>aapl</category><title>Reference Bits</title><description>Thoughts on software development by Rudy Lacovara</description><link>http://www.referencebits.com/</link><managingEditor>noreply@blogger.com (Rudy Lacovara)</managingEditor><generator>Blogger</generator><openSearch:totalResults>51</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-8808256825172093651</guid><pubDate>Fri, 17 Feb 2017 19:25:00 +0000</pubDate><atom:updated>2017-02-25T22:09:25.872-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Clean Code</category><category domain="http://www.blogger.com/atom/ns#">Refactoring</category><title>Programmers could learn a thing or two from bridge engineers</title><description>&lt;img style=&quot;border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; padding-right: 0px&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNj9H7uGXtGeEdtoYv2cbKH89TpiqDVkmf7zvLfimDk1vmebEBH2qMV3a-fPbf15T9igilAWM81nZLHbfJUclNclIxou9XwwG_aRrpHvGUOt8qYFOBEiLDmaxkaLG2Nzx_Fko38IVArWI/s400/bridge.jpg&quot; width=&quot;640&quot; height=&quot;254&quot;&gt;  &lt;p&gt;I was recently watching a documentary that described the engineering and design of bridges.&amp;nbsp; These bridges, some of them 80 years old, have been in constant use from the moment they were built. &lt;/p&gt; &lt;p&gt;There really was no cookie cutter recipe.&amp;nbsp; For the most part, each bridge was a custom design job that resulted from the engineering best practices at the time it was designed, the available materials, and the opinions of the engineers.&amp;nbsp; Sounds a lot like the software development.&lt;/p&gt; &lt;p&gt;One thing that didn’t sound like software at all was the attitude toward maintenance.&amp;nbsp; The bridge engineers all had respect for the original design and applied their skills to maintain it.&amp;nbsp; They would incorporate new techniques and materials where it supported the intent of the original designers, but there was no push to “refactor” the bridge.&amp;nbsp; As a result the bridges keep working, fulfilling their original purpose and providing value every day.&lt;/p&gt; &lt;p&gt;Imagine how this might be different if each new engineer on the project came in and said “This bridge design is crap.&amp;nbsp; I’m surprised it even works.&amp;nbsp; We know so much more about the right way to build a bridge now.&amp;nbsp; We’re going to have to gut this architecture and update it to implement current best practices.”&amp;nbsp; &lt;/p&gt; &lt;p&gt;How long do you think that bridge would remain in service?&amp;nbsp; Even worse, how reliable do you think that bridge would be after a new set of engineers came in and tried to implement a brand new architecture, producing a mish-mash of the old and the new.&lt;/p&gt; &lt;p&gt;It sounds like an obviously bad idea when talking about bridges, but for some reason we don’t give it a second thought in the software world.&amp;nbsp; We have a chronic lack of respect for those who came before us. We’re constantly rewriting code &lt;strong&gt;THAT IS WORKING&lt;/strong&gt; for no better reason than we don’t like the design.&lt;/p&gt; &lt;p&gt;I can look back at my career and see multiple instances where I’ve been the chief refactor offender, rewriting code that was working, even entire application architectures because I thought they were written poorly.&amp;nbsp; It’s easy to do.&amp;nbsp; There’s no shortage of legitimately bad code or developers who rush to implement the latest tools and ideas without really understanding what they’re doing.&lt;/p&gt; &lt;p&gt;These days I’m a little older and a little wiser. I try to keep 2 important principles in mind&lt;/p&gt; &lt;p&gt;&lt;strong&gt;1.&amp;nbsp; Software is not an end unto itself.&lt;/strong&gt;&amp;nbsp; &lt;br&gt;Our end customers don’t really care if our services are perfectly RESTful, or if we’ve properly implemented dependency injection throughout our app.&amp;nbsp; They care if it works.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;2.&amp;nbsp; There’s always a reason the developer who came before me did things the way they did.&amp;nbsp; &lt;br&gt;&lt;/strong&gt;Here’s a cycle I’ve gone through countless times.&amp;nbsp; I see code that works but it’s stupidly implemented, what was this idiot thinking.&amp;nbsp; It’s going to take me an extra hour but let me fix this nonsense.&amp;nbsp; Ok I’m pretty much finished and wait, this key use case doesn’t work.&amp;nbsp; I can fix it, but now the rest of my refactor is broken.&amp;nbsp; Well if I do this and this it will work, but…. oh… it pretty much works the same way it did to start with.&amp;nbsp; &lt;/p&gt;</description><link>http://www.referencebits.com/2017/02/programmers-could-learn-thing-or-two.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNj9H7uGXtGeEdtoYv2cbKH89TpiqDVkmf7zvLfimDk1vmebEBH2qMV3a-fPbf15T9igilAWM81nZLHbfJUclNclIxou9XwwG_aRrpHvGUOt8qYFOBEiLDmaxkaLG2Nzx_Fko38IVArWI/s72-c/bridge.jpg" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-516347414859498031</guid><pubDate>Mon, 06 Apr 2015 17:41:00 +0000</pubDate><atom:updated>2015-04-06T11:43:21.212-06:00</atom:updated><title>Core Frameworks Don’t Work (most of the time)</title><description>&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrLdGChjfmz80kZDlot-99BrqFMo94OW8Jd8XLsdwntckfhjinj1rarfrDlUN4QZVLkjmC7Obbl5geaCDeLBRJtohL3DqJvvaxUsdtDXCvjmy09ixctMQ9oMrggO98NapqRzEHqRIc620/s1600-h/assimilate4.jpg&quot;&gt;&lt;img title=&quot;assimilate&quot; style=&quot;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&quot; border=&quot;0&quot; alt=&quot;assimilate&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG4-Pk9rRZttSZEvvjpK21mQK6AwtCST0rc0Icoa8H7MEpBtIoqX2dRk2mou7kimUoEHtOUVHyrdiomUNVfwwyF88OIVmKUujUa7bZRBtI8RhkRKwPrOZgI6JDjXnVi9FmCu8JO-a0nJg/?imgmax=800&quot; width=&quot;650&quot; height=&quot;382&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I work as a consultant, mostly for large software development organizations at public companies that maintain large systems that handle millions of transactions each day.&amp;#160; My typical engagement is at least 1 year, no longer than 2 years.&amp;#160; It’s long enough that I’m able to dig deep but it’s short enough that I get exposed to many different organizations.&amp;#160; This gives me a unique and informed perspective on how software development works at a fair number of .Net shops.&amp;#160; I’m kind of like Marlin Perkins, able to observe the natural behavior of cutting edge software development organizations in the wild.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;One interesting ritual that I’ve seen practiced over and over is rite of THE CORE FRAMEWORK.&amp;#160; Some organizations actually go as far at to call it THE FRAMEWORK.&amp;#160; Others are content to simply call it core.&amp;#160; Regardless of what you call it, it’s usually a terrible idea that will cripple the productivity of your organization. &lt;/p&gt;  &lt;h3&gt;A point of clarification&lt;/h3&gt;  &lt;p&gt;I want to be clear. This post is about company-wide core frameworks.&amp;#160; That’s a common framework that is used as the foundation for multiple applications across the company.&amp;#160; That is not the same as an application core library.&lt;/p&gt;  &lt;p&gt;An application core library is usually just the business layer for a single application that can be shared across different assemblies at the application layer.&amp;#160; For example, the SquareHire core library is used for the SquareHire public &lt;a href=&quot;https://www.squarehire.com&quot;&gt;website&lt;/a&gt;, the SquareHire ATS (webapp), the SquareHire admin app (another web app), the SquareHire processor, and the SquareHire webapi. All of these are different assemblies that share a common data and service backend.&amp;#160; Together they make up the SquareHire application so it makes sense that they would share a common SquareHire &lt;strong&gt;core library&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;A company wide Core Framework is something different.&amp;#160; That’s when a company has multiple applications that do not share a common data and service backend.&amp;#160; They may have nothing to do with each other.&amp;#160; However, the company wants to standardize software architecture across the company so the decision is made to build every application on top of a common, company-wide Core Framework.&amp;#160; Often these Core Frameworks are named after the company, like AcmeInc.core.&amp;#160; These company-wide core frameworks force a common architecture and code base on apps that do NOT share a common data and service backend. These are the frameworks this post is about.&lt;/p&gt;  &lt;h3&gt;How does it happen?&lt;/h3&gt;  &lt;p&gt;A group of programmers build an application.&amp;#160; The application works, the company grows, and it hires more developers.&amp;#160; As time goes on the more features are added to the application by developers who really don’t understand the original designs. They just kind of bolt code on that works in the way they’re used to doing things.&amp;#160; The new code doesn’t follow the patterns established by the original designers, but it does accomplish the short term goal.&amp;#160; Besides, the new approach is probably better than the original design anyway. &lt;/p&gt;  &lt;p&gt;More time goes on.&amp;#160; The company becomes even more successful which means the app is supporting more users.&amp;#160; It’s now maintained by a completely new group of programmers, nobody’s really sure who the original developers were.&amp;#160; Each new wave of developers implements features using what they feel are the best practices.&amp;#160; The new features hang off the original application like tumors.&amp;#160; Growths that are powered by the original organism, but refuse to cooperate with it.&lt;/p&gt;  &lt;p&gt;Things go on this way. The company is now so successful that it is developing multiple applications, or maybe they’re acquired and merged with another organization and their software projects.&amp;#160; However it happens, the point is that we now have a situation where the organization has multiple teams maintaining multiple applications.&amp;#160; Each application has a different architecture and is becoming increasingly more difficult to maintain as new generations of programmers keep bolting stuff on that doesn’t work in the way the original app was designed.&lt;/p&gt;  &lt;p&gt;This is the moment when somebody has the bright idea “You know what would make our software development efforts much more efficient and would make all of our applications work much better… if we standardized on a single perfect architecture and made everybody in the entire organization adopt it.&amp;#160; In fact, we should create a set of Core libraries, and then every new project will be built on top of them.” &lt;/p&gt;  &lt;h3&gt;The theory of company-wide Core&lt;/h3&gt;  &lt;p&gt;On the surface, a Core Framework makes a lot of sense.&amp;#160; There are some good arguments for standardizing and implementing a common framework across your organization.&amp;#160; Here’s a few of them.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1. New development should be faster/easier&lt;/strong&gt;     &lt;br /&gt;New projects will be much easier to get off the ground. They won’t need to write plumbing code like persistence, they’ll get everything they need from the “Core”. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;2. Managing development resources (people) should be easier&lt;/strong&gt;     &lt;br /&gt;If you standardize it should be easier for developers to move between teams.&amp;#160; They’ll already be familiar with the architecture.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;3. The smartest guys can guide us&lt;/strong&gt;     &lt;br /&gt;Since the “Core” will be designed by the architect (or architects) it will embody the best possible architecture for the organization and it will enforce a set of best practices that will be implemented by less capable programmers who might otherwise go astray.&amp;#160; &lt;/p&gt;  &lt;p&gt;I may have overdone the sarcasm on that last one but the point is that creating a core really does seem like a good idea on paper.&lt;/p&gt;  &lt;h3&gt;The reality of company-wide Core&lt;/h3&gt;  &lt;p&gt;The theory sounds good, but the reality doesn’t work out that way.&amp;#160; Here’s some real world observations pulled from my time at real organizations that were in various stages of implementing Core libraries.&amp;#160; Keep in mind that the architects and developers at these organizations are some of the smartest that I’ve ever worked with.&amp;#160; The problem definitely was not a lack of skill or programming experience.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1. New development should be faster/easier      &lt;br /&gt;&lt;/strong&gt;The Core may actually get the initial phase of a new project off the ground a bit quicker, but it doesn’t last.&amp;#160; Quickly developers find that the Core brings with it limitations as well as benefits. It’s not long until the developers of the new application are scheduling time with the architect and the Core team to try and figure out how Core can be changed to accommodate the needs of this new application.&amp;#160; Did you catch that?&amp;#160; Not only does core not meet the needs of the new app, the new application actually winds up driving changes to Core. This happens for every application that consumes it.&amp;#160; What’s really been created is a massive cross-application dependency.&amp;#160; Nothing brightens my day like pulling the latest build of core and discovering that my entire application is now broken because some other team decided that we needed a better way to manage transactions or the DI container or any of the other things that find their way into these core libraries.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;2. Managing development resources (people) should be easier      &lt;br /&gt;&lt;/strong&gt;This benefit actually does happen to some extent. Different apps in the organization will be structured in a similar way so that it’s easier for developers to move between them.&amp;#160; Unfortunately this commonality comes with negative side effects.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;The worst effect is that software development becomes uniformly slow and difficult.&amp;#160; The Core is a cross application dependency that forces the decisions made by one team onto every other team in the organization.&amp;#160; This also means that when your team needs a change made to core, it’s not going to happen quickly.&amp;#160; You’re going to have to wait for buy in from the architects and other team leads. &lt;/p&gt;  &lt;p&gt;I’ve also seen Core negatively effect organizations’ ability to hire. Core usually goes hand in hand with a dramatic increase in complexity.&amp;#160; I’ve heard managers express that it’s incredibly difficult to find programmers and once they do it takes 5 or 6 months for them to be productive because the app architecture is just so complex.&amp;#160; Does the Core have to be that complex? No, but for some reason it always is… which brings us to number 3.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;3. The smartest guys can guide us      &lt;br /&gt;&lt;/strong&gt;The core is indeed designed by the smartest programmers in the organization.&amp;#160; This has to be a good thing right?&amp;#160; Wrong.&amp;#160; There are two big problems here.&amp;#160; &lt;/p&gt;  &lt;p&gt;First is the problem of misaligned incentives.&amp;#160; When a group of programmers is working together to ship a product, their incentives are all pretty much aligned.&amp;#160; They want to ship the product.&amp;#160; If the product doesn’t ship it’s a big problem for everyone.&amp;#160; When you take your smartest programmers and you pull them out of that team and tell them their priority is to build the core architecture that all other applications will be built on, you’ve just given them a different set of incentives.&amp;#160; You just told them that it’s not their job to ship product. Their job may be to create beautiful architecture or develop new techniques for modeling business domains, or researching new technologies, or to instruct developers who are shipping products on how they should be implementing the core.&amp;#160; The point is, incentives are no longer aligned.&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;The second problem is a phenomenon that I’ve labeled Smart Guy Disease.&amp;#160; This is another thing I’ve seen repeated in multiple organizations.&amp;#160; The basic idea is that the worst mistakes that have the most negative impact on an organization are consistently made by the “smart” programmers not the “dumb” programmers.&amp;#160;&amp;#160; If you want more detail, I wrote a whole post on it here &lt;a title=&quot;http://rlacovara.blogspot.com/2014/03/smart-guy-disease.html&quot; href=&quot;http://rlacovara.blogspot.com/2014/03/smart-guy-disease.html&quot;&gt;http://rlacovara.blogspot.com/2014/03/smart-guy-disease.html&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;The company-wide core framework is an idea that seems like an obvious win to management and architects.&amp;#160; But be warned.&amp;#160; Here there be monsters.&amp;#160; I have never, not once, seen a company-wide architecture solve more problems than it solved.&amp;#160; I have seen two examples where company-wide frameworks had hugely negative impacts on their respective organizations.&amp;#160; So think twice before starting down that path.&amp;#160; &lt;/p&gt;  &lt;p&gt;One alternative I would suggest is to maintain a base application code-base.&amp;#160; But instead of forcing every application to consume it and inherit from it (thus creating cross-application dependencies), just fork it when you create a new app.&amp;#160; That way you have a solid starting point, but you’re free to make changes without worrying about how those changes will affect other apps in your organization.&lt;/p&gt;  </description><link>http://www.referencebits.com/2015/04/core-frameworks-dont-work-most-of-time.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG4-Pk9rRZttSZEvvjpK21mQK6AwtCST0rc0Icoa8H7MEpBtIoqX2dRk2mou7kimUoEHtOUVHyrdiomUNVfwwyF88OIVmKUujUa7bZRBtI8RhkRKwPrOZgI6JDjXnVi9FmCu8JO-a0nJg/s72-c?imgmax=800" height="72" width="72"/><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-3602740361168820879</guid><pubDate>Mon, 12 Jan 2015 02:48:00 +0000</pubDate><atom:updated>2015-01-12T11:08:06.659-07:00</atom:updated><title>You might be using TFS</title><description>&lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If your dev computer has 500GB of disk space taken up by 237 branches of the same code even though 99% of the files in those branches are identical…&amp;#160; you might be using TFS.&lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;/em&gt;&lt;/font&gt;  &lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If you spend 45 seconds watching a little spinning icon each time you rename a file… you might be using TFS.&lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If your supervisor requires that you log your time in 15 minute increments and track those increments against work items in your current sprint… you might be using TFS. &lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If the workflow for tracking a bug fix requires a simple 27 click process that includes setting values in 3 different status fields and manually reassigning the work item and still doesn’t map to your actual process… you might be using TFS.&lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If the most productive team in your company manages their sprints in Excel instead of the company standard ALM software – and you wish you were too… you might be using TFS.&lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If creating a new branch requires more than 10 seconds… you might be using TFS.&lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If you ARE NOT CONFUSED when you hear people say things like “It took me 5 minutes before I was able to switch to disconnected mode” or “I spent a full day trying to fix a corrupted workspace”… you might be using TFS.&lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If you’ve ever heard the phrase “Yeah it’s kind of inefficient and it has some rough spots and the constantly connected model is a pain, but it’s sooooo much better than Source Safe”… you might be using TFS.&lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If you’ve ever lost 2 days trying to fix history after a bad merge… you might be using TFS&lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;font face=&quot;Arial&quot;&gt;&lt;em&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;If your company has ever sent all of the devs home because the source control server was down and nobody could work… you might be using TFS.&lt;/font&gt;&lt;/em&gt;&lt;/font&gt;&lt;font style=&quot;font-weight: normal&quot;&gt;&amp;#160;&lt;/font&gt;&lt;/h3&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I find it tragic that so many .Net teams use what I consider to be the “worst of breed” solution just because it’s made by Microsoft.&amp;#160; TFS is a cumbersome, poorly designed system that extracts a productivity tax on every developer, every day that they use it.&amp;#160; The constantly connected model alone is reason enough for me to never use this tool.&amp;#160; So why do people use it if it’s so bad? I think it comes down to 3 unfortunate facts of life:&lt;/p&gt;  &lt;p&gt;1. SCM (Source Control Management) and ALM (Application Lifecycle Management) software is typically selected by managers, not by the developers who will need to use those systems.&lt;/p&gt;  &lt;p&gt;2. Microsoft makes TFS.&amp;#160; It’s a safe choice.&amp;#160; Nobody is going to get fired for picking the solution made by Microsoft.&lt;/p&gt;  &lt;p&gt;3. Most of the teams using TFS have either never used another SCM, or were previously using Microsoft Source Safe, the only SCM so horrible that it actually makes TFS look good by comparison.&lt;/p&gt;  &lt;p&gt;So the next logical question is “Ok Mr. Complainer, if TFS is so bad what do you use instead?”&lt;/p&gt;  &lt;p&gt;I’m glad you asked.&amp;#160; In my opinion there is only one choice, git. A git repository hosted on either Bitbucket (from Atlassian) or GitHub, and managed using&amp;#160; SourceTree (also from Atlassian) is such a gloriously simple and low friction solution that I’ve never found cause to use anything else.&amp;#160; If you find yourself in the position of picking an SCM, do yourself a favor and try the git/SourceTree/(GitHub or Bitbucket) solution first and see if it meets your needs.&lt;/p&gt;  &lt;p&gt;ALM/sprint planning/bug tracking is another matter.&amp;#160; There are lots of SAAS apps that provide this functionality and I find all of them easier to use than TFS.&amp;#160; GitHub itself has some basic ALM functionality that will be good enough for most small teams.&amp;#160; There are also 3rd party SAAS apps that plugin to GitHub or Bitbucket to provide additional features.&amp;#160; Huboard is one such app that provides a Kanban board for your issues on GitHub.&amp;#160; The bottom line is try something simple and see if that works for you before moving on to a more complex solution like Jira or (shudder) TFS.&lt;/p&gt;  &lt;p&gt;One last thing.&amp;#160; I know some people will read this and say “You’re totally out of touch, you can use git as your source control within TFS, get all the git advantages, and still get all the great TFS vertical integration.”&amp;#160; That might be true. I’ve never used TFS with git and I’ve never seen it used in any of the TFS shops that I’ve worked with. That’s worth noting. I’ve heard a lot of people talk about TFS with git, but I’ve never actually seen it implemented in the wild. So, it might be the land of milk and honey. It might fix every problem I’ve ever had with TFS. I doubt it, but it is possible.&amp;#160; &lt;/p&gt;  &lt;p&gt;To conclude, I don’t have an informed answer to the TFS with git question because what I’m doing works great and I’m not willing to invest another iota of my precious time into the sinkhole of productivity that is TFS.&amp;#160;&amp;#160; I think the great philosopher Samuel Jackson (Pitt) from Pulp Fiction said it best. “Sewer rat might taste like pumpkin pie, but I wouldn’t know, because I wouldn’t eat the filthy mother-scratcher.”&lt;/p&gt;  </description><link>http://www.referencebits.com/2015/01/you-might-be-using-tfs.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-5447577045815345296</guid><pubDate>Fri, 23 May 2014 18:47:00 +0000</pubDate><atom:updated>2014-05-23T12:52:23.879-06:00</atom:updated><title>SafeSubstring Extension Method</title><description>&lt;p&gt;Extension methods are a great way to keep you’re view code tight and readable.&amp;#160; I always include a UIExtensionMethods class in my projects that contains extensions for the HtmlHelper and for my View Model classes. &lt;/p&gt;  &lt;h3&gt;The Problem&lt;/h3&gt;  &lt;p&gt;I often want to restrict the max number of characters that will display in the UI.&amp;#160; If a field has 2000 characters, I just want to show the first 100.&amp;#160; Easily done right?&amp;#160; Just use a substring like so:&lt;/p&gt;  &lt;p&gt;   &lt;div id=&quot;scid:9D7513F9-C04C-4721-824A-2B34F0212519:f81cebd7-9f3d-4985-a09a-b233eae8d718&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px&quot;&gt;&lt;pre style=&quot; width: 633px; height: 79px;background-color:#FCFCF5;overflow: visible;;font-family:Courier New;font-size:12&quot;&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;
    @Model.JobSummary.Substring(0, 300)
    &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;a &lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;=&amp;quot;#&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;=&amp;quot;empub_feed_readmore_link&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;read more...&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn&#39;s Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;The problem is that there are a number of things that will make Substring throw errors.&amp;#160; If JobSummary is null, error.&amp;#160; If JobSummary has less than 300 characters, error.&amp;#160; I can check for these things in my view code, but that’s going to make my view look messy and I’ll have to copy and paste that code every time I do a substring in a view.&lt;/p&gt;

&lt;h3&gt;A Quick Solution&lt;/h3&gt;

&lt;p&gt;Anytime I have view code that needs to be used more than once, or even hurts the readability of my view, I extract that code into an extension method.&amp;#160; Since I want a version of Substring that I can run in a view without throwing an error, I call the method SafeSubstring.&lt;/p&gt;

&lt;p&gt;
  &lt;div id=&quot;scid:9D7513F9-C04C-4721-824A-2B34F0212519:54a180e6-e65e-4f01-958e-0b65ad03c602&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px&quot;&gt;&lt;pre style=&quot; width: 648px; height: 180px;background-color:#FCFCF5;overflow: visible;;font-family:Courier New;font-size:12&quot;&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style=&quot;color: #008000;&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt; SafeSubstring&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; MvcHtmlString SafeSubstring(&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; HtmlHelper htmlHelper, &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; text, &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; start, &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; stop)
{
    &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.IsNullOrEmpty(text) &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; text.Length &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; (start &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; stop))
    {
        &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; MvcHtmlString(text);
    }
    &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; MvcHtmlString(text.Substring(start, stop));
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn&#39;s Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now my view code looks clean, and I have another tool in my toolbox that can be used next time&lt;/p&gt;

&lt;p&gt;
  &lt;div id=&quot;scid:9D7513F9-C04C-4721-824A-2B34F0212519:fbba62a0-b534-40ba-b10b-02e1ba57a68b&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px&quot;&gt;&lt;pre style=&quot; width: 642px; height: 80px;background-color:#FCFCF5;overflow: visible;;font-family:Courier New;font-size:12&quot;&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;
    @Html.SafeSubstring(Model.JobSummary, 0, 300)
    &lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;a &lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;=&amp;quot;#&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;=&amp;quot;empub_feed_readmore_link&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;read more...&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn&#39;s Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;/p&gt;  </description><link>http://www.referencebits.com/2014/05/safesubstring-extension-method.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-8591244035774064086</guid><pubDate>Mon, 31 Mar 2014 05:59:00 +0000</pubDate><atom:updated>2014-04-14T12:16:09.682-06:00</atom:updated><title>Smart Guy Disease</title><description>&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0S0b1xSBpi4UYDk6zzYfOACftdcOV_MAg-a6ThjVpmN6o-tkC07uK7bdtmUigBx6gmHzQKmeRsOaxloDCvhTlTnwQ9089K1E7QdDo2wCF9Ii8UgJOyWVEHPoS-G8H1jvbqALB4IytfQk/s1600-h/nerd_dog1%25255B6%25255D.jpg&quot;&gt;&lt;img title=&quot;nerd_dog1&quot; style=&quot;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&quot; border=&quot;0&quot; alt=&quot;nerd_dog1&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5B4LNtOJBkIH8sUy9PdgnDYNsekR3_VojXrJu5WCrPhZcJ43JOBqSbfNNOA1UQWQAAdRyYEWHxyT2QFWt-Rtuwhj0XwHTDEBKA9I51929mkqK1ilrZezu4_KGOjRXKiybsXCXOzVBR0w/?imgmax=800&quot; width=&quot;640&quot; height=&quot;428&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As a consultant I’ve had the opportunity to see first-hand how software development works at some of the most cutting-edge .Net shops in the Denver area.&amp;#160; One of the things I’ve seen over and over is a phenomenon I call “Smart Guy Disease”.&lt;/p&gt;  &lt;p&gt;The basic concept is this.&amp;#160; The biggest, most destructive mistakes are always made by the “smart” programmers, not the “dumb” programmers.&amp;#160;&amp;#160; The smarter the programmer, the greater the capacity for catastrophe.&lt;/p&gt;  &lt;p&gt;It seems counter-intuitive, but it’s true.&amp;#160; Think about it for a minute.&amp;#160; “Dumb” programmers…. actually I have to stop a moment because I don’t really like the term dumb programmers.&amp;#160; One of the reasons I like software development is that I consistently get to work with smart people.&amp;#160; Even programmers who aren’t up on the latest technologies and are just showing up to do their job every day tend to be very intelligent people.&amp;#160;&amp;#160; However, I think we all have worked with people who just weren’t up to the task.&amp;#160; They consistently ship code rife with bugs, you have to code review every line they commit with a fine tooth comb, they just aren’t very good at programming and they consistently produce bad code.&amp;#160; Bad programmers might be a better term, but I’ll keep using “dumb” because it contrasts well with smart.&lt;/p&gt;  &lt;p&gt;Anyway, “dumb” programmers do produce crappy code, but they almost never make the kind of massive mistakes that cripple an entire organization’s ability to ship software.&amp;#160; They may implement features that perform poorly, but they don’t deploy code that destroys performance on a global scale.&amp;#160;&amp;#160; Nope, when you want to cause that kind of damage, you need a smart guy.&amp;#160; &lt;/p&gt;  &lt;p&gt;I’ve seen it.&amp;#160; It happens.&amp;#160; The smartest guys in an organization are the ones that make the truly big mistakes. &lt;/p&gt;  &lt;h3&gt;Shipping architecture instead of software&lt;/h3&gt;  &lt;p&gt;The first mistake I’ve seen is shipping architecture instead of software.&amp;#160; Companies take the smartest guys in the organization and tell them that they’re architects now and they are no longer responsible for shipping an actual application. Instead they will create a company-wide enterprise architecture, or Core Framework,&amp;#160; that other app development teams, who do ship production applications, will be required to build on top of. &lt;/p&gt;  &lt;p&gt;I’ll go into more detail on why this is a bad idea in a later post, but for now just know that in this situation the incentives of the architects and the app developers are not aligned.&amp;#160; The architects’ priority is to create architecture, something that implements cutting edge technologies, looks great in diagrams, is impressive when presented to C level executives, and if it’s overly complex… well that just demonstrates how smart they are.&amp;#160;&amp;#160; The app development team’s priority is to produce code that performs well, is easy to maintain, and ships on time.&amp;#160; The architects really have no idea if their designs are easy to implement or perform well until they are implemented by the app team.&amp;#160; Not a good way to ship code.&lt;/p&gt;  &lt;h3&gt;Overly complex solutions&lt;/h3&gt;  &lt;p&gt;Another common problem, that I alluded to above, is overly complex solutions.&amp;#160; You might be tempted to dismiss this one as no big deal.&amp;#160; Don’t.&amp;#160; This is probably the most common and most damaging mistake that I’ve seen in the real world.&amp;#160; Smart programmers usually like to show how smart they are.&amp;#160; You can’t really do that if you produce code that is easy to understand and maintain.&amp;#160; Why implement a simple solution when you can do something clever instead?&amp;#160; &lt;/p&gt;  &lt;p&gt;So what’s the cost of extra complexity? If you look only at the first 9 months of an application’s lifecycle then the answer is “not much”.&amp;#160; However, if your application is successful and has a lifecycle that extends beyond the first year then that extra complexity becomes a tax that you pay over and over, every time someone touches that code base.&amp;#160; That’s the real problem.&amp;#160; The price for additional complexity isn’t just paid by that initial developer, it’s paid again and again by every developer who touches that code for the entire lifetime of the application.&amp;#160; It’s far too common for smart developers to ship a new product with needlessly complex architectures, then declare the project a success and move on, leaving a maintenance nightmare for the “average” developers who come after them.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;Complexity also leads to brittle code.&amp;#160; We all understand intuitively that the more complex something is, the more likely something will go wrong and it will break. That’s bad enough by itself. Now think a year down the road when that complex system is being modified by a developer who doesn’t necessarily understand the intent of the original “smart” programmer. Now you’ve got a high probability that something will get broken.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;The crazy thing is that teams sometimes look at the complexity of their code as a badge of honor.&amp;#160; If you ever catch yourself saying something like “We are so cutting edge that we can only hire the best developers, and even then it takes them a good 4 months before they’re productive with our architecture”…. maybe you should ask yourself if that’s really a good thing.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;h3&gt;Smart on paper, disaster in reality&lt;/h3&gt;  &lt;p&gt;One last mistake, solutions that look great on paper but are a complete disaster when implemented in the real world.&amp;#160; The best example I’ve seen of this was when a company decided that they wanted to rewrite their ASP.Net WebForms applications so that they could be managed with a 3rd party CMS.&amp;#160; The idea was that the WebForms pages were mostly made up of .ascx controls anyway, so it should be possible to make every .ascx responsible for loading and saving it’s own data, then publish that data to and coordinate with the other controls on the page at runtime.&amp;#160; These individual controls could then be composited together in any combination by designers or business people using the CMS. There was some kind of limitation with the CMS that required all of the heavy lifting to be done in the browser with JavaScript and Ajax.&amp;#160; Did I mention this was WebForms code with update panels and all that nonsense. Remember how fun it was to do real JavaScript and manage DOM Ids browser-side with update panels?&lt;/p&gt;  &lt;p&gt;To be fair, it’s been a while and I don’t really remember the details.&amp;#160; I just remember that the big brains decided this made sense, and I’m sure it did make sense in a high-level planning meeting.&amp;#160; But, every hands-on-keyboard developer who worked with those apps immediately recognized that this was a terrible idea.&amp;#160; Every developer who actually shipped code could see that even if a miracle happened and this idea worked, it would make the code much, much harder to maintain and it would slow development to a crawl.&amp;#160; It did by the way.&lt;/p&gt;  &lt;h3&gt;How to avoid smart guy disease&lt;/h3&gt;  &lt;p&gt;So what’s the point?&amp;#160; Is this just a rant because I hate smart people?&amp;#160; No of course not.&amp;#160; I like smart people and I like working with smart people.&amp;#160;&amp;#160; The point is that it’s possible, even common, for the smartest developers, who should be our greatest asset, to instead create massive problems.&amp;#160; But it doesn’t have to be that way. &lt;/p&gt;  &lt;p&gt;I have 2 simple rules that can help prevent smart guy disease from crippling your organization.&lt;/p&gt;  &lt;h3&gt;&lt;strong&gt;Rule 1.&amp;#160; Make sure your smartest developers are always in a role where they are shipping production code.&lt;/strong&gt;&amp;#160; &lt;/h3&gt;  &lt;p&gt;Most of the problems mentioned above happened when the smartest developers became disconnected from the reality of building and maintaining production software.&amp;#160; That disconnect messes up the incentives and puts your smartest programmers at odds with the programmers who actually ship code.&lt;/p&gt;  &lt;p&gt;Instead, keep your best and brightest developers embedded in teams that are shipping production applications.&amp;#160; Let them be a team leader or a team member.&amp;#160; Give each application team their own architect. Or, if they can’t be a permanent team member, at least let them be a temporary resource embedded inside the team. They can work hands-on-keyboard with the other developers to actually implement their architectural designs.&amp;#160; That kind of hands on experience will help everyone involved and will keep those incentives aligned.&lt;/p&gt;  &lt;h3&gt;&lt;strong&gt;Rule 2. Value simplicity.&lt;/strong&gt;&amp;#160; &lt;/h3&gt;  &lt;p&gt;Make simplicity a cultural value, a goal that every developer strives to attain.&amp;#160; Make it clear that simple solutions are valued and needlessly complex solutions won’t be tolerated.&amp;#160; Keep a close eye on any developers who want to use multiple layers of base classes, Unity Interceptors, or other techniques that are used to make things happen automagically.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;Never, ever boast that your architecture is so complex that only the very best developers can work on it.&amp;#160; Instead your boast should be that your architecture is so well designed that any junior developer can come in off the street, understand the basics in a day or two, and be productive within the week.&lt;/p&gt;  </description><link>http://www.referencebits.com/2014/03/smart-guy-disease.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5B4LNtOJBkIH8sUy9PdgnDYNsekR3_VojXrJu5WCrPhZcJ43JOBqSbfNNOA1UQWQAAdRyYEWHxyT2QFWt-Rtuwhj0XwHTDEBKA9I51929mkqK1ilrZezu4_KGOjRXKiybsXCXOzVBR0w/s72-c?imgmax=800" height="72" width="72"/><thr:total>9</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-6766428865667410729</guid><pubDate>Wed, 19 Dec 2012 15:34:00 +0000</pubDate><atom:updated>2012-12-19T08:34:29.565-07:00</atom:updated><title>NHibernate WCF Error: HTTP request context being aborted by the server??</title><description>&lt;p&gt;I’ve been ambivalent toward ORMs for quite some time now, but I’m really starting to dislike NHibernate in a special way.&amp;#160; My experience has been that ORMs lead to a lot of wasted time troubleshooting code problems that are really ORM problems.&amp;#160; Here’s one example.&lt;/p&gt;  &lt;p&gt;I recently got this mysterious and unhelpful error message on an ASP.Net MVC3 web app that was pulling data via a WCF web service, which in turn was using NHibernate for persistence.&lt;/p&gt;  &lt;p&gt;&lt;font style=&quot;background-color: #ffff00&quot; color=&quot;#ff0000&quot;&gt;An error occurred while receiving the HTTP response to &lt;/font&gt;&lt;a href=&quot;http://localhost:8080/JobsService/ws&quot;&gt;&lt;font style=&quot;background-color: #ffff00&quot; color=&quot;#ff0000&quot;&gt;http://localhost:8080/JobsService/ws&lt;/font&gt;&lt;/a&gt;&lt;font style=&quot;background-color: #ffff00&quot; color=&quot;#ff0000&quot;&gt;. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Ok, my service endpoint was definitely using HTTP and none of that other stuff was happening either.&amp;#160; So what was the problem?&amp;#160; NHibernate lazy load proxies.&lt;/p&gt;  &lt;p&gt;Let’s say I have a Company entity that contains a child collection of type List&amp;lt;Job&amp;gt; as shown in the diagram below.&amp;#160; So if we look at the Company entity, all of the fields (Name, State, City) exist in my data table, except for the Jobs.&amp;#160; Jobs represents a relation between my Company and Job tables and to get Job data NHibernate has to query the Job table.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgX_hC9CIVHkn29CbkOIldAYvkETT_WFY8qYQJ_lqaYR0jba28OiJX1I-Sut-Y4q8AB8ys0hMPi2SbYDn4E4C95Z0gYv6dVt0dD_m4Ob9SQNydA4vozuS4n_eGp8lXqQc4o_xMrNcSS_pg/s1600-h/image%25255B9%25255D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxUooyafykt8aMwuS-mSAHDkR87j0JpzC4ego7du6Zjz8OLX4MVwNkK6W3xf6nbassMM4o1aDr3RUHwqG5Sb5ExId0N2sHNBEDewnTS2gDHKaMk8xgRQcBmabOQzbdthuQPp3R4asFSkY/?imgmax=800&quot; width=&quot;361&quot; height=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;At this point it’s important to note that I’m using NHibernate, all of my entity properties are virtual, and I have lazy loading enabled.&amp;#160; So what happens when I run a query to get a Company? What data does NHibernate populate?&amp;#160; It’s going to populate all of the data that’s in the Company table (Name, State, City) , but it’s not going to populate the Jobs collection.&amp;#160; It’s going replace that virtual property with a proxy that will execute a query to get Jobs only when that property is accessed.&amp;#160; &lt;/p&gt;  &lt;p&gt;I actually think this lazy loading proxy technique is pretty neat and it’s technically impressive.&amp;#160; Unfortunately it’s also the root cause of a lot of application errors. &lt;/p&gt;  &lt;p&gt;So the lazy loading proxy works great when we’re accessing an entity directly in our web app while it’s still has the context of the NHibernate session, but what happens when we try to return our Company over WCF and WCF tries to serialize that Jobs property that hasn’t been lazy loaded yet?&amp;#160; That’s right, you get the ambiguous error message above.&amp;#160; &lt;/p&gt;  &lt;p&gt;The fix is simple.&amp;#160; You just need to eager load the Jobs. You can do this any number of ways.&amp;#160; Here’s how to do it in a criteria query.&lt;/p&gt;  &lt;p&gt;&lt;font color=&quot;#008000&quot; face=&quot;Courier New&quot;&gt;return session.CreateCriteria&amp;lt;Company&amp;gt;()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Add(Restrictions.Eq(&amp;quot;Id&amp;quot;, _Id))      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .SetFetchMode(&amp;quot;Jobs&amp;quot;, FetchMode.Eager);&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Now that we’re eager loading the Jobs there’s real data there instead of the lazy load proxy, WCF has no problem serializing the Jobs, and our mysterious ambiguous error goes away.&amp;#160; &lt;/p&gt;  &lt;p&gt;So, I hope this saves someone a little time, and please remember, friends don’t let friends use ORMs.&lt;/p&gt;  </description><link>http://www.referencebits.com/2012/12/nhibernate-wcf-error-http-request.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxUooyafykt8aMwuS-mSAHDkR87j0JpzC4ego7du6Zjz8OLX4MVwNkK6W3xf6nbassMM4o1aDr3RUHwqG5Sb5ExId0N2sHNBEDewnTS2gDHKaMk8xgRQcBmabOQzbdthuQPp3R4asFSkY/s72-c?imgmax=800" height="72" width="72"/><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-6005410164338780591</guid><pubDate>Wed, 18 Jul 2012 17:35:00 +0000</pubDate><atom:updated>2012-07-18T11:41:32.266-06:00</atom:updated><title>The problem with ORMs</title><description>&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEplaHrBHIVj_zYGkflGC-Ct6dmkGGo8ICqgZpV9rwEw6XB11lm3kkuMPmsc_Kz8Xpg6on-PpeXxXQ9XU6p_C7SIt3lJTbxiGL3H7lKDf97dCezIjdVEHWSZgjsuPFimnlaznnIXN2yzI/s1600-h/pigdog%25255B4%25255D.jpg&quot;&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&quot; title=&quot;pigdog&quot; border=&quot;0&quot; alt=&quot;pigdog&quot; align=&quot;left&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGcrPu9ILfqAGID0WMLjxy1O3LwQv4KKGRyHpDQ5iJoTziV2pJuBTBAAeVGUCao5qfSbmuV0NbY7m6baKRSMn1OT8B-k2xphq6rBaO94WyylDJKrP6I2jeiZsRKB5RV3fNT_9BGuHJtCg/?imgmax=800&quot; width=&quot;240&quot; height=&quot;240&quot; /&gt;&lt;/a&gt;Over the last 8 years I’ve done a lot of thinking and experimenting with different persistence models. If you’d asked me 6 years ago what a persistence layer should look like I would have whole heartedly suggested an ORM like nHibernate.&amp;#160; Linq to SQL came along and initially looked promising but I quickly soured on it in favor of Entity Framework.&amp;#160; Then I actually tried to use Entity Framework in a production app and quickly found that in spite of the initial query writing efficiencies, it caused problems in other areas and the net result was that it didn’t save me any time at all but did add limitations and complexity to my app.&amp;#160; &lt;/p&gt;  &lt;p&gt;So what was the problem?&amp;#160; As I spoke with more developers I started to hear the same complaints, even though some were using EF, some were using nHibernate, others were using LinqToSql.&amp;#160; I heard the problem stated best by a guy I met at the speaker’s dinner for the Rocky Mountain Tech Trifecta.&amp;#160; I can’t remember the guy’s name but his words stuck with me. I was telling him that I would never use EF again if the choice was up to me.&amp;#160; He said it’s not that he has a problem with Entity Framework, it’s that he’s done with ORMs.&amp;#160; I think that’s really the heart of it.&amp;#160; There’s a fundamental problem with ORM as a pattern.&amp;#160; It just doesn’t adequately solve the problem and it ads significant complexity and inefficiency to an application.&lt;/p&gt;  &lt;p&gt;I recently ran across a couple of old blog posts that make for interesting reading, &lt;a href=&quot;http://ayende.com/blog/105473/is-or-m-an-anti-pattern&quot;&gt;Is OR/M an anti pattern?&lt;/a&gt; by Ayende, and &lt;a href=&quot;http://seldo.com/weblog/2011/08/11/orm_is_an_antipattern&quot;&gt;ORM is an anti-pattern&lt;/a&gt; by Laurie.&amp;#160; I’m hearing this line of thought more often as more developers use EF and other ORMs in production apps and then have a couple of years of living with the consequences. &lt;/p&gt;  &lt;p&gt;So it’s easy to knock something, but what’s the alternative? For me it’s been moving to a simpler data mapper instead of a full on ORM.&amp;#160; I first wrote &lt;a href=&quot;http://rlacovara.blogspot.com/2011/01/aapl-part-7-building-generic-data.html&quot;&gt;my own simple data mapper&lt;/a&gt;, but since then I’ve discovered &lt;a href=&quot;http://code.google.com/p/dapper-dot-net/&quot;&gt;Dapper.net&lt;/a&gt; by Sam Saffron and Marc Gravel.&amp;#160; On the surface Dapper works almost identically to the mapper I wrote, but under the covers it’s way better and much more efficient. So I’ve been porting all of my old code over to use Dapper.&amp;#160; &lt;/p&gt;  &lt;p&gt;The results so far have been fantastic.&amp;#160; Sometimes I have to write more code that I would have to write with a full-on ORM, but I find that I can make major application changes (relatively) quickly and easily and I have never once spent hours struggling with my persistence architecture trying to understand why I’m erroring out on an update for an object graph, or troubleshooting lazy load issues, or select N+1 issues, or thinking to myself “If only this was SQL I would know exactly how to do this”.&amp;#160; Instead it just works, exactly the way I expect it to, and in the end that saves a lot of time and effort. &lt;/p&gt;  </description><link>http://www.referencebits.com/2012/07/the-problem-with-orms.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGcrPu9ILfqAGID0WMLjxy1O3LwQv4KKGRyHpDQ5iJoTziV2pJuBTBAAeVGUCao5qfSbmuV0NbY7m6baKRSMn1OT8B-k2xphq6rBaO94WyylDJKrP6I2jeiZsRKB5RV3fNT_9BGuHJtCg/s72-c?imgmax=800" height="72" width="72"/><thr:total>6</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-4740270962380186147</guid><pubDate>Mon, 04 Jun 2012 18:10:00 +0000</pubDate><atom:updated>2012-06-04T12:10:32.390-06:00</atom:updated><title>Great Backbone.js Tutorial</title><description>&lt;p&gt;&lt;img style=&quot;background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px&quot; border=&quot;0&quot; src=&quot;http://coenraets.org/blog/wp-content/uploads/2011/12/backbone.png&quot; /&gt;&lt;/p&gt;  &lt;p&gt;I definitely think that JavaScript is the future (at least the near term future) of app development, and I recently converted HireFlo to a single page JavaScript app.&amp;#160;&amp;#160; You know the type, where you only have 1 real HTML page and it defines the main content areas of your site.&amp;#160; Then the rest of the app is all JavaScript that is used to load content into those areas mostly through AJAX and client side templates (check out &lt;a href=&quot;https://github.com/BorisMoore/jsrender&quot; target=&quot;_blank&quot;&gt;jsrender&lt;/a&gt;).&amp;#160; &lt;/p&gt;  &lt;p&gt;After doing the HireFlo rework, and seeing the rats nest of JavaScript that I created, I can definitely see the value of using a JavaScript MVC or MVVM framework to manage my JavaScript UI code.&amp;#160; One framework that I’ve been looking into is Backbone.js.&amp;#160; It’s a full-on MVC framework for JavaScript.&amp;#160; How cool is that?&amp;#160; The more I learn the more I like, and I think I might be refactoring HireFlo in the near future to use Backbone.&amp;#160; &lt;/p&gt;  &lt;p&gt;If you want to take a look at Backbone, check out the &lt;a href=&quot;http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/&quot; target=&quot;_blank&quot;&gt;Backbone.js Wine Cellar Tutorial&lt;/a&gt; by Christophe Coenraets.&amp;#160; It’s the best intro I’ve seen so far.&lt;/p&gt;  </description><link>http://www.referencebits.com/2012/06/great-backbonejs-tutorial.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-2122217766079869434</guid><pubDate>Sun, 13 May 2012 07:07:00 +0000</pubDate><atom:updated>2014-03-15T22:23:13.582-06:00</atom:updated><title>Awesome Startup Architecture List</title><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;table cellspacing=&quot;0&quot; cellpadding=&quot;2&quot; width=&quot;692&quot; border=&quot;0&quot;&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign=&quot;top&quot; width=&quot;203&quot;&gt;&lt;img style=&quot;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: left; padding-top: 0px; padding-left: 0px; margin: 4px 23px 0px 0px; display: inline; padding-right: 0px; border-top-width: 0px&quot; border=&quot;0&quot; alt=&quot;&quot; src=&quot;data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBhQPDxQUEBQQFRUUDxcUFRUQFA8WFBUYGBUVFhcUFBQYHSYeFxkkGhUYIC8gIygsLC0tFR4xNTAqNSYsLCkBCQoKDgwOGg8PGiwkHR8sKS8qKSoqKTUsLSkwNSwsKSwpNSwpLiwpLCksLCopKiksLCkpLCwpKSksLCwpKSwsKf/AABEIALAAsAMBIgACEQEDEQH/xAAbAAABBQEBAAAAAAAAAAAAAAABAAIEBQYDB//EAEEQAAIBAgMFBQMJBgUFAAAAAAECAwARBBIhBRMxQVEGImFxgTKRoRQjM0JScpKx8ENTYmOCwQeDotHhFRYkwuL/xAAZAQACAwEAAAAAAAAAAAAAAAAAAQMEBQL/xAAqEQACAgEEAAQFBQAAAAAAAAAAAQIRAwQSITEiMkFRExSRsfAVM0Jhgf/aAAwDAQACEQMRAD8A81tRtRAogVTN4AFECjanAUgBaiBRAogUAC1OAogUQKABaiBRFECkMFqcBRAo2oAAFG1G1OApDGgU61G1ECgAAUbUbUQKBitRAogUbUAC1ECjaiBQBTgUQKIFG1dHAgKIFG1ECgAAU4CjStSGICiBRAogUAACjajVhsnYM+LBaFPmx7U0rCOBfEytofIXppN9HMpxgrkyvJsLngKm4TY2Ilz5IZDkbK2bIhDWBtZiDexHlcVbQbBSKWNopRiWBtGyqRhnn4rkvrLHCo3jvexORRxraYHBiGMICTa5LN7TsSS7uebMxJPnU8cK9Shk1bvwHnLdn8UOOHm/p3bfk1cm2ZOvGDED/Kc/letrje18cUrx7uZ8hys0YjK5rXKi7Aki+tM/74w4F3GIUDiWiNh+EmjZjurOlk1O3dsde9OjDuhUgMsikmwzpIt+drsKIFarbm0IcccOi7xhvSzXSZNN09jmIA4kc6iv2aht3d6p5ESObehuDSeH2COt48S+hQAU4CpOK2TLFrbeL9pB3h95P9r1GjcMLqb/AK59KhlFx7LuPLDJ5WECjaiBTrVwSgAo2ogUQKQFNanAUbUbV2cCtStRApwFAAAo2pskgUam365VOh2NO/CPKOsjKvwFzXSi30Rzywh5mQybanpzrumAdnyWYvu8wgiVmnJb6NSoBy3HeOndW19SBVrguzkiurtJECjZgoj3i3HAkPo1jrqLaVdfJ2JctNOTIxZ8jiIOx1JcQhcxPjep4Yq5ZRzandxDoooMFHhpMrpHvROsTSbSZckZKh2YYVCVsim53jNqQLXNq2Z2zs4EM5xe0XX2S0TNCtuG7iISBB5D1qng2dFGbpGgPXKC3qx1NSb1MkU277GT7cxM2LecwRquQRwrNLYxxjUqFjBAZm1J8AOVdG21ibG0eFBtod7MbHkbZNfKhSpiM3/0meMWyrLqSWRrMSTcllfmSetdodiSv9IyxjpH329WPdHoDV9SqH4MLujR/VNV8NY1Okval9iPgcEsCBEzWBv3iTqePl5CpFKlUxmiqDjdjxzG5BV/tpo3ryb1qdSpDTrozGL2dJCCSN4g1LIO8B1ZP7j3VyXUXHMXrWVl8RhdzKY/qnvx/dvqn9JPuIqtlxpK0aWl1Dk9k/8ABtqdaiBRtVU0SmAo2o2rvgcE88qRQqXkkayqLDxJJ+qoFySeFqkXPCIm1FWzhV/2b7E4jH2ZBuof30oNiP5ScZD46L41stkf4fYbAR/KNpSRuVsTn0w6HkAp1lbpfjyWpuN7RT4ru4YPhoeG9cAYhx/KjOkK/wATd7oBViOL3M/Lq2+IfUoNt7Aw+DaPCQLmewmxU0lmkKg/NRE8EDMMxUW0QXvem1xx+zlw08ZjBCzK0bklmLSC8iO7G5ZiM4JPQV2qdFFuxUqVKmIVKlSoAVKlStQAqVNdwvtEDzIH50QaADSpUqAFSpUqAFUHbGBMsfd9tDmTxI4r5Eae6p1Kk+Rp07Rl4pA6hhwI/QPjXQCum0MNuptPYlJI8JOLL6jvDyNNtWdOO10b2HIskFIpWNhc1u+xzLgUZooxPjZBZtbQ4VOIjllH7Q8WRLtew0tWS2NgUnxUUctyjFtASLsq5lBI1toeFelwYdY1CoqqqiwVQAB5AVawx4sztXkd7COME0kglxTmaUeySLRxeEMXBPvasetTKVUu2+1MWFDDWR1IBVNQhOg3j6hPLj4VYKJ37SQZ8K5Fg0YEqEkABozmGvjYj+qqOPbcTAFWZri9kSVzry7qnWrMbO38e9xDrN82XRI7/J17pIKr+0P8Te4Vj+zHaBsHlvmaFlBdBe6kgfOIOvUc/OkM0a4pm9iDFN/lFR73IqNs3aj4rNuIr5DZt5LElr3sbakjQi46Va9pdtKmDLxMCZhkiZT9oG7DyXMfOsLgMY2GkWSL2kFrcmXmjeBt6G1AGykwWJVSz/JI1VSzFnmewAuTooHKqjYGPkxjlGlWFrZkCxKc687Fm0YdLV37T9pUnw8aQn6XvSD6yKp+jYciXFvJT1rMAkEFSQykFWHFSOBFAG02zsz5Ph3lfEYhiq90AxICx0Ud1b8SPcaqOzDRTPusXvHkJ+bZpZgr9UKhgAw5dR5axds9o2xaRIy5Sl2kt7LvwUr4WubcifCqsr59QRoQRwIPIigDS9s8Bh4EjjiiiV5HzFgt2CJqe8bnViB76rNjbY3FkkPzV9D+7/8Aj8vKomMx8mIfPMQWEaxgjS4W5zEciSbmuNAG6Bo1ltjbZ3NkkPzf1WP7M9D/AAfl5Vqb0xCpUqVACpUqVAEbaGDE0ZQ6Hip+yw1VvQ1S4eQsveFmBKsOjDQj9crVoJZQilm4KpY+QFzWZ2biQ8ZYnvFiz30szG/PlYi1VtQuEy/oZNSa9DhgZt3PC/2Z0J8i2U/BjXo+P2jHAAZGtc2VQCXc/ZRBqx8hXmUkeYEcL8+njWs7KbTidrS3GKYaySHMZR0iY+yP5Yt68aeGXFBrIO1L0AvaD5ViWglZ8KoOULoJJTb2DMDaPQ+yNTfjR7aQJBgUSNVRPlCaKLcAxuep04nWs1t8A4zEg2PzxuP6VrnNtKWZEgkbOiOJAWvnAUFQpb6wudL66VM3Sspxi5SSR32N2hbDq6i7ROrgp9aNipGZBy1Oq8+NVkZyoL8kHwFSZsLfvLo3XkfBuvnUdQXcKQRY3YHoPHmCbfGo45E1ZPPTSjJL39RqOdFbMLXZUJ7ozWzMo6kAXp8hPAcSbDz/ANhx9KmzYcOLHzBHEHqK5YXCsGJe1wLLb4t4XrhZuP7JpaNqaS6IzQbo66hiO9zv0bx6Gi1yQq8W4eHVj4CrJowRY6g8Qa5YXBhCTcknQX5DkP8AmuVm8PPZJLRremuiLLhjFqLled9Svj4j8qANWlqqsmVnUcA+nkQD/eusM3LhkWqwKHiiGlSoMwHEgeZAqwUTthERpoxLfdmQK9iV0bu3uOhIrT4zLgZhFcmIpmF7kwd7KFdv3ZPAnhY8taygw7SIxRJGUKSWVWygAXJzGw048eVavZatIhlnsZJlUsCNMoUKqWPK2pHVjQBPpVWKxwpCtcwE2Vj+yPJGP2Oh5cDVnTEKlSrnPiFjF3ZVHViAP+aAK3tNiMuHKjjIwT09pvgPjVNsLYy4vGRIwuoJeT7iakHzJA9TTttbRWeRd3coimxIIBZjqRflYDXxrWf4dbNyxSTkayNkT7iE3Pq5P4RVbU5Ph42zuKtmOApNGCLH9eIPI+NOtTgKr2b9X2cGw+csZCXZmuWb2uAHtDnpxp0GGCXsSb9deHAXrtaiBQ5NnKxxXKXQLU4CjaiBXJIACiBTgKNqQAAogUQKIFAFfjcQ4fKCFGW97XJ5G19Bao6ra/Ek8STcmpW1ioUG4zK1wOZB0YW+PpUaruGtpj6vdvpvgVPw+I3UiSABjG+bKQDmH1lseov62pnQAXJYKOA1JsNToNau4+xuIsWkMMKgEkuzOQBqSQotpbryqYqGh27jhOsUMZusyiVyP3IsQPDO1h5BqFVPZzBbuEMSSX1GbQhLnItuQsSbcs1W1MQHQMCCAQRYg6gg8iKzeF2+IWdAHeJSREQQW0+rcnVL3s3hUztHtHIm7Q2eQakcVTmfM8B69KzoFhYcqQyxxPaGZ/ZyxD+HvP8AiOg9BVay3OZiWb7TksfeeFGlQAUhaRlRPbdwi+bGw93H0r2LA4NYIkjT2Y0CDyAtf14+tecdiVU7Qjz8RHIyffAH/qWr06sjXzuSj7E2NcWeRgUbUbUQKlNwAFOtRAogUDABTgKQFOApAC1ECiBRtSAAFQdpswK2YhWuDl0N+I142tf3VYgVC2s67sgsA2hUcTcajQa+HrXeN+JEWdXjfNFekYHAevP306kDQe9jbjbS/WtEwROtwQeYtWrTbfyvDRQk99iRiPuRkXP+YSo9Wo7J7J4eWBJmlnZWTMbsiBbe0DlF9CCDrypmw8EiK0iKVErZlBJJEY0QEnW5HePi1AFnXLFYlYkZ30Ci5/2HiTp611rL9oNob2Tdr7EZ738T9PJfzPhTEV00zSOzv7TG9vsjko8AP702lSpDFSpUqAJGzcZuMRDLySZSfuk5W/0sa9iryPZPZ+bG3ES2Q6NK9xGORy83PgPeK9Xw0RSNVJzFUVSxFsxAAvble1ZGucXJU+SbGeWWpwFG1ECpDdABRAo2ogUAK1ECiBRApDABTgKNqNICv2tmCqQzBc1mym3Eaa8eP51XpGF4C35+p51Y4/GRsjJmuSOCAsQeR00GtV6nQX0NtavYPLyjI1lOdp2Gmb9b2zC/QG59wopIG4G/66Vuew+KX5KVOVTC7KTZR3fbViR4Hj/DU5SM3sfFSFWwuVxHKwkYsGWyA/OKLjUOQo9W61qAKi4WUzM87X+dIyA8ViW+7Hrqx+/UiSQKCzEAAEknkBqTQBX7c2luY+77b91PDq/kB8bVlUWwt+j5+NXbbEmxT71ikasBkDZmdU4gFRYAm9zrzqdh+yMQ+kaWTwvkX3Lr8ary1MI+p0osyryAcSB5mpOHwEsv0cUhHUjKvva1bXC7Nii+jjRfEKL/AIuPxqTVeWsf8UdbDJQdk5W9t408FBdv7D86k4vsvHFCz3eRkGcByMrZSGZSgABBAI9a0lCoHqMjfZ1tRocM6lFMdghQFcoAGUgFbAcBY11ql7JS/wDjbo8YJGh/pBzR/wChl91XVUZKpNHaPLLUaIFOArRNsaBTgKNqIFAAtTgKNqNqQwAVV7dW5jFr3LAA8CTlC6eZ+NW1qjS5RisKXDkLMWsilmYhQVUKOJJA91SYfOivqv2mQNo7KlwptMmUXsHXWI+Tcj4G1csNhDNIkS8ZHCX6A+03ooJr0J8VPMCEw6KpFicW41HjCmYnyJFV+B7ELHJvDK4Ovdw3zSKDxC6swB8xWiYRK27sfCtGN+UhyqFSTMqOoAsF19oW5G9ZDDxPEzoGLYebJG84SVEIDEgd4aFhdLjTvca3uF2JBEcyRpm+2wLv+N7n41LmiDqQ4DKRZg2oIPEG/KgCgtaq6eRZ8QIL91RvJB9qxGWP3kMfC3WuWLxowrmKJ1mBHzIVwzoeUcxF+6OIboCDrXPCQbmaA3uS7o7H6zSKSSf6lFQZ57Y0uybHick5eiNLSpUqyBipUqVACoUaVAA2NLu8ay8p4Mw+/CbH3xuPwVpqx20Zd1u5v3EyufuE5JP9LE+lbGo8q6f5+dDR5iBRtRAogVdNwAFOAogUbUhgApwFECiBQMAFNlgDizeemhBGoIPIjka6gUQKBNJ8Mn7O7T7tcmKzlgO66Izb0eIUd1xz5HiKfL2rY/RQOfGdljH4Rmb4Cq8CkBVj5mVFL5HHd2zrJtXEycZI4x0hjufxyX/KokmCEn0rSSn+c7sPw3y/CpFqdaopZZvtliGmxx6RzjhCiygAdFAA9wrhtM5Ys/7t0k/C4J+F6mWps8GdGU/WUr7wR/eo0+bJZxuLiXFKoeyJ95h4mPExLfzAyn4g1MqBqnRgipUqVACpUqVADJoQ6srcGUqfIix/OrTszizJhI83toDE/wB6M5CfWwPrVdT+zsuTEzxcnC4hfP6OQe8If6qUlcWNH//Z&quot; width=&quot;198&quot; align=&quot;left&quot; height=&quot;198&quot; /&gt;&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;487&quot;&gt;         &lt;p&gt;I wanted to put together my short list of awesome stuff you must have if you’re building a new web application on .Net. Each of these libraries is free, open source, and takes the simple, no-nonsense approach to getting things done that is essential in a startup.&lt;/p&gt;          &lt;p&gt;Before I get into the list just let me say that jQuery isn’t on the list because I just assume that you’re already using it, and because it’s already included by default when you create a new .Net web app. &lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;font size=&quot;4&quot;&gt;&lt;strong&gt;Dapper&lt;/strong&gt;&lt;/font&gt;     &lt;br /&gt;You really can’t make the claim that there is any one right way to do data access... but this is the one right way to do data access.&amp;#160; It was written by two of the guys at StackOverflow and it gives you reflection like functionality without reflection.&amp;#160; They actually emit IL.&amp;#160; It’s cool.     &lt;br /&gt;&lt;a href=&quot;http://code.google.com/p/dapper-dot-net/&quot;&gt;http://code.google.com/p/dapper-dot-net/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size=&quot;4&quot;&gt;Automapper&lt;/font&gt;       &lt;br /&gt;&lt;/strong&gt;When you find yourself writing that mapping code between your DTOs (or entities) and your ViewModels, save yourself some time and just use this incredibly useful convention based mapping library by Jimmy Bogard     &lt;br /&gt;&lt;a href=&quot;https://github.com/AutoMapper/AutoMapper&quot;&gt;https://github.com/AutoMapper/AutoMapper&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size=&quot;4&quot;&gt;&lt;strong&gt;NLog&lt;/strong&gt;&lt;/font&gt;     &lt;br /&gt;It’s a good idea to bake logging into your app from the beginning. I have no idea why people still use Log4Net (confusing) when NLog (simple and awesome) is out there.     &lt;br /&gt;&lt;a href=&quot;http://nlog-project.org/&quot;&gt;http://nlog-project.org/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size=&quot;4&quot;&gt;&lt;strong&gt;Quartz.Net&lt;/strong&gt;&lt;/font&gt;     &lt;br /&gt;Surprisingly powerful scheduler.&amp;#160; Ideal for writing processors, console apps, and windows services that do things on a schedule.     &lt;br /&gt;&lt;a href=&quot;http://quartznet.sourceforge.net/&quot;&gt;http://quartznet.sourceforge.net/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size=&quot;4&quot;&gt;Twitter Bootstrap&lt;/font&gt;&lt;/strong&gt;     &lt;br /&gt;CSS and Javascript that gives you a whole UI framework and a bunch of themed widgets to build a modern webapp on top of.&amp;#160; Before you write any UI code, spend a day experimenting with Bootstrap. It’s amazingly useful and it gives your app a super polished look.     &lt;br /&gt;&lt;a href=&quot;http://twitter.github.com/bootstrap/&quot;&gt;http://twitter.github.com/bootstrap/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size=&quot;4&quot;&gt;&lt;strong&gt;JsRender&lt;/strong&gt;&lt;/font&gt;     &lt;br /&gt;Javascript templating done right.&amp;#160; Indispensable if you’re&amp;#160; writing a single page javascript app using Bootstrap, BackboneJs, or KnockoutJs.     &lt;br /&gt;&lt;a href=&quot;https://github.com/BorisMoore/jsrender&quot;&gt;https://github.com/BorisMoore/jsrender&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size=&quot;4&quot;&gt;&lt;strong&gt;dotLess&lt;/strong&gt;&lt;/font&gt;     &lt;br /&gt;A .Net port of the Less CSS library. In the style of FakeGrimlock: IT MAKE CSS MORE AWESOME!!&amp;#160; &lt;br /&gt;&lt;a href=&quot;http://www.dotlesscss.org/&quot;&gt;http://www.dotlesscss.org/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size=&quot;4&quot;&gt;jqModal&lt;/font&gt;&lt;/strong&gt;     &lt;br /&gt;Simple jQuery based dialog boxes and modal popups.     &lt;br /&gt;&lt;a href=&quot;http://dev.iceburg.net/jquery/jqModal/&quot;&gt;http://dev.iceburg.net/jquery/jqModal/&lt;/a&gt;&lt;/p&gt;  </description><link>http://www.referencebits.com/2012/05/awesome-startup-architecture-list.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-4519173887635780862</guid><pubDate>Wed, 04 Apr 2012 15:20:00 +0000</pubDate><atom:updated>2012-04-04T10:03:32.699-06:00</atom:updated><title>The 5 Step Recipe for Building a Startup: Developer Edition</title><description>&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN2HO5Nip3Zffcw1ijLWFOarvY0vYAP_3VtYJEQO6lBNKdpg2dPE_90J0sXj_bZICuEzGfLvawGGcR7VFfd-l8QMuU2VI9eEkaXhWhBO_goGdEIPBPhGSErsds_kVgDBxc8I5rIMFcxq8/s1600-h/image%25255B6%25255D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 20px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; align=&quot;left&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKITEcKgGS1z2C_CFdhdpybsXiExMa-zntPonRXMUnJdSbc7W5KCZMm0RU_YdgS6cwgDIB_-dJVxc66VJReEDwy-IpOGVVjRalAYW0zcy9gRHqQldfiGfzxwF-RgWGsCw3TPBY17zMBBc/?imgmax=800&quot; width=&quot;439&quot; height=&quot;310&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Building a startup is hard.&amp;#160; If you spend any time around newtech meetups or startup groups you’re constantly running into people who all seem to have the same problem, they’re a business person with this great idea, if only they could find a developer to build it.&amp;#160; Seems like if you’re a developer and have a startup idea you’ve got it made, right?&amp;#160; Wrong.&lt;/p&gt;  &lt;p&gt;Developers have the skills to make their own vision a reality.&amp;#160; That’s a huge advantage, but it’s also a problem. Because developers are really good at building things with code, the first thing they usually do is…write code.&amp;#160; That’s the absolute wrong place to start, and that’s why most developers wind up producing something that nobody wants, with no users, and no idea where to go from there.&amp;#160; &lt;/p&gt;  &lt;p&gt;The key to not being “that developer” is to start with the business side.&amp;#160; Don’t start writing code right away. Instead start thinking through your app, start talking to potential customers, start doing things like collecting emails that measure the interest in your idea.&amp;#160; Now I’ve done this wrong a couple of times, but I like to think that I can be taught, and this post lists the 5 simple steps that I’ve learned for making a successful startup.&lt;/p&gt;  &lt;h4&gt;1. Mock&lt;/h4&gt;  &lt;p&gt;Buy a copy of Balsamiq mockups and mock your app. Not just the landing page, all of it. Every screen.&amp;#160; This will force you to focus on the user experience side of your app (which is the only part the user cares about) and work through a lot of the logical problems, application flow problems, stuff like that.&amp;#160; It will also give you something you can show to other people when describing your idea, which leads us to....&lt;/p&gt;  &lt;h4&gt;2. Talk&lt;/h4&gt;  &lt;p&gt;Take your mocks and show them to as many people as you can who are potential users.&amp;#160; That part&#39;s important.&amp;#160; You want to talk to people who are in your target market.&amp;#160; Talk through the idea, talk through the app.&amp;#160; Listen.&amp;#160; Listen most to the people who love your idea and the people who hate your idea. Those are the people who care. Figure out what the key values that your product provides are from your customer&#39;s perspective. Also, as you get feedback, you should be updating the mockups incorporating the things that you&#39;&lt;/p&gt;  &lt;p&gt;This is the step that most developers will want to skip.&amp;#160; Don’t.&amp;#160; Talking to potential customers is the most important thing you can do as a Founder.&amp;#160; It’s uncomfortable, it’s tough, you’ll find yourself fumbling for words and talking in circles trying to figure out how to describe your product.&amp;#160; You’ll feel like an idiot for the first 10 or 20 conversations, but then something will start to happen.&amp;#160; Just by sheer force of repetition you’ll find that you’re starting to figure out how to describe your product clearly, and you’ll find that you’re saying a lot fewer words while doing it.&amp;#160; You’ll also develop a picture of what the benefits your product brings that seem to interest people the most.&amp;#160; This is what you’re after.&amp;#160; This whole process is necessary to teach you what your product really is and what your key value propositions are.&amp;#160; It’s not easy but it’s absolutely necessary. If you’re not willing to do it then go work for somebody else, you’re not an entrepreneur. &lt;/p&gt;  &lt;h4&gt;3.&amp;#160; Announce&lt;/h4&gt;  &lt;p&gt;Now that you have mocks, and you know what your key value props are (because you talked about it with many potential customers) it&#39;s time to put up the &amp;quot;coming soon&amp;quot; page.&amp;#160; I would recommend doing a single page that describes the key value props and includes a screenshot.&amp;#160; The screenshot isn&#39;t a real screenshot of your app.&amp;#160; You don’t have an app yet.&amp;#160; You can hire a designer to dummy it up from one of your mocks.&amp;#160; I would recommend spending a few hundred dollars on 99designs.&amp;#160; You can get every thing done at once, a good looking design for your &amp;quot;coming soon&amp;quot; page, a dummy screenshot, and a nice text treatment (no icon needed at this point) for the logo.&amp;#160; Get this page up and start collecting emails of potential users.&amp;#160; &lt;/p&gt;  &lt;p&gt;If you&#39;re really ambitious and have something to say, you may also want to start a blog at this point.&amp;#160;&amp;#160; But don&#39;t blog about building a startup, blog about the problem space that your product is for.&amp;#160; Blog about things that show your expertise in that area, and write things that your target customer will want to read because it&#39;s relevant to the problem that you solve.&amp;#160; If you&#39;re building small business accounting software, then blog about small business accounting issues. Remember, the purpose of the blog is to attract and reach potential users, not other startup founders or the hackernews crowd.&lt;/p&gt;  &lt;h4&gt;&lt;b&gt;4. Bizspark&lt;/b&gt;&lt;/h4&gt;  &lt;p&gt;If you’re building on the .Net stack you absolutely need to join Bizspark.&amp;#160; Microsoft will give you free licenses to basically every product they make, including SQL Server, and a free MSDN gold subscription, for 3 years.&amp;#160; They figure 3 years is long enough for you to get going so after that they want you to pay for new licenses, but here&#39;s the great part, they let you keep the licenses you&#39;re already using.&amp;#160; So you don&#39;t wind up in a situation where you&#39;re just scraping by, then 3 years is up and you have a big Microsoft bill to pay.&amp;#160; It’s great. Microsoft has basically taken the cost factor completely out of the equation for new startups, and it ‘s a perfect program for a .Net developer who’s building their first startup.&lt;/p&gt;  &lt;p&gt;It may seem odd that I didn’t start with Bizspark as step 1. There’s a reason.&amp;#160; You’re not ready to join Bizspark until you reach this point.&amp;#160; Keep in mind that Microsoft doesn’t just accept everyone who applies to Bizspark.&amp;#160; You actually are required to have a company, a website, and a sponsor.&amp;#160; Your sponsor can be a company that has some kind of partner relationship with Microsoft, like Rackspace, or it can be a person employed by Microsoft, like Aaron Stannard who is the startup evangelist for Microsoft (&lt;a href=&quot;mailto:aaronst@microsoft.com&quot;&gt;aaronst@microsoft.com&lt;/a&gt;).&amp;#160; So you’ll need be ready to describe your app and you’ll need to have a website up, not a completed product, just some type of website for your company.&amp;#160; The “coming soon” site is fine for this requirement.&lt;/p&gt;  &lt;h4&gt;5. Build&lt;/h4&gt;  &lt;p&gt;Now that all that other stuff is done, now you start building the first version of your app.&amp;#160; Note that you should continue to talk to potential users, you may even want to establish a small group of users who you show your app to as it&#39;s being built.&amp;#160; The key point is that step 1 is not finding a developer, step 1 is not start building.&amp;#160; Building the app doesn&#39;t happen until step 4.&amp;#160; &lt;/p&gt;  &lt;p&gt;While building, do not follow the common advice &amp;quot;don&#39;t worry be crappy&amp;quot;.&amp;#160; Nothing you produce should ever be crap.&amp;#160; Instead I would recommend taking the 37Signals approach &amp;quot;Build half a product, not a half-assed product&amp;quot;.&amp;#160; In other words, your MVP should have half the features you want it to have, but what you do build should be awesome.&lt;/p&gt;  &lt;p&gt;To give some technical specifics, I highly recommend building on MVC, not WebForms, and I recommend using a virtual server like an OrcsWeb cloud server or a Rackspace cloud server, not Azure.&amp;#160; The reason is that for an early stage startup speed and flexibility are paramount.&amp;#160; You’re going to be making major changes to your app on a regular basis for the first few years.&amp;#160; MVC is a framework that embraces change and makes those changes faster and easier than Webforms.&amp;#160; &lt;/p&gt;  &lt;p&gt;The same principle applies in reverse for Azure. Azure solves the problem of scale, but scale isn’t your problem. My experience, and the experience of a couple of other startups I have first hand experience with, is that Azure slows down development. I think it makes your development slower and less flexible. Debugging is a pain, setting up your environment is a pain, deployment is a pain, the DAL becomes something you actually have to spend time and effort on, plus… what if you decide you need to run Redis or MongoDb or some other open source thing?&amp;#160; Microsoft will probably make it possible so they can check off that box on the feature list and claim that it works, but odds are it will be a whole lot harder than just installing that software on a regular server.&amp;#160; What if you end up needing to have 10 different services running?&amp;#160; Is each of those going to be a separate Azure instance, and thus an additional bill? You don’t want to spend time worrying about stuff like that in the early days when your app can change massively every 2 weeks.&lt;/p&gt;  &lt;p&gt;At the beginning, I think you’re much better off just buying a single cloud server.&amp;#160; It runs just like a real windows server, it provides a familiar environment, it’s going to be a solid single platform that you can load up with whatever open source code and services you need (without paying extra), and you can scale it up if you you need to.&amp;#160; Later, when you know what all the pieces of your app are and you have customers pushing the limits of your cloud server, that is the right time to look at Azure.&lt;/p&gt;  </description><link>http://www.referencebits.com/2012/04/5-step-recipe-for-building-startup.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKITEcKgGS1z2C_CFdhdpybsXiExMa-zntPonRXMUnJdSbc7W5KCZMm0RU_YdgS6cwgDIB_-dJVxc66VJReEDwy-IpOGVVjRalAYW0zcy9gRHqQldfiGfzxwF-RgWGsCw3TPBY17zMBBc/s72-c?imgmax=800" height="72" width="72"/><thr:total>5</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-2891971357298903412</guid><pubDate>Sat, 24 Mar 2012 21:14:00 +0000</pubDate><atom:updated>2012-03-25T14:58:48.714-06:00</atom:updated><title>Should you use .Net for your Startup Company?</title><description>&lt;p&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; margin: 5px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&quot; border=&quot;0&quot; align=&quot;left&quot; src=&quot;data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBhAQDxAPDxAQDxAQEA4PEBAPDQ8PDxAPFRAVFBQQEhQXHCYeFxkkGRUVIC8gIycqLCwsFR4xNTAqNSYrLCkBCQoKDgwOGg8PGi4hHyQsKiw0NS4tLCotLDUtNCwsLy4sLCwsLCwqKiwsLCksKiwwKSwqLCwtKTUsKSotKSktKv/AABEIAOEA4QMBIgACEQEDEQH/xAAcAAEAAQUBAQAAAAAAAAAAAAAAAQIDBQYHBAj/xABDEAACAQIDBQUFBgEKBwEAAAAAAQIDEQQFEgYhMUFRBxNhcZEiMoGhsRQjQlJykmIVgoOTorLB0eHwMzRDRFOjwiX/xAAbAQEAAgMBAQAAAAAAAAAAAAAABAUCAwYBB//EAC8RAQACAgECBAQFBAMAAAAAAAABAgMRBAUhEjFBUXGhscEiYZHh8BMyQtEUI4H/2gAMAwEAAhEDEQA/AO4gAAAAAAAAAAAUymkm20kt7b3JLqBUDWM07SMuw7cXXVWS4xoRdXf01L2fmYGt204ZP2MNiJLrJ0o/K7NU5scecrDH03l5I3XHP0+rooOd0e2jCt+3h8RFdV3U/wD6Rmsv7TctrWXf903yrwlT/te78xGbHPlJk6Zy8cbtjn/zv9G1At4fEwqRU6c4zi+EoSUovya3Fw2oExrtIAA8AAAAAAAAAAAAAAAAAAAAAAAAADA7X7V08vw7qStKpK8aNK++c+r6RXNnlrRWNy2YsV8t4pSNzJtVthh8vp6qr1VJJ93Ri1rn4/wx8X8zi+0m2mLx0n3tRxpX9mhTbjSXS6/E/F/IxeZ5nVxNWdevNzqTd238opckuSPKVGbPbJOo8nf9P6Vi4lYtP4r+/t8P5sABHXASQAPZlmcV8NLXh6s6Mv4JNJ/qjwl8UdK2W7XlJqlmEVBvcsRTXsf0kPw+a3eCOUg248tsflKFyuBg5UayV7+/r+v8h9Q0q0ZxUoNSjJKUZRacWnwaa4orOE7C7eVMBNUqjlPCSftQ4uk3+On/AIx5+Z3HDYmNSEalOSnCcVKMou8ZRaummWuLNGSO3m4LqHT8nCvq3es+U/z1XQAblcAAAAAAAAAAAAAAAAAAAAGBj88zinhKE69V+zBbkvenJ+7CPi2cA2lz+rjcRKtVfhGKfswjyhHwXz3vmbR2obSuviXQg/usM3BW4SrcJy+HuryfU0EquTl8dvDHlDvujdPjj4oy2j8dvlHpH3n9gkgEVeJLNfFwh70vhxfoe7C5BjsX7OCw1WtylUjFRpx8O8laN/iV1ux7OranhVLnaOJw7l/fJOLjzaNypeb1amC39Ompn6ML/LNP+L9p6aGLhP3ZXfTn6GJzXZ/FYSWnFYetQb3LvacoqX6ZcJfBnkpxdzZPHqhY+r5d7mImGzg8uCxEpK0975S5+UuvmekiXpNZ1K/4/Irnr4qpudH7J9rnTqLAVpfd1G3h237lXi6flLe14+ZzcrpVHGSlFuMotSjJcVJO6a8bnuO80t4oY8vjU5WKcVvX5T7vqFEmI2VztYzB0MRwlOFppcqsXpmv3J/Boy5dxMTG4fMslJx2mlvOJ0AA9YAAAAAAAAAAAAAAAABjtoc0+zYSviOdOnKUfGdrQX7mjImB212eqY/B1MLSrKhKbhLW4OatF302TVrtLf4GNt6nTbh8H9Svj8txv4PnLMM5Sm4u83duck/xX3+bKaOOpy4SV+j3P5mUz3skzXCXfcfaaa/HhW6u7xhZTX7TTqkHFuMk4tbmmmmn0afAgTx6xGnXV6vktabRqY9vZsp7MmyueKxFHD0/eqzjC9r6VxlJ+Cim/gajRxc4e7JpdHvXozt/Y1sniI//AKGKpqmp03HDQaam4ytes4v3U0rLm02+Fr4V40zaPZIy9ZpXFadatrt8XTsuy+nQo06FJaadKMYQXglxfi+LfU9QBaOFmZmdysYzA060JUq0IVac1aUKkVOEl4p7jifaL2VRwd8Xg0/s1/vKTbk6Db3Si3vdO/XevLh3MtYnDxqQlTnFShOMoyi1dSi1Zp/AxtXcNuHLOK248nyyrQRNCrqv4MubUZXLC4zEYV3ao1ZRi3xcOMG/OLibRsV2Y4nFwVao/s1CVnGc43qVF1hDp4u3hcr8lZv2iO7r+Lnx8f8A7L21WYawDtmE7J8tgrTVas+cp1nH5Qsi1juyPL5p91KtQlyaqd5G/jGfH1Rh/wATI3R1/i713+Ov338nh7FMxbpYrDt+5OFaK8Jpxl84L1OmHOdgtj8Rl2YVY1LVKNTDyUK0L6XKNWDUZLjGVm93nZs6MTsETFIifRy/VbY78m18c7i2p+QADerAAAAAAAAAAAAAAAAAAALGKzrZXBY1WxWGpVuSlKC7xfpmrSXwZlQHsTMeTQMv7Fcto4uOJSqzhD2o4erONSiql90ndXkl+Vt7/Q35IkHkRp7a9rf3SAA9YgBbrVowjKcpKMYpylJuyjFK7bfSwHLMx2XhjNosTOrFSoYeGFnUi1uqVXSjopvw3XfhG3M6Gq5qWzOZKvHEYtf91iq1RX493G1Kmn5RgvVmZ+1GvHEa37pfKtbxxS3+MRH6efzZTvx35i/tQ+0mxEZaOJse+jVUldf7ZraxJlMnr31LpZ/VAZMAAAAAAAAAAAAAAAAAAAAAAAAAAALmI2i2nw2Bpd5XnZu+inHfUqNcorp4vcjyZiI3LOmO2S0VpG5lksRiI04ynOUYQinKUpNRjFLi23wONdoHaO8XqwuFbjhr2nN3Uq9nwtyh4c+fQwm1+3mIzCWmT7rDp3hQi3bwlN/il8lyRrRXZuR4u1fJ1/TukRgmMmbvb29I/wBz8nRtgcwvhHC++nVnfylaSf8Ae9DZPtRyvZvOPs9a8n93USjPw37pfB/Js337V0d/FcPMlce8Wpr2UnV+PbFyJt6W7x9/mzH2olYox2X4yjqaranw06ZafNmXqZPGcdWGqXf5Kjt6SS+qJCoW1iTP7M71Uly9mK+bf1Rh1s5U037yOro4u3rc2fJsLGlRjBPU1vm+s3x/34Ae4AAAAAAAAAAAAAAAAAAAAAAAAMGJ2o2ghgcLUxE97Xs04Xtrqv3Y+XN+CZ5MxEblnjx2yWilY3M9mN2022p4CGmKVTEzV6dO+6K/8lTpHw5282uF5xnFXFVZVa1SVSUnvk+fRJcorklwK84zOpWqTq1ZOdSrJuUn9F0VrJLkjGFTlyzkn8nfcLgY+HTUd7T5z9o/L6pBANSftJmckzmrGUaKjKspNRhCKcql290YLn5HmyLIMRjayo4aDnLjJvdCnH885cl83yTO47GbAUMujr3VsS1adaStpvxhSX4Y/N8+ik4Md5ncdlN1Tl8emOceSPFM+n3/ACaNi8rrQk41KbhJWunbddX4p258j0ZZmFWmnvvps+O+3M3PHOFSrKbSd9y8krIxuJ2dpVLuL0SfTg/gWbiFeWbQqdk2bNlcE3Kd3vUVa/s83e3X/I1XLdnXRneUU48dSbav4p8DY6tZ0lGceCaUlycX/qBmAW6NZTipRd0y4AAAAAAAAAAAAAAAAAAAAAADjna7nbqYuGFi/Yw8U5Lk601d+kdP7mdikz5pzvMXiMTXrv8A6tapNfpcnpX7bETlW1XXu6DoGGLZ5yT/AIx85/bbG1pXZQGyLle66Z7puZrZPZatmOIVGl7MY2lVqtXjShfj4yfBLn5JmEO/9l+SLDZbRla1TEL7RUfN6vcXkoafV9Tdhx+O3dWdS5k8bDuvnPaP9s3kGz2HwVGNDDw0xW+UnvnUlznOXN/TgrIyFdPTK3HS7edisFpEa7Q4a1ptPitO5c3lmVnvZcp5qupVnWTQWIqqz99tWbW6XtW+Zjp7Py5Nx85O/oesWYjnslwkV/y9eLU3dNNGBhk7vZyl6mYwOX04RacdWpWk5vU2ungBmMjzPTub9l8fDxNlTNUpYylBWUYrySNhyqq5Uoy66reWp2A9YAAAAAAAAAAAAAAAAAAAADG7SYrusFiqvOGHryXmqbt8z5pcrLfyXM+lNp8pni8HXw1OapyrQ0KcouUVdq90vBNfE4jnHYhm124Tw2IXKMKsqT/bOKXzIufFN5j2X3TOdj4uO2/7plqad+G8FzG9nGb0L68DiN3OlHvl/wCtsw0liISVOUasZtqKhOnJSb6KLV7keePK1p1Wk+cfpO2Yo0nOUYLjOSgvOTsvqfU2Fw6pwhTj7sIxhHyikl9DhuwXZjmNWtQxOLjHC0adSlV01U+/qKMlLSqa929uMreTO7knj45pvam6tzKciaxSfLf2AC1ipNQm1xUZNeaTJKlavm2JXfSn1elPySV/keN1iucFNb+Zj6uVO+6pNLpdAemVWKKZYu+6G98+i82eVZWvxzlLzdl8iudZRtGNkvADP5XszSqwhVqSqSbveCkowupNW3K9t3U2WnBRSjFJJJJJKySXJGP2d/5am+ut/wBtmSAAAAAAAAAAAAAAAAAAAAAABaxOJhTg51JxhCKvKU5KMUvFvcavtxt1DARVOCVTEzWqMW/YhHhrnbfxvZc7PgcW2i2mxOMl9/VlU52vaEfCMFuX1I2TkRSdR3lc8LpGTkV/qWnw1+c/CPv9XXsf2v5bSk4xlWr24yo0rw+Dk1f4HsybtHy3GTjTjV7uo37EcRDu25dIyd438L3Pnwgjxyb7W1ujcea6iZifd9XknOOyPbGeJpzwdeTnVoRUqU5O8p0b2cZPm4trf0kuh0cnUtF43Dl+RgtgyTjt6BDRIM2hp9SlolKH5ZSj8E9xZqSPTj397U/XL6njqMDyYuvpTb4JXMTRqSnK/wDuxfzap7sOru/Jf6k4KlwXWyA6PlVHRQpR6U4X87XfzZ6yIxskuisSAAAAAAAAAAAAEXIuBUCm5NwJBTcXAqIZGoXA+cNsc2lXx+KqNv8A49SEfCEJOEV6RRgWzYe0HKXhMxxMJK0alSWIpPlKnUk5bvKWqP8ANNcpzUrpcVvtza8CqvSYmZd5g5NLUrWPLUaSAQa0tuPZLNrN6FuDhiFL9PdSf1SPoA5V2N7KTp68wrRce8h3eHTVm6bacqtujskuqu+DR1PUWXHrMU7uM6rlrk5H4fSNKgU6imdVJNvgk2/I3qtquOf3tT9c/qeCsz1Yud5yl+aUperueCvUAw+JlqqvorIyWV071Ka6zgv7SMZTXtPxdzN5HC9ekl+eL+C3v6AdABSmLgVApuLgVApuTcCQRcXAkEXAFvUQ5Fu5DYF3WRrLLkQ5AX+8I7w87mUSqgevvCO9PE65bligMftjsjh8zoqnWvCpC7o1oJOdOT4q34ouyvHw5PecYzXsdzWlN91CniY39mdGvCD8G41HFp+vmdvqZjbr6HnnnNuUvQxmsS34896RqJ7OT5J2P5nUaeJqUMLDnqksRV+Eabt6yR0LIuyzL8M4zqKWLqRs719KpJ9VSjuf87UZL+XF0l+0lZvflL0MYxVjvpstzc9o8M2nTYe+J78wUcyvyfoXY4t9GbERmO+MfnOMtBRXGT3+S/1sW44hngzKbk78krAYfGYhk5XgnUjWqy9ylTqW/iqaHZfDj6HmqxlKWmKu20l5t8zZq9ONHCSpL8koL+Kclvf1YGmU17Rsmy8Pv0/ywm/lb/EwFOg9XA2HI6ipTcp7vYaW67but24DbdZOsxtHMoy5Nedj1xqAX9ZKkWVMnUBe1DUW9Q1AXbi5bUidQFy4KNQAosQ0V2GkC20UuJd0jSBZcCnQXtI0ged0yl0l0PTpIcAPI6C6FLw66L0PX3ZDpAeJ4ddF6FLoroe50iO5A8OhENI9roFP2VAY2vP2Wk0nyb4fE8azeEfZnZPzTT8mZmpl8ZcVc8GI2Uw8/eprzTcX8gLEc6glaOleVkY/OMx1wTinKUX7sd7afHd6Huew+H5OqvKtIv4bZKjTd462+sqjkBqdPMZLjRq/1UmX1nXWnVX9FP8AyNxjk0VwLsctQGoUs95KFX+qn/kbPl+Kfdxvxtv8N/A9iwRUsIBSqxWqpKw5UqAEKZUpDuiruwCkTqCgNAE6gNAAv2FiRYCmwsV2IsBTYixXYWAo0jSV2FgLekaSuwsBQ4kaS5YWAt6BoLlhYC3oGguWFgLegaC7YiwFvSTpLliLAUaCdJXYWAo0jSV2AFGknSVWJAo0jSVWJsBRYFdgAAAAAAAABBIAEAAAAAAAAAAAAAJAAAAAAAAAAAkACAAB/9k=&quot; /&gt;&lt;/p&gt;  &lt;p&gt;I get this question a lot, from both people inside and outside of the .Net community, and it came up again the other day on Quora.&amp;#160; Below is my answer.&amp;#160; Please keep in mind that this is my opinion, and I can’t promise that I’m right, but I can promise that this is what I really think and if you walked up on the street and asked me “should I use .Net for my startup?” this is what I would tell you.&lt;/p&gt;  &lt;p&gt;In spite of the fact that I’m a pretty high end .Net guy, I would caution new startups that are considering using .Net.&amp;#160; I think .Net is awesome, and I use it for my own startup HireFlo.&amp;#160; However there&#39;s one big landmine that could handicap your startup before it even gets going, webforms.&lt;/p&gt;  &lt;p&gt;You see, ASP.Net development is divided into 2 camps, and there are major architectural differences, and cultural differences between them.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;ASP.Net MVC is awesome for startups&lt;/h4&gt;  &lt;p&gt;ASP.Net MVC is the awesome new framework that is heavily influenced by Rails and is the platform of choice for startups like StackExchange.&amp;#160; It’s a breath of fresh air for skilled .Net developers who want a framework that embraces the way the web works instead of struggling against it.&amp;#160; This camp is full of people who care about the craftsmanship of writing code and who like to actually ship software.&amp;#160; A programmer in this camp is likely to be familiar with lots of open source projects, and is most likely a great fit for your startup.&lt;/p&gt;  &lt;h4&gt;WebForms is death for startups&lt;/h4&gt;  &lt;p&gt;The other camp is WebForms, a festering bog of evil, spaghetti code, and hate. It&#39;s the domain of corporate developers who prize process and documentation over shipping code and hope that the glacial pace of WebForms development will hide the fact that they haven&#39;t shipped a product in years and can&#39;t tell the difference between an HTTP GET and an HTTP POST.&amp;#160; I mean it.&amp;#160; WebForms and it’s poisonous PageLifeCyle pattern are a sink hole of productivity and they are going to resist you every step of the way as your startup struggles to find the right fit between your application and your market (product market fit).&amp;#160; Plus Webforms and the cursed UpdatePanel make it incredibly difficult to do the fancy new style of client side javascript app that tools like Backbone.js, Knockout.js, and jQuery have made possible.&amp;#160; &lt;/p&gt;  &lt;h4&gt;So the answer is…Heck yes you should (as long as it’s .Net MVC)&lt;/h4&gt;  &lt;p&gt;So .Net is awesome, C# is amazing, MVC3 is hugely productive,&amp;#160; if you find a .Net developer who works with those, I&#39;d recommend using them.&amp;#160; But you&#39;ve got to be careful.&amp;#160; A WebForms developer with a corporate dev background is most likely going to be death for your startup.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;&lt;em&gt;There are three other points I want to add that I think are relevant.&lt;/em&gt;&lt;/h3&gt;  &lt;h4&gt;Don’t use Azure for an early stage startup&lt;/h4&gt;  &lt;p&gt;Microsoft is really pushing Azure as a platform for startups.&amp;#160; I strongly recommend that early stage startups do not use Azure.&amp;#160; Azure development is significantly slower than straight up .Net development that runs on a windows server or VS.&amp;#160; Deployment takes longer, debugging takes longer, the Azure environment is difficult to replicate on your local dev computer, backups are a pain, and you&#39;re locked into a single hosting provider.&amp;#160; It all ads up to a lot of inflexibility and friction at a time when your startup needs flexibility and speed over all other things.&amp;#160; Azure can have a place later, when scale is your problem, but at the beginning of your startup that isn&#39;t the issue.&lt;/p&gt;  &lt;h4&gt;The ASP.Net MVC/C#/SqlServer stack scales like a madman&lt;/h4&gt;  &lt;p&gt;Listen to the middle days of the first StackOverflow podcast and you&#39;ll hear that for a long time they ran StackOverflow on a single server.&amp;#160; They were serving a million uniques&amp;#160; with the web app and database running on a single box!&amp;#160; It wasn’t even a very big box.&amp;#160; This is consistent with my experience with .Net.&amp;#160; It scales well. So, if your startup does make it, you&#39;ll probably have a much easier time scaling the .Net stack than you would with say Ruby or PHP.&lt;/p&gt;  &lt;h4&gt;&lt;b&gt;Bizspark is proof that Microsoft loves programmers and startups&lt;/b&gt;&amp;#160; &lt;/h4&gt;  &lt;p&gt;If you want to build a startup on the Microsoft stack, they will give you free licenses to basically every product they make, including SQL Server, and a free MSDN gold subscription, for 3 years.&amp;#160; They figure 3 years is long enough for you to get going so after that they want you to pay for new licenses, but here&#39;s the great part, they let you keep the licenses you&#39;re already using.&amp;#160; So you don&#39;t wind up in a situation where you&#39;re just scraping by, then 3 years is up and you have a big Microsoft bill to pay.&amp;#160; They don&#39;t do that, they just let you keep using the software. So Microsoft has basically taken the cost factor completely out of the equation for new startups.&lt;/p&gt;  </description><link>http://www.referencebits.com/2012/03/should-you-use-net-for-your-statup.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>108</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-5408188600260706681</guid><pubDate>Sat, 03 Mar 2012 18:28:00 +0000</pubDate><atom:updated>2012-03-03T13:49:07.647-07:00</atom:updated><title>HireFlo, an actual startup built on .Net</title><description>&lt;h4&gt;What have I been doing for 10 months?&lt;/h4&gt;  &lt;p&gt;I’ve gotten a number of emails asking for part 2 of the ValidationLabel post that I wrote 10 months ago, and a number of people have said there’s problems with the sample code on &lt;a href=&quot;http://aapl.codeplex.com/&quot; target=&quot;_blank&quot;&gt;aapl.codeplex.com/&lt;/a&gt; and asked for updated code samples.&amp;#160; Sorry about that.&amp;#160; There is a reason I’ve dropped off the face of the earth for the last year.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2Bf37Txw-Aw1jNF5j9YXqX0d27_ZWibbvy6VcYBIvXiF-jShMWR3yjIYsDOJy3TPfKEW1pqE5w4L-7cSoqfHtddbcKGJrnGXVdXht0LhFHpXpB3uy9teN4tk9UfhFONaS9kjfqhKPHHE/s1600-h/dashboard800%25255B4%25255D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px&quot; title=&quot;dashboard800&quot; border=&quot;0&quot; alt=&quot;dashboard800&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyli8-3Qb-Bbkh1BFW_D7sBIgsWby-pbTMbNtUIaxERxol41K5xmeNOrGapLuzihXWgYQ45Vf0_QQrqIa04CH5UFdK7NwCkqEFOO3zs6GRm9zRvd9ZnEc06Ob7ktC4xjdXoKnvpnJbG7o/?imgmax=800&quot; width=&quot;700&quot; height=&quot;486&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;That reason is &lt;a href=&quot;http://hireflo.com&quot; target=&quot;_blank&quot;&gt;HireFlo&lt;/a&gt;.&amp;#160; I took all of the principles I’ve been discussing here, the techniques for creating an architecture that embraces change and can quickly adapt to massive changes to the application, and I put them into practice with my own startup.&amp;#160; &lt;a href=&quot;http://hireflo.com&quot; target=&quot;_blank&quot;&gt;HireFlo&lt;/a&gt; is an Applicant Tracking System designed for the small business market.&amp;#160; It takes all the tools that recruiters use like jobsites, aggregators, social networks, resume databases, and wraps them up in a simple app designed for small business users who probably don’t even know that most of those tools exist.&amp;#160; &lt;/p&gt;  &lt;p&gt;I’ve been building HireFlo for about 18 months. It’s an ASP.NET MVC app that uses &lt;a href=&quot;http://rlacovara.blogspot.com/2010/01/agile-adonet-persistence-layer-part-1.html&quot; target=&quot;_blank&quot;&gt;AAPL&lt;/a&gt; for persistence, and I have to say the MVC and &lt;a href=&quot;http://rlacovara.blogspot.com/2010/01/agile-adonet-persistence-layer-part-1.html&quot; target=&quot;_blank&quot;&gt;AAPL&lt;/a&gt; combination has been a huge success.&amp;#160; I’m on version 4 of the app and I’ve made some massive changes since version 1.&amp;#160; That’s what happens in startups, and why an architecture that embraces change but can still scale, is so incredibly important.&amp;#160;&amp;#160; Each time I’ve made major changes, MVC and AAPL have made the experience as quick and painless as possible.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;The last major change was November 2011 when I decided to completely throw away the old UI, and rewrite a simpler, streamlined version based on a Kanban board, and also make it a single page Javascript app in the style of backbone.js and knockout.js apps.&amp;#160; I completed the complete rewrite of every page, controller, HTML template, CSS file, and Javascript file in the app in just 14 days.&amp;#160; This would never, never, NEVER have been possible if I’d used WebForms or a more Microsofty approach to persistence that used a bunch of sprocs and hard coded mapping logic in my data and business layers. &lt;/p&gt;  &lt;p&gt;So anyway HireFlo is up.&amp;#160; It’s been officially launched since August 2011 but I would say the real launch was end of December 2011 when I released version 4 of the app and the redesigned marketing site.&amp;#160; So far there about 300 companies using it.&amp;#160; It’s a start. &lt;/p&gt;  &lt;h4&gt;What’s coming next&lt;/h4&gt;  &lt;p&gt;So the first thing I need to do is write Part 2 of my last post.&amp;#160; This part details how to take a helper method and turn it into a strongly typed helper method by using expressions and lambdas because…. well because doing that is awesome.&amp;#160; After that I’ve got a lot of tech problems that I’m solving for HireFlo and I’d like to share them.&amp;#160; I just need to find some time and get back on a somewhat regular schedule.&amp;#160; BTW, I’d also be happy to give updates on HireFlo if you’re interested.&amp;#160; Just let me know.&amp;#160; -rudy&lt;/p&gt;  </description><link>http://www.referencebits.com/2012/03/hireflo-actual-startup-built-on-net.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyli8-3Qb-Bbkh1BFW_D7sBIgsWby-pbTMbNtUIaxERxol41K5xmeNOrGapLuzihXWgYQ45Vf0_QQrqIa04CH5UFdK7NwCkqEFOO3zs6GRm9zRvd9ZnEc06Ob7ktC4xjdXoKnvpnJbG7o/s72-c?imgmax=800" height="72" width="72"/><thr:total>5</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-6413211000411708331</guid><pubDate>Sun, 17 Apr 2011 15:05:00 +0000</pubDate><atom:updated>2011-04-17T14:13:37.220-06:00</atom:updated><title>Creating a Validation Label Helper – Part 1</title><description>&lt;p&gt;Presenting validation messages is a pain.&amp;#160; Programmers usually tack validation messages on to a User Interface is a way that gets the job done, but also is clunky and detracts from the overall UI design.&amp;#160; Let me show you an example.&amp;#160; Below is a really simple form from my HireFlo application.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPT7xD0zraJ6IouIi4YtUup5uHfch7IXbsiVFDFgNJg2SkqTnj0F4J07phK2m3f6akkzqVZspLcuWjkuWKN_N4d7nOl3JFiCbTy2W1tco7R8miUWiRrEjcOUICliEbFGSJX1pXkVQyx-4/s1600-h/image%5B10%5D.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8DEGMVA2dygkAhYLka-dqHXEmKpvG08dCeFhStSWiz0q_fq48-6M2CPLtuR4rwa5EGn_pi4JGLZ65zRJiiMw2gDXY8ht2pc93cS2C3-sAEpu3MvZDIMIdIwd9jW1Xu5wDz1rd_C95imY/?imgmax=800&quot; width=&quot;750&quot; height=&quot;407&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here’s the markup that creates the form fields and buttons.&amp;#160; I just use a simple layout with a label followed by a validation message, and a Textbox created using MVC’s strongly typed Html helper methods.&lt;/p&gt;  &lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;div&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;form-row&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;label&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;form-label&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;folder.FolderName&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;The name for this folder&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;br /&gt;           &lt;/span&gt;Enter a name for your folder:&lt;br /&gt;        &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;label&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;background: yellow&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt; Html.ValidationMessageFor(model =&amp;gt; model.Folder.FolderName)&lt;span style=&quot;background: yellow&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;br&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;&lt;/span&gt;&amp;#160;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;background: yellow&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt; Html.TextBoxFor(model=&amp;gt;model.Folder.FolderName, &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; {style=&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;width:300px;&amp;quot;&lt;/span&gt;, maxlength=&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;50&amp;quot;&lt;/span&gt;})&lt;span style=&quot;background: yellow&quot;&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;input&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;SaveButton&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;button-green&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot; Save Folder &amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;input&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;CancelButton&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;button&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;button-gray&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot; Cancel &amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The only validation on the form is that you have to enter a name for the folder you’re creating.&amp;#160; If you don’t, you get a validation error that looks like this.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDbOqHO7fdIOTk2bTd15MaZmerDyCyds8UVMycflEgJFv1TVIqFvSHwVmo68mjjKqV909XBZ3zbafeVeItzm0jfUU_Q9mso-IhwmNVvsqKLKnhq1FjALD-uC7z_C462b4GTobaEue7pUE/s1600-h/image%5B11%5D.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvqLljsNJnWD09m5eEdOY7FX0eC1dX8gzQx9-hyWSQErBC0YA5cgLJ9brSzLkcFuT_JksaONadXqhZ0i9wLYFV54YEQH8McVx6gCgdpWNDQPgB3U97E1CJDxrcOHzfc4DfHCJs1JXSQZk/?imgmax=800&quot; width=&quot;748&quot; height=&quot;406&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;While functional, this doesn’t look very good. The validation message for the field is just kind of tacked on after the label, but where else am I going to put it. I could put it underneath the field, beside the field, but these options all result in extra text, and an interface that looks cluttered to me.&amp;#160; &lt;/p&gt;

&lt;p&gt;I’ve become more and more of a minimalist when it comes to UI design.&amp;#160; I want to put as few things on the page as possible.&amp;#160; So I started thinking, what is the function of the label on a form?&amp;#160; It’s there to tell the user what data to put into a given field.&amp;#160; Now what is the purpose of the label on a form with a validation message?&amp;#160; The validation message and the label are both trying to tell you what goes into a given field.&amp;#160; Maybe it makes sense to combine them.&amp;#160; So I tried that out and the results look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF6Jmj9Dq8C0Cecv43JzSFuCvy1XOvwGzsh2ESU62uH6sQb3MRK8tD3squMhPd2BGdk4j-v-mQAfVjWNvti_lrh6nBXrsQ7t0mZ5szR0hMooUJ927azuQg4-znm3dTgepMP2fhf8PBdzo/s1600-h/image%5B18%5D.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhW_8_0tG_W7MBO6q6csGBmc-7DyoYNENhsCBRwXOrH4weoRQhVhooQRVG8O7UwwjzdAGYeaIThQ_pXY-6VQ3kxQhB-skkMrwyIdSaeU45agNYRxTu1Tekqm07sVl2-CBCSijA3dFNTZmo/?imgmax=800&quot; width=&quot;749&quot; height=&quot;406&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;All right.&amp;#160; This is looking better to me.&amp;#160; The interface is clean and clear.&amp;#160; I have to make sure that my validation messages always start with the field name, and they need to be fairly short, but those both sound like good design constraints so I’m willing to work with them. The ValidationLabel is now the standard way that I’m going to handle error messages in my application.&amp;#160; &lt;/p&gt;

&lt;p&gt;So how should I implement the ValidationLabel?&amp;#160; I’m already using the ValidationMessage Html helper method and that works pretty well, I’ll just customize that and add the Label functionality to it.&amp;#160; I write an extension method for HtmlHelper that will take all of the parameters required to behave as both a label and an error message.&amp;#160; If there’s no error, my helper will render a label.&amp;#160; If there is an error, my helper will render an error message.&amp;#160; Simple.&amp;#160; Here’s how my markup will look with the new helper.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;div&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;form-row&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;background: yellow&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt; Html.ValidationLabel(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Folder.FolderName&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Enter a name for your folder&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;)&lt;span style=&quot;background: yellow&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;background: yellow&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt; Html.TextBoxFor(model=&amp;gt;model.Folder.FolderName, &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; {style=&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;width:300px;&amp;quot;&lt;/span&gt;, maxlength=&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;50&amp;quot;&lt;/span&gt;})&lt;span style=&quot;background: yellow&quot;&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;input&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;SaveButton&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;button-green&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot; Save Folder &amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;input&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;CancelButton&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;button&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;button-gray&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot; Cancel &amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Hmmmm, cleaner interface and cleaner markup.&amp;#160; I like the way this is going.&amp;#160; Now let’s write the ValidationLabel helper method.&amp;#160; It’s actually surprisingly simple.&amp;#160; Here’s the code.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// ValidationLabel&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;static&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;MvcHtmlString&lt;/span&gt; ValidationLabel(&lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;HtmlHelper&lt;/span&gt; htmlHelper, &lt;br /&gt;                                                    &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; modelName, &lt;br /&gt;&lt;span style=&quot;color: blue&quot;&gt;                                                    string&lt;/span&gt; labelText, &lt;br /&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;                                                    IDictionary&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: blue&quot;&gt;object&lt;/span&gt;&amp;gt; htmlAttributes)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (modelName == &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;) { &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;ArgumentNullException&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;modelName&amp;quot;&lt;/span&gt;); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;ModelState&lt;/span&gt; modelState = htmlHelper.ViewData.ModelState[modelName];&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;ModelErrorCollection&lt;/span&gt; modelErrors = (modelState == &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;) ? &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt; : modelState.Errors;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;ModelError&lt;/span&gt; modelError = ((modelErrors == &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;) || (modelErrors.Count == 0)) ? &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt; : modelErrors[0];&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// If there is no error, we want to show a label.&amp;#160; If there is an error,&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// we want to show the error message.&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; tagText = labelText;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; tagClass = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;form_field_label_normal&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; ((modelState != &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;) &amp;amp;&amp;amp; (modelError != &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;))&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tagText = modelError.ErrorMessage;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tagClass = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;form_field_label_error&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Build out the tag&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;TagBuilder&lt;/span&gt; builder = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;TagBuilder&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;span&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; builder.MergeAttributes(htmlAttributes);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; builder.MergeAttribute(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;class&amp;quot;&lt;/span&gt;, tagClass);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; builder.MergeAttribute(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;validationlabelfor&amp;quot;&lt;/span&gt;, modelName );&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; builder.SetInnerText(tagText);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;MvcHtmlString&lt;/span&gt;.Create(builder.ToString(&lt;span style=&quot;color: #2b91af&quot;&gt;TagRenderMode&lt;/span&gt;.Normal));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;Here’s how it works.&amp;#160; This is an extension method on the HtmlHelper class so the first parameter is the HtmlHelper.&amp;#160; Next we pass in the modelName, the labelText, and a dictionary of Html Attributes that can be used for custom formatting.&amp;#160; The first thing we do is make sure that we have a modelName and then check to see if there’s an item in the ModelErrorsCollection for that modelName.&amp;#160; &lt;/p&gt;

&lt;p&gt;Note that modelName is not the name of your entire model, it’s the name of your field.&amp;#160; In my folder form example the model name is “Folder.FolderName”, because I have view model class that contains a Folder object and that Folder object has a property called FolderName.&amp;#160; If you’re ever uncertain about what a field’s modelName is, just load up your page and look at the markup for your input tag. It will look something like this, and your model name is the value that ASP.Net MVC puts in the name attribute for your tag.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon&quot;&gt;input&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;Folder_FolderName&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;maxlength&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;50&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;Folder.FolderName&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;style&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;width&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;300px&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;;&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;text&amp;quot;&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: red&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Let’s get back to our method. Now we know we have a modelName, we know what the modelName is, and we’ve checked the ModelState’s ModelErrorsCollection to see if we have any errors for our modelName.&amp;#160; From this point on the logic is pretty simple. If we do have an error we set the tagText to the error message text and we set the tagClass to “form_field_label_error”.&amp;#160; If we don’t have an error we set the tagText to the labelText and we set tagClass to “form_field_label_normal”.&amp;#160; Then we just use a TagBuilder to render out a span using the tagText and tagClass that we defined.&amp;#160; I also added a custom “validationlabelfor” attribute that holds the model name and is used for client side (javascript) validation.&lt;/p&gt;

&lt;p&gt;So that it! We now have a method that renders our field label, and will magically present any error messages for that field if they exist in the ModelState (In case you’re wondering, ASP.Net MVC automatically stores all errors resulting from model validation or model binding in the ModelState).&amp;#160; Plus the syntax is really clean and simple.&amp;#160; A ValidationLabel, input pair is just 2 lines.&lt;/p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;background: yellow&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt; Html.ValidationLabel(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Folder.FolderName&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Enter a name for your folder&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;)&lt;span style=&quot;background: yellow&quot;&gt;%&amp;gt;&lt;/span&gt; 

&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;background: yellow&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt; Html.TextBoxFor(model=&amp;gt;model.Folder.FolderName, &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; {style=&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;width:300px;&amp;quot;&lt;/span&gt;, maxlength=&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;50&amp;quot;&lt;/span&gt;})&lt;span style=&quot;background: yellow&quot;&gt;%&amp;gt; 
  &lt;br /&gt;&lt;/span&gt;

&lt;p&gt;There is one more thing I want to do though.&amp;#160; Notice how my ValidationLabel requires me to manually enter the modelName “Folder.FolderName”, but the TextBoxFor method uses a lambda instead?&amp;#160; That’s pretty cool, plus it eliminates the need to use a “magic string” in our UI code.&amp;#160; For Part 2 I’m going to cover how we can create our very own strongly-typed ValidationLabelFor method that will take a lambda as a parameter instead of that modelName string. &lt;/p&gt;  </description><link>http://www.referencebits.com/2011/04/creating-validation-label-helper-part-1.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8DEGMVA2dygkAhYLka-dqHXEmKpvG08dCeFhStSWiz0q_fq48-6M2CPLtuR4rwa5EGn_pi4JGLZ65zRJiiMw2gDXY8ht2pc93cS2C3-sAEpu3MvZDIMIdIwd9jW1Xu5wDz1rd_C95imY/s72-c?imgmax=800" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-8038819215751825159</guid><pubDate>Fri, 04 Mar 2011 20:20:00 +0000</pubDate><atom:updated>2011-03-04T14:14:01.039-07:00</atom:updated><title>.Net as an Awesome Startup Platform</title><description>&lt;p&gt;This is the short (10 slide) deck from my presentation at the Denver Tech Trifecta.&amp;#160;&amp;#160; I used slideshare to share the presentation.&amp;#160; If you click the menu button there is a View Fullscreen option.&amp;#160; &lt;/p&gt;  &lt;p&gt;First, this is all my own personal opinion, not the views of Microsoft or anybody associated with the Tech Trifecta. If it seems a little terse that’s because most of the content was spoken.&amp;#160; I favor short slides and lots of talking with code demos. The main theme of the presentation was that classic ASP was an awesome startup platform. Then Microsoft pushed out ASP.Net WebForms, and they took over corporate software development, but they lost the startup community.&amp;#160; I think the reason is that it takes too long and costs too much money to build anything real with WebForms (it’s also painful to make significant changes to an existing app).&amp;#160; Open source tools like Ruby/Rails and PHP on the other hand provide a much faster and lower friction development experience, plus they’re free.&lt;/p&gt;  &lt;p&gt;But now that’s all changed.&amp;#160; Microsoft is supporting startups in a big way with the BizSpark program which will basically give your startup every product that Microsoft makes for FREE for 3 years. The biggest and most important change for me personally is ASP.Net MVC. Three years ago I was ready to throw away a decade of work on Microsoft platforms and go do Ruby/Rails.&amp;#160; WebForms is a never ending source of bad code and frustration for me. Then ASP.Net MVC came along and it was like a love letter written to web developers.&amp;#160; Every place I turned the MVC Framework had given me just the right levels of abstraction and access to support me in what I needed to code. Plus there’s no ridiculous artificial page lifecycle that tries to make the web look like Windows, awesome!&amp;#160; I love MVC and because of it I am excited to keep building on the .Net stack.&amp;#160; Unfortunately, now that I’ve experienced the light, I cry every time I’m forced to go back and work in WebForms.&amp;#160; One place that I do think the page event lifecycle model works well is Silverlight.&amp;#160; Silverlight gives you the ability to write apps with a user experience that is so good that I think building with it will become a competitive advantage for line of business apps.&amp;#160;&amp;#160; All these changes add up to Microsoft once again being an awesome platform to build your web startup on.&lt;/p&gt;  &lt;div style=&quot;width: 425px&quot; id=&quot;__ss_7152884&quot;&gt;&lt;strong style=&quot;margin: 12px 0px 4px; display: block&quot;&gt;&lt;a title=&quot;Net as an awesome startup platform&quot; href=&quot;http://www.slideshare.net/rlacovara/net-as-an-awesome-startup-platform-7152884&quot;&gt;Net as an awesome startup platform&lt;/a&gt;&lt;/strong&gt;&lt;object id=&quot;__sse7152884&quot; width=&quot;425&quot; height=&quot;355&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=netasanawesomestartupplatform-110304143246-phpapp02&amp;amp;stripped_title=net-as-an-awesome-startup-platform-7152884&amp;amp;userName=rlacovara&quot; /&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowScriptAccess&quot; value=&quot;always&quot; /&gt;&lt;embed name=&quot;__sse7152884&quot; src=&quot;http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=netasanawesomestartupplatform-110304143246-phpapp02&amp;amp;stripped_title=net-as-an-awesome-startup-platform-7152884&amp;amp;userName=rlacovara&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;425&quot; height=&quot;355&quot;&gt;&lt;/embed&gt;&lt;/object&gt;    &lt;div style=&quot;padding-bottom: 12px; padding-left: 0px; padding-right: 0px; padding-top: 5px&quot;&gt;View more &lt;a href=&quot;http://www.slideshare.net/&quot;&gt;presentations&lt;/a&gt; from &lt;a href=&quot;http://www.slideshare.net/rlacovara&quot;&gt;rlacovara&lt;/a&gt;.&lt;/div&gt; &lt;/div&gt;  &lt;h4&gt;Startup Links&lt;/h4&gt;  &lt;p&gt;For those who are just looking for the resource links at the end of the presentation, here they are.&amp;#160; These are all great startup resources and well worth checking out, especially if you live in the Boulder/Denver area.&amp;#160; -rudy&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;newtech Meetup&lt;/strong&gt;     &lt;br /&gt;About 4 tech startups present in Denver and Boulder every month. The Denver meetup meets on Auraria Campus. The Boulder meetup has free beer!     &lt;br /&gt;&lt;a href=&quot;http://www.bdnewtech.com/&quot;&gt;http&lt;a href=&quot;http://www.bdnewtech.com/&quot;&gt;://www.bdnewtech.com&lt;/a&gt;&lt;a href=&quot;http://www.bdnewtech.com/&quot;&gt;/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;TechStars     &lt;br /&gt;&lt;/strong&gt;One of the first Y Combinator style accelerator programs for early stage startups.    &lt;br /&gt;&lt;a href=&quot;http://www.techstars.org/&quot;&gt;http&lt;a href=&quot;http://www.techstars.org/&quot;&gt;://www.techstars.org&lt;/a&gt;&lt;a href=&quot;http://www.techstars.org/&quot;&gt;/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Open Angel Forum     &lt;br /&gt;&lt;/strong&gt;Angel Investors meet in Boulder about twice a year to hear pitches from early stage startups and possibly fund them.     &lt;br /&gt;&lt;a href=&quot;http://openangelforum.com/&quot;&gt;http&lt;a href=&quot;http://openangelforum.com/&quot;&gt;://openangelforum.com&lt;/a&gt;&lt;a href=&quot;http://openangelforum.com/&quot;&gt;/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;StartupDigest     &lt;br /&gt;&lt;/strong&gt;Weekly email digest by Chris McCann that tracks all of the tech startup related events in the Boulder / Denver area.     &lt;br /&gt;&lt;a href=&quot;http://startupdigest.com/&quot;&gt;http&lt;a href=&quot;http://startupdigest.com/&quot;&gt;://startupdigest.com&lt;/a&gt;&lt;a href=&quot;http://startupdigest.com/&quot;&gt;/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Lean Startup     &lt;br /&gt;&lt;/strong&gt;Best practices for launching a startup by Eric Reis. Must see video here:    &lt;br /&gt;&lt;a href=&quot;http://ecorner.stanford.edu/authorMaterialInfo.html?mid=2329&quot;&gt;http://ecorner.stanford.edu/authorMaterialInfo.html?mid=2329&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Feld Thoughts     &lt;br /&gt;&lt;/strong&gt;Amazing blog by Brad Feld, Boulder resident and vocal venture capitalist.    &lt;br /&gt;&lt;a href=&quot;http://www.feld.com/wp/&quot;&gt;http&lt;/a&gt;&lt;a href=&quot;http://www.feld.com/wp/&quot;&gt;://www.feld.com/wp/&lt;/a&gt;&lt;/p&gt;  </description><link>http://www.referencebits.com/2011/03/net-as-awesome-startup-platform.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-775408919151509685</guid><pubDate>Mon, 21 Feb 2011 16:06:00 +0000</pubDate><atom:updated>2011-02-21T09:14:20.803-07:00</atom:updated><title>Easy XML Feed using LINQ to XML and ASP.Net MVC2</title><description>&lt;p&gt;I recently had to create an XML feed for an app I’m working on.&amp;#160; The use case is simple.&amp;#160; I have a database of jobs and I need to provide an XML feed containing any jobs that were posted in the last week. I’m going to need to do this for several different 3rd party companies that each use a different XML format, so I want to do a little object oriented design to make sure that creating a feed using a new XML format is as easy as possible.&lt;/p&gt;  &lt;p&gt;I’ve always heard that LINQ to XML makes working with XML much easier, and I know that MVC2 makes it easy to package up and return any type of data so I’m going to use them. It should make for a pretty painless XML feed.&amp;#160; Let’s see.&lt;/p&gt;  &lt;h4&gt;Starting at the controller&lt;/h4&gt;  &lt;p&gt;I want to start by writing the consuming code and let that drive the shape of my business layer code.&amp;#160; That means I start by writing a Feed action method on one of the controller classes in my MVC2 app.&amp;#160; I want to return XML data so i pick a return type of ContentResult, which will allow me to set the content type to “text/xml”.&amp;#160; The logic for this method is simple.&amp;#160; I have a feedKey that distinctly identifies the 3rd party company / XML schema that I want to generate a feed for.&amp;#160; All companies will hit the same action method, but they’ll pass in a different feedKey, which will result in them getting the appropriate XML feed.&amp;#160; My action method takes the feedKey as a parameter and then passes it to a factory method on my FeedService class that returns an instance of the appropriate FeedBuilder object.&amp;#160; The FeedBuilder is the class that I’ll use to encapsulate logic for getting job data and formatting it as an XML document. I’ll have a different FeedBuilder for each feedKey.&amp;#160; The design is a variation on the Strategy pattern.&amp;#160; So, back to controller action method. Here’s what it looks like.&lt;/p&gt;  &lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Feed&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [&lt;span style=&quot;color: #2b91af&quot;&gt;AcceptVerbs&lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;HttpVerbs&lt;/span&gt;.Get)]&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;ContentResult&lt;/span&gt; Feed(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; feedKey)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; feedBuilder = &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.FeedService.GetFeedBuilderFor(feedKey);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.Content(feedBuilder.GetFeedXml(), &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;text/xml&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;h4&gt;The Factory Method&lt;/h4&gt;

&lt;p&gt;The factory method on my FeedService is pretty straight forward.&amp;#160; It just returns an instance of the right FeedBuilder for the feedKey that we pass in to it.&amp;#160; Note that the return type is FeedBuilderBase which is an abstract class that defines the shape of a FeedBuilder.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetFeedBuilderFor&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FeedBuilderBase&lt;/span&gt; GetFeedBuilderFor(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; feedKey)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;switch&lt;/span&gt;(feedKey)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;case&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FeedKey&lt;/span&gt;.SimplyHired:&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;SimplyHiredFeedBuilder&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;case&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FeedKey&lt;/span&gt;.Indeed:&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;IndeedFeedBuilder&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;default&lt;/span&gt;:&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;EmptyFeedBuilder&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;h4&gt;The FeedBuilderBase&lt;/h4&gt;

&lt;p&gt;Remember we’re using a Strategy pattern.&amp;#160; Usually you would define the shape of your strategy class with an interface, but I’m going to use an abstract class because there’s some boilerplate code that will be the same for all of my concrete FeedBuilder classes and I want to implement that code in a single base class.&amp;#160; My abstract class is FeedBuilderBase and it looks like this.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;abstract&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FeedBuilderBase&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// PROPERTIES&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// FeedService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FeedService&lt;/span&gt; _feedService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FeedService&lt;/span&gt; FeedService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_feedService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _feedService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// ABSTRACT METHODS&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetFeedData&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;abstract&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;JobFeedItem&lt;/span&gt;&amp;gt; GetFeedData();&lt;br /&gt;  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// BuildXmlFor&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;abstract&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XDocument&lt;/span&gt; BuildXmlFor(&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;JobFeedItem&lt;/span&gt;&amp;gt; list);&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// HELPER METHODS&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// NewIfNull&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt; T NewIfNull&amp;lt;T&amp;gt;(T obj) &lt;span style=&quot;color: blue&quot;&gt;where&lt;/span&gt; T : &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (obj == &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;) { obj = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; T(); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; obj;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetFeedXml&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; GetFeedXml()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;JobFeedItem&lt;/span&gt;&amp;gt; list = GetFeedData();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;XDocument&lt;/span&gt; xdoc = BuildXmlFor(list);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; sb = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;StringBuilder&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; sw = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;StringWriterUtf8&lt;/span&gt;(sb);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xdoc.Save(sw);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; sb.ToString();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;I have two abstract methods, GetFeedData and BuildXmlFor.&amp;#160; These methods represent the two things that change for each XML feed. Each company has slightly different business rules around what data they want to pull and each company has it’s own proprietary XML format.&amp;#160; These 2 methods will have to be overridden whenever we create a concrete FeedBuilder.&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;

&lt;p&gt;You’ll also notice that we implemented GetFeedXml which is the method we called in our action method to get the text of our XML document. GetFeedXml should have been very easy to write, but it took a little time due to an unexpected encoding problem that I described in &lt;a href=&quot;http://rlacovara.blogspot.com/2011/02/how-to-create-xml-in-c-with-utf-8.html&quot;&gt;How to Create XML in C# with UTF-8 Encoding&lt;/a&gt;. &lt;/p&gt;

&lt;h4&gt;The Concrete FeedBuilder&lt;/h4&gt;

&lt;p&gt;Now I’m ready to create my first concrete FeedBuilder class.&amp;#160; I’m going to start with a jobsite aggregator called SimplyHired.&amp;#160; So I create a new SimplyHiredFeedBuilder class that inherits from FeedBuilderBase an I override my GetFeedData and BuildXmlFor methods.&amp;#160; GetFeedData is just a wrapper for the appropriate data access method in my FeedService class. GetFeedData returns a list of JobFeedItem objects.&amp;#160; JobFeedItem is a simple Data Transfer Object (DTO) that I created to contain the aggregate of data needed to build a feed item.&lt;/p&gt;

&lt;p&gt;BuildXmlFor is more interesting.&amp;#160; It takes a list of JobFeedItem objects returned by our GetFeedData method and transforms it into an XDocument using a LINQ query.&amp;#160; The LINQ query is just a select with a bunch of nested XElement constructors that create the XML elements needed to represent each job.&amp;#160; We then take the expression created to represent the list of jobs as XElements and we wrap it in a single top level XElement called, wait for it…. jobs.&amp;#160; BTW, it is at this point that the LINQ expression actually executes.&amp;#160; Up to now it has just been an expression.&amp;#160; As soon as we use it in the constructor for our jobs XElement the expression compiles and executes. &lt;/p&gt;

&lt;p&gt;So, we now have an XML tree contained in a single XElement called jobs.&amp;#160; To complete the method we just wrap the jobs XElement in a new XDocument, return the XDocument, and we’re done.&amp;#160; &lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;SimplyHiredFeedBuilder&lt;/span&gt;:&lt;span style=&quot;color: #2b91af&quot;&gt;FeedBuilderBase&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// FEEDBUILDERBASE OVERRIDES&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetFeedData&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;override&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;JobFeedItem&lt;/span&gt;&amp;gt; GetFeedData()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.FeedService.GetJobFeedItemsForSimplyHired();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// BuildXmlFor&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;override&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XDocument&lt;/span&gt; BuildXmlFor(&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;JobFeedItem&lt;/span&gt;&amp;gt; list)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; xmlExpression = &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;from&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;JobFeedItem&lt;/span&gt; j &lt;span style=&quot;color: blue&quot;&gt;in&lt;/span&gt; list&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;select&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;job&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;title&amp;quot;&lt;/span&gt;, j.JobTitle),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;job-code&amp;quot;&lt;/span&gt;, j.JobGuid),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;job-board-name&amp;quot;&lt;/span&gt;, j.CompanyName),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;job-board-url&amp;quot;&lt;/span&gt;, j.CompanyJobPageUrl),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;detail-url&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;apply-url&amp;quot;&lt;/span&gt;, GetApplyUrl(j.CompanyKey, j.JobGuid)),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;job-category&amp;quot;&lt;/span&gt;, j.JobCategory),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;description&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;summary&amp;quot;&lt;/span&gt;, j.JobDescription),&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;required-skills&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;), &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;required-education&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;required-experience&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;full-time&amp;quot;&lt;/span&gt;, j.IsFullTime),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;part-time&amp;quot;&lt;/span&gt;, j.IsPartTime),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;flex-time&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;internship&amp;quot;&lt;/span&gt;, j.IsInternship),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;volunteer&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;exempt&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;contract&amp;quot;&lt;/span&gt;, j.IsContract),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;permanent&amp;quot;&lt;/span&gt;, j.IsPermanent),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;temporary&amp;quot;&lt;/span&gt;, j.IsTemp),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;telecommute&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;compensation&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;salary-range&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;salary-amount&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;salary-currency&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;benefits&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;posted-date&amp;quot;&lt;/span&gt;, GetPostedOnDate(j)),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;close-date&amp;quot;&lt;/span&gt;, GetClosedOnDate(j)),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;location&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;address&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;city&amp;quot;&lt;/span&gt;, j.JobCity),&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;state&amp;quot;&lt;/span&gt;, j.JobState),&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;zip&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;country&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;contact&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;, j.ContactName),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;email&amp;quot;&lt;/span&gt;, j.ContactEmail),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;hiring-manager-name&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;hiring-manager-email&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;phone&amp;quot;&lt;/span&gt;, j.ContactPhone),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;fax&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;company&amp;quot;&lt;/span&gt;,&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;, j.CompanyName),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;description&amp;quot;&lt;/span&gt;, j.CompanyDescription),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;industry&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;),&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;url&amp;quot;&lt;/span&gt;, j.CompanyUrl)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; )&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; );&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; jobs =&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XElement&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;jobs&amp;quot;&lt;/span&gt;, xmlExpression);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; declaration = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XDeclaration&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XDocument&lt;/span&gt;(declaration, jobs);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;h4&gt;Conclusion&lt;/h4&gt;

&lt;p&gt;So I built it, tested it, and it works.&amp;#160; Except for the hour that I spend trying to figure out my UTF-8 encoding problem, the LINQ / XElement method was a relatively painless way to create XML. I love the simplicity of the MVC action method and the fact that it gives me a ContentResult type that gives me access the right parts of the stack.&amp;#160; The LINQ / XElement query was concise and the style of passing element data in XElement constructors makes it hard to mess up the document structure. My conclusion is that I like it and I’ll definitely be using this technique again.&lt;/p&gt;  </description><link>http://www.referencebits.com/2011/02/easy-xml-feed-using-linq-to-xml-and.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-8827022942225317017</guid><pubDate>Mon, 14 Feb 2011 06:26:00 +0000</pubDate><atom:updated>2011-02-14T23:47:58.909-07:00</atom:updated><title>How To Create XML in C# with UTF-8 Encoding</title><description>&lt;p&gt;I can’t believe that I spent over an hour on this problem.&amp;#160; It’s one of those things that you can’t understand how it’s possible that it doesn’t just work. But it doesn’t, so here’s the solution I found for anyone who might have the same issue.&lt;/p&gt;  &lt;h4&gt;The Problem: My XML refuses to use UTF-8 encoding&lt;/h4&gt;  &lt;p&gt;I have some code where I’m doing a query and creating an XML doc from the data that’s returned.&amp;#160; I used LINQ to XML to create my XML and package it up in an XDocument.&amp;#160; The XDocument allows me to set the XML version and the encoding. Here’s the code I used to create and return my XDocument.&lt;/p&gt;  &lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; declaration = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XDeclaration&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;yes&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;XDocument&lt;/span&gt;(declaration, jobs);&lt;/pre&gt;

&lt;p&gt;So far so good.&amp;#160; Now the last (and what should be trivial) step is to write that XML to a string and return it.&amp;#160; This is where things go wrong.&amp;#160; To get an XDocument object to write out a full XML document you call it’s Save method.&amp;#160; No problem, the Save method has an overload that takes a StringWriter parameter so I’ll just create a StringWriter and a StringBuilder and I’ll be in business.&amp;#160; Here’s my initial code.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetFeedXML&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; GetFeedXML()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;JobFeedItem&lt;/span&gt;&amp;gt; list = GetFeedData();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;XDocument&lt;/span&gt; xdoc = BuildXmlFor(list);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; sb = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;StringBuilder&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; sw = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;StringWriter&lt;/span&gt;(sb);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xdoc.Save(sw);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; sb.ToString();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;That all looks good. So I run it and here’s the result.&lt;/p&gt;

&lt;p&gt;&lt;font color=&quot;#008000&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-16&amp;quot; standalone=&amp;quot;yes&amp;quot; ?&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;jobs&amp;gt; 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;job&amp;gt; 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;title&amp;gt;&lt;b&gt;Audit Engagement Manager&lt;/b&gt;&amp;lt;/title&amp;gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;What the heck is that???&amp;#160; UTF-16??&amp;#160; It’s like the UTF-8 encoding that I set in my XDocument was completely ignored and .Net decided to use UTF-16 encoding.&amp;#160; How can this not work?&amp;#160; Where else do I even have an option to set the encoding? The StringWriter won’t let me set the encoding, the StringBuilder certainly won’t let me set the encoding.&lt;/p&gt;

&lt;p&gt;So I started Googling and found that other people were having the same problem and I found several solutions that sounded plausible.&amp;#160; Most of them centered around creating an XMLWriter with a settings object that would allow me to set the encoding.&amp;#160; All of these required a fair bit of extra code and I never got one of them to actually work, but they did get me looking at the StringWriter as the place where I needed to set my encoding.&lt;/p&gt;

&lt;h4&gt;The Solution: Subclass StringWriter&lt;/h4&gt;

&lt;p&gt;At this point I’m looking into progressively more and more complex solutions when I stumble across a post by Ian Dykes called &lt;a href=&quot;http://devproj20.blogspot.com/2008/02/writing-xml-with-utf-8-encoding-using.html&quot;&gt;Writing XML with UTF-8 Encoding using XmlTextWriter and StringWriter&lt;/a&gt;.&amp;#160; Ian basically says to create a new StringWriterWithEncoding class that inherits from StringWriter but allows you to set the encoding in the constructor.&amp;#160; I used the same idea and created the StringWriterUtf8 class below.&amp;#160; Instead of taking the encoding in a constructor, I opted to make the Encoding property always return UTF8.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;StringWriterUtf8&lt;/span&gt; : &lt;span style=&quot;color: #2b91af&quot;&gt;StringWriter&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; StringWriterUtf8(&lt;span style=&quot;color: #2b91af&quot;&gt;StringBuilder&lt;/span&gt; sb) : &lt;span style=&quot;color: blue&quot;&gt;base&lt;/span&gt;(sb)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;override&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Encoding&lt;/span&gt; Encoding&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Encoding&lt;/span&gt;.UTF8; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;Now I just need to use StringWriterUtf8 in my code instead of StringWriter and I’ll be using UTF-8 encoding in my writer.&amp;#160; I did it, I tested it, and it worked.&amp;#160; My XML output now looks like this:&lt;/p&gt;
&lt;font color=&quot;#008000&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; standalone=&amp;quot;yes&amp;quot; ?&amp;gt; 
  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;jobs&amp;gt; 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;job&amp;gt; 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;title&amp;gt;&lt;b&gt;Audit Engagement Manager&lt;/b&gt;&amp;lt;/title&amp;gt;&lt;/font&gt; 

&lt;p&gt;Thank you Ian for understanding more about this than I do and taking the time to put the solution out on your blog.&amp;#160; By the way, I think it’s worth mentioning that several people posted more “settings type” solutions in the comments to Ian’s post.&amp;#160; I tried them.&amp;#160; They didn’t work for me.&amp;#160; The only thing that did work was Ian’s idea of subclassing the StringWriter.&amp;#160; So I now have a working method and here’s the final version of GetFeedXml that uses my StringWriterUtf8 class.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetFeedXML&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; GetFeedXML()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;JobFeedItem&lt;/span&gt;&amp;gt; list = GetFeedData();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;XDocument&lt;/span&gt; xdoc = BuildXmlFor(list);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; sb = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;StringBuilder&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; sw = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;StringWriterUtf8&lt;/span&gt;(sb);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xdoc.Save(sw);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; sb.ToString();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;  </description><link>http://www.referencebits.com/2011/02/how-to-create-xml-in-c-with-utf-8.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>9</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-8100085417723009739</guid><pubDate>Tue, 08 Feb 2011 07:34:00 +0000</pubDate><atom:updated>2011-02-08T08:56:44.489-07:00</atom:updated><title>Refactoring a TrySave Method</title><description>&lt;p&gt;When writing code, it’s unusual for me to get it right the first time.&amp;#160; Typically I’ll just write a big block of statements that do something that I’ll need done.&amp;#160; Then I’ll verify that it works.&amp;#160; Then I’ll look at it, notice what an ugly mess it is, and refactor it so that the poor developer who inherits this code (and that developer might very well be me) will have some hope of maintaining it. Here’s a typical example that came up this week.&lt;/p&gt;  &lt;h4&gt;The scenario&lt;/h4&gt;  &lt;p&gt;I’m working in a controller class in an ASP.Net MVC2 application.&amp;#160; I have some action methods that need to save the data passed in via a signup form.&amp;#160; The&amp;#160; signup form collects all of the data needed to create a new company and a new user in my application.&amp;#160; So my signup form is a composite of Company data, User data, an other data that’s needed for the UI (like a ConfirmPassword field).&amp;#160; I’ve chosen to encapsulate all of this stuff that the signup form needs in a view model class called SignupVM.&amp;#160; It looks like this.&lt;/p&gt;  &lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;SignupVM&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;User&lt;/span&gt; User { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Company&lt;/span&gt; Company { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; ConfirmPassword { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; FormError { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; FormMessage { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; SignupVM()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.User = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;User&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.Company = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Company&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.ConfirmPassword = &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.FormError = &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.FormMessage = &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;So that part works really well.&amp;#160; Here’s where it starts to get a little messy.&amp;#160; I have a couple of Action Methods in my controller that receive a SignupVM that’s been populated with data by the View (or more accurately, the ModelBinder) and they need to save that data.&amp;#160; Saving data involves three main steps.&amp;#160; First I need to validate the Company, the User, and my ConfirmPassword field.&amp;#160; Second I need to actually save the data, but I need to save the Company first and then make sure I set the correct CompanyGuid on the User before I save the User data. Third, I have some standard data that needs to get created for all new signups.&amp;#160; Right now the only standard data is the GeneralApplicantsJob but I know that there will be a need for more standard data down the road.&amp;#160; To handle this stuff, I created a TrySave() method in my controller class that looks like this.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// TrySave - SignupVM&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;bool&lt;/span&gt; TrySave(&lt;span style=&quot;color: #2b91af&quot;&gt;SignupVM&lt;/span&gt; vm)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; userValidator = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;UserValidator&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; companyValidator = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;CompanyValidator&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; isError = &lt;span style=&quot;color: blue&quot;&gt;false&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// validate user&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; userValidator.Validate(vm.User);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidateConfirmPassword(vm);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;RuleException&lt;/span&gt; e)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isError = &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.SetUiFieldNames(GetUserFieldNameMappings());&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.CopyToModelState(ModelState, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;User&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// validate company&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; companyValidator.Validate(vm.Company);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;RuleException&lt;/span&gt; e)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isError = &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.SetUiFieldNames(GetCompanyFieldNameMappings());&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.CopyToModelState(ModelState, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Company&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// validate vm level fields&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidateConfirmPassword(vm);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;RuleException&lt;/span&gt; e)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isError = &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.CopyToModelState(ModelState, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// if error, return false&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (isError) { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;false&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// if no error let&#39;s&lt;/span&gt;&lt;span style=&quot;color: green&quot;&gt; save the Company&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.CompanyService.Save(vm.Company);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// set the companyguid for the user&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; vm.User.CompanyGuid = vm.Company.CompanyGuid;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//save the user&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.UserService.Save(vm.User);&lt;br /&gt;            &lt;span style=&quot;color: green&quot;&gt;// create the GeneralApplicants job, it&#39;s created the first time we get it.&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.JobService.GetGeneralApplicantsJobForCompany(vm.Company.CompanyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;h4&gt;Let’s Refactor&lt;/h4&gt;

&lt;p&gt;So, this block of code isn’t unmanageable, but I could definitely see it becoming unmanageable if I leave it like this and it gets added to a few times.&amp;#160; Plus, it’s not immediately clear what this code is doing.&amp;#160; What am I returning? Where am I returning it?&amp;#160; What logic decides what my return value is? Looking at it I realize that I’m writing in a very Imperative style that puts the focus on the individual steps of “how” to do something, instead of writing in a Declarative (or Functional) style that puts the focus on “what” is being done.&amp;#160; The code above is just a big list of steps that are a little difficult to follow without the comments.&amp;#160; I’m just validating data and saving data, so why do I have all of these statements in my method?&amp;#160; I can do better. &lt;/p&gt;

&lt;p&gt;First let’s extract all of the validation code into an IsValidSignup() method.&amp;#160; Just that one refactoring helps quite a bit.&amp;#160; Now our TrySave method looks like this.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// TrySave - SignupVM&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;bool&lt;/span&gt; TrySave(&lt;span style=&quot;color: #2b91af&quot;&gt;SignupVM&lt;/span&gt; vm)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!IsValidSignup(vm)) { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;false&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// if no error let&#39;s save the Company&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.CompanyService.Save(vm.Company);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// set the companyguid for the user&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; vm.User.CompanyGuid = vm.Company.CompanyGuid;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//save the user&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.UserService.Save(vm.User);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// create the GeneralApplicants job, it&#39;s created the first time we get it.&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.JobService.GetGeneralApplicantsJobForCompany(vm.Company.CompanyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;The next step is to refactor the data saving code.&amp;#160; There’s two things that bother me about this code.&amp;#160; First it’s critical that the the entities (Company and User) are saved it the right order, and that the user.CompanyGuid needs to be set in between the saves.&amp;#160; Second, it’s critical that the GeneralApplicantsJobForCompany get created after the Company has been saved, plus I know that there will be a need for more standard data later on, which means at some point a developer is going to have to modify this helper method in a controller class to make sure signups are created with the right standard data.&amp;#160; That feels like something that should be in my business layer, not in my UI code.&amp;#160; So, I decided to extract all of the persistence logic (including the creation of standard data) to a new method on my SystemService class in my business layer. The mehtod is called SignupNewCompany() and now my TrySave method looks like this.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// TrySave - SignupVM&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;bool&lt;/span&gt; TrySave(&lt;span style=&quot;color: #2b91af&quot;&gt;SignupVM&lt;/span&gt; vm)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!IsValidSignup(vm)) { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;false&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.SystemService.SignupNewCompany(vm.Company, vm.User);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;This looks much cleaner to me.&amp;#160; The focus is on what I’m doing instead of how I’m doing it, and the intent of the code is much more clear even though it no longer has comments.&amp;#160; Now I do have three methods instead of one, but each of those three methods does one thing that is easy to understand at a glance.&amp;#160; Best of all, I realized that I had put some logic in my UI that really needed to live in my business layer where it would be easier to reuse and easier to maintain.&amp;#160; For anyone who’s interested, here are all 3 methods in their final form.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// TrySave - SignupVM&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;bool&lt;/span&gt; TrySave(&lt;span style=&quot;color: #2b91af&quot;&gt;SignupVM&lt;/span&gt; vm)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!IsValidSignup(vm)) { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;false&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.SystemService.SignupNewCompany(vm.Company, vm.User);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&lt;/pre&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// IsValidSignup&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;bool&lt;/span&gt; IsValidSignup(&lt;span style=&quot;color: #2b91af&quot;&gt;SignupVM&lt;/span&gt; vm)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; userValidator = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;UserValidator&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; companyValidator = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;CompanyValidator&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; isError = &lt;span style=&quot;color: blue&quot;&gt;false&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// validate user&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; userValidator.Validate(vm.User);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;RuleException&lt;/span&gt; e)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isError = &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.SetUiFieldNames(GetUserFieldNameMappings());&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.CopyToModelState(ModelState, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;User&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// validate company&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; companyValidator.Validate(vm.Company);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;RuleException&lt;/span&gt; e)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isError = &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.SetUiFieldNames(GetCompanyFieldNameMappings());&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.CopyToModelState(ModelState, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Company&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// validate vm level fields&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ValidateConfirmPassword(vm);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;RuleException&lt;/span&gt; e)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; isError = &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.ErrorList.CopyToModelState(ModelState, &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; !isError;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&lt;/pre&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; SignupNewCompany( &lt;span style=&quot;color: #2b91af&quot;&gt;Company&lt;/span&gt; newCompany, &lt;span style=&quot;color: #2b91af&quot;&gt;User&lt;/span&gt; newUser)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// create the new company&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.CompanyService.Insert(newCompany);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// create the new user&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newUser.CompanyGuid = newCompany.CompanyGuid;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.UserService.Save(newUser);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// create the GeneralApplicants job, it&#39;s created the first time we get it.&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.JobService.GetGeneralApplicantsJobForCompany(newCompany.CompanyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;  </description><link>http://www.referencebits.com/2011/02/refactoring-trysave-method.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-6162446856366008803</guid><pubDate>Mon, 17 Jan 2011 17:57:00 +0000</pubDate><atom:updated>2011-01-17T11:01:21.255-07:00</atom:updated><title>How To Not Hate Dependency Injection.</title><description>&lt;p&gt;The Dependency Injection pattern is all the rage these days and more and more DI containers are becoming part of how we build .Net applications.&amp;#160; I still have a love / hate relationship with Dependency Injection.&amp;#160; I love the idea of being able to inject any external dependencies into my classes, thereby breaking those dependencies, but I hate the idea of creating an interface for every class that I need to inject, then maintaining that interface as well as any classes that implement it.&amp;#160; Let me show you what I mean.&lt;/p&gt;  &lt;h4&gt;Dependency Injection on an MVC&amp;#160; Controller Base Class With Interfaces&lt;/h4&gt;  &lt;p&gt;Typical scenario, I have an ASP.Net MVC2 application that needs to get data from a database. All of my data access code is contained in service classes like UserService, SystemService, CompanyService, etc.&amp;#160; I don’t want to manually declare and instantiate whatever service classes I need in each of my controller classes, so I created an abstract base class for my controllers called AtsControllerBase which has properties that will provide instances of my service classes using a lazy creation pattern.&amp;#160; Here’s an abbreviated version of the base class.&lt;/p&gt;  &lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;abstract&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;AtsControllerBase&lt;/span&gt; : &lt;span style=&quot;color: #2b91af&quot;&gt;Controller&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//******************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// PROPERTIES&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//****************************************************** &lt;/span&gt;&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// StateBox&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;HfStateBox&lt;/span&gt; _stateBox;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;HfStateBox&lt;/span&gt; StateBox&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_stateBox); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _stateBox = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// AuthToken&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;AuthToken&lt;/span&gt; AuthToken { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; StateBox.AuthToken; } }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// ApplicantService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;ApplicantService&lt;/span&gt; _applicantService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;ApplicantService&lt;/span&gt; ApplicantService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_applicantService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _applicantService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CompanyService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;CompanyService&lt;/span&gt; _companyService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;CompanyService&lt;/span&gt; CompanyService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_companyService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _companyService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// FolderService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderService&lt;/span&gt; _folderService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderService&lt;/span&gt; FolderService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_folderService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _folderService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// JobService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;JobService&lt;/span&gt; _jobService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;JobService&lt;/span&gt; JobService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_jobService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _jobService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// SecurityService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;SecurityService&lt;/span&gt; _securityService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;SecurityService&lt;/span&gt; SecurityService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_securityService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _securityService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// SystemService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;SystemService&lt;/span&gt; _systemService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;SystemService&lt;/span&gt; SystemService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_systemService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _systemService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// ServicePlanService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;ServicePlanService&lt;/span&gt; _servicePlanService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;ServicePlanService&lt;/span&gt; ServicePlanService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_servicePlanService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _servicePlanService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// UserService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;UserService&lt;/span&gt; _userService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;UserService&lt;/span&gt; UserService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_userService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _userService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// WorkflowService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;WorkflowService&lt;/span&gt; _workflowService;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;WorkflowService&lt;/span&gt; WorkflowService&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt; { &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; NewIfNull(_workflowService); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt; { _workflowService = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//******************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// UTILITY METHODS&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//******************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// NewIfNull&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; T NewIfNull&amp;lt;T&amp;gt;(T obj) &lt;span style=&quot;color: blue&quot;&gt;where&lt;/span&gt; T:&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (obj == &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;) { obj = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; T(); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; obj;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;This pattern works well for me because my concrete controllers never need to instantiate a service, they can just access any of the service properties in the base class.&amp;#160; I don’t have the overhead of newing up a bunch of service classes that the concrete controller won’t use because the services aren’t instantiated until the first time the service property is accessed.&amp;#160; That’s the lazy creation part.&amp;#160; Finally, each property has a public setter, so I can replace any of my services if I need to inject a mock or stub or something.&lt;/p&gt;

&lt;p&gt;So this already looks a lot like Dependency Injection.&amp;#160; After all we have public setters for each of the services so we could switch them out if needed.&amp;#160; There’s one thing missing though.&amp;#160; We have no interfaces.&amp;#160; Our properties are all using the concrete type of the service class (like FolderService) instead of some public interface (like IFolderService).&amp;#160; So we could inject something, but it would have to be another instance of FolderService.&amp;#160; That get’s us nowhere. How can we inject something else like a MockFolderService??&amp;#160;&amp;#160; Hmmmm,&amp;#160; I guess the right way to implement Dependency Injection is to create an IFolderService interface for my FolderService class.&amp;#160; At least that’s the pattern that I always see in books and code samples.&amp;#160; Let’s try that.&amp;#160; Ok, FolderService only has 10 public methods.&amp;#160; That’s not too bad so we’ll start with that one.&amp;#160; Here’s the service code.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;partial&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderService&lt;/span&gt; : &lt;span style=&quot;color: #2b91af&quot;&gt;ServiceBase&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// FOLDER METHODS&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//**************************************************************************************&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetFolderByFolderGuid&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; GetFolderByFolderGuid(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; folderGuid)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; sql = &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;SELECT * &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM [Folder]&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WHERE [FolderGuid] = @FolderGuid&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(sql);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Parameters.Add(dao.CreateParameter(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;@FolderGuid&amp;quot;&lt;/span&gt;, folderGuid));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dao.GetSingle&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;&amp;gt;(command);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetListOfFolderWithCountsForCompany&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt; GetListOfFolderWithCountsForCompany(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; sql = &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;SELECT f.*, (SELECT COUNT([ApplicantGuid]) &lt;br /&gt;                          FROM [ApplicantInFolder] WHERE [FolderGuid] = f.[FolderGuid]) As ApplicantCount&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM [Folder] f&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WHERE f.[CompanyGuid] = @CompanyGuid&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(sql);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Parameters.Add(dao.CreateParameter(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;@CompanyGuid&amp;quot;&lt;/span&gt;, companyGuid));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; (dao.GetList&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt;(command));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetListOfFoldersForCompany&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;&amp;gt; GetListOfFoldersForCompany(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; sql = &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;SELECT f.*&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM [Folder] f&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WHERE f.[CompanyGuid] = @CompanyGuid&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(sql);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Parameters.Add(dao.CreateParameter(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;@CompanyGuid&amp;quot;&lt;/span&gt;, companyGuid));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; (dao.GetList&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;&amp;gt;(command));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetListOfFoldersForApplicant&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;&amp;gt; GetListOfFoldersForApplicant(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; applicantGuid)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; sql = &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;select f.*&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; from Folder f&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; join ApplicantInFolder aif on aif.FolderGuid = f.FolderGuid&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where aif.ApplicantGuid = @ApplicantGuid&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(sql);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Parameters.Add(dao.CreateParameter(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;@ApplicantGuid&amp;quot;&lt;/span&gt;, applicantGuid));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; (dao.GetList&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;&amp;gt;(command));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetCachedListOfFoldersForCompany&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt; GetCachedListOfFoldersForCompany(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (companyGuid.Equals(&lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullGuid))&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// return empty list if a null guid was passed&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; key = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;__GetCachedListOfFoldersForCompany_&amp;quot;&lt;/span&gt; + companyGuid.ToString();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;CacheHelper&lt;/span&gt; helper = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;CacheHelper&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt; list = helper.TryGet&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt;&amp;gt;(key);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (list == &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; RefreshCachedListOfFoldersForCompany(companyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; list = helper.TryGet&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt;&amp;gt;(key);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; list;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// RefreshCachedListOfFoldersForCompany&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; RefreshCachedListOfFoldersForCompany(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; key = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;__GetCachedListOfFoldersForCompany_&amp;quot;&lt;/span&gt; + companyGuid.ToString();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;CacheHelper&lt;/span&gt; helper = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;CacheHelper&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt; list = helper.TryGet&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt;&amp;gt;(key);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; list = GetListOfFolderWithCountsForCompany(companyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; helper.Add(key, list);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Insert&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; Insert(&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; folder)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.FolderPersister.Insert(folder);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Save&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; Save(&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; folder)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.FolderPersister.Save(folder);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Delete&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; Delete(&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; folder)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.FolderPersister.Delete(folder.FolderGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// AddApplicantToFolder&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; AddApplicantToFolder(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; applicantGuid, &lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid, &lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; folder)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.FolderPersister.SafeInsertApplicantInFolder(applicantGuid, folder.FolderGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; RefreshCachedListOfFoldersForCompany(companyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;We’ve basically got 10 public data access methods. Now I need to create a public interface that defines all of the public methods, so I’m going to create a new IFolderService interface.&amp;#160; ReSharper even makes it easy for me with a handy Extract Interface tool.&amp;#160; So my interface looks like this.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;interface&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;IFolderService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; GetFolderByFolderGuid(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; folderGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt; GetListOfFolderWithCountsForCompany(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;&amp;gt; GetListOfFoldersForCompany(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;&amp;gt; GetListOfFoldersForApplicant(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; applicantGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderWithCount&lt;/span&gt;&amp;gt; GetCachedListOfFoldersForCompany(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; RefreshCachedListOfFoldersForCompany(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; Insert(&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; folder);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; Save(&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; folder);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; Delete(&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; folder);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; AddApplicantToFolder(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; applicantGuid, &lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; companyGuid, &lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; folder);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; SharedSqlDao { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;ApplicantPersister&lt;/span&gt; ApplicantPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;CareerSiteSettingsPersister&lt;/span&gt; CareerSiteSettingsPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;CompanyPersister&lt;/span&gt; CompanyPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;CompanySettingsPersister&lt;/span&gt; CompanySettingsPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;FolderPersister&lt;/span&gt; FolderPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;HistoryItemPersister&lt;/span&gt; HistoryItemPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;JobPersister&lt;/span&gt; JobPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;JobStubPersister&lt;/span&gt; JobStubPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;ResumePersister&lt;/span&gt; ResumePersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;UserPersister&lt;/span&gt; UserPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;WorkflowPersister&lt;/span&gt; WorkflowPersister { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; T NewIfNull&amp;lt;T&amp;gt;(T obj) &lt;span style=&quot;color: blue&quot;&gt;where&lt;/span&gt; T : &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; GetSqlCsv(&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;&amp;gt; list);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; GetSqlCsv(&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt;&amp;gt; list);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;There, that wasn’t too bad. Now I just need to change my controller FolderService property to type IFolderService and I have Dependency Injection implemented for the FolderService.&amp;#160; I can create new classes that implement IFolderService and inject them into my controller using the public FolderService property like this.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; controller = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderController&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;IFolderService&lt;/span&gt; mock = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;MockFolderService&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; controller.FolderService = mock;&lt;/pre&gt;

&lt;p&gt;That’s great! Now I just need to do that for… wait… I’m going to have to do that for every service class in my application.&amp;#160; Then I’m going to have to maintain each of those interfaces every time I make a change to a service class.&amp;#160; That’s a lot of extra code and a lot of work that’s going to have to be repeated every time I make a change to my service classes.&amp;#160; Why am I doing this again?&amp;#160; This is starting to feel like the hate part of the love / hate relationship.&lt;/p&gt;

&lt;h4&gt;Dependency Injection on an MVC&amp;#160; Controller Base Class Without Interfaces&lt;/h4&gt;

&lt;p&gt;So I don’t really want to create an interface for every single dependency in my application and then have to maintain that interface as well as my concrete classes, but what other option do I have?&amp;#160; Everybody implements DI using interfaces right?&amp;#160; Interfaces are like a badge of honor that demonstrate you know how to architect an application the right way.&amp;#160; You haven’t been to architecture land until you have your interface badge. We love interfaces! &lt;/p&gt;

&lt;p&gt;Well I don’t love any code that I have to spend extra time maintaining every time I make a change to my application.&amp;#160; All that stuff adds up after a while.&amp;#160; I call it the death of a thousand cuts.&amp;#160; One day you look up and all those little extra tasks like maintaining interfaces add up to a mountain of friction that makes it miserable to make changes to your code.&amp;#160; Now there are places for interfaces, but if possible I’m opting for an easier alternative.&lt;/p&gt;

&lt;p&gt;So step one in the easier alternative, throw out the interface that I just created and change my FolderService property back to type FolderService (no more IFolderService).&amp;#160; Step two…. actually we don’t need a step two we’re done.&amp;#160; Lets take a closer look at one of the data access methods in our FolderService class.&lt;/p&gt;
&lt;font face=&quot;Courier New&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetFolderByFolderGuid&lt;/span&gt; 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; GetFolderByFolderGuid(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; folderGuid) 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; sql = &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;SELECT * &lt;/span&gt;

  &lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM [Folder]&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;

  &lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WHERE [FolderGuid] = @FolderGuid&amp;quot;&lt;/span&gt;; 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao; 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(sql); 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Parameters.Add(dao.CreateParameter(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;@FolderGuid&amp;quot;&lt;/span&gt;, folderGuid)); 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dao.GetSingle&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;&amp;gt;(command); 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } 

  &lt;br /&gt;&lt;/font&gt;

&lt;p&gt;Hmmm, that’s a virtual method.&amp;#160; That means that I can create a class that inherits from FolderService and just override the GetFolderByFolderGuid() method.&amp;#160; The FolderService class actually functions as my interface in this scenario.&amp;#160;&amp;#160; So, if I’m writing a unit test where I want to stub out my FolderService I could create the following class.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderServiceStub&lt;/span&gt;: &lt;span style=&quot;color: #2b91af&quot;&gt;FolderService&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;override&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; GetFolderByFolderGuid(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; folderGuid)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;() {FolderGuid = folderGuid, FolderName = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Test Folder&amp;quot;&lt;/span&gt;};&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;FolderServiceStub inherits from FolderService so I can use that stub class anywhere that an object of type FolderService is expected.&amp;#160; The end result is that my testing code (or any other DI code) winds up looking almost exactly like my code did with full on interface based Dependency injection&lt;/p&gt;
&lt;font face=&quot;Courier New&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var&lt;/span&gt; controller = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderController&lt;/span&gt;(); 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;FolderService&lt;/span&gt; mock = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;FolderServiceStub&lt;/span&gt;(); 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; controller.FolderService = mock;&lt;/font&gt; 

&lt;h4&gt;Summary&lt;/h4&gt;

&lt;p&gt;So, make all of your public methods virtual and you can implement a Poor Man’s Dependency Injection that doesn’t require interfaces.&amp;#160; It’s not the right solution every situation, but I’ve found that it works most of the time for me. It also has the one quality that I prize most in code.&amp;#160; It’s so simple that I forget it’s even there. &lt;/p&gt;  </description><link>http://www.referencebits.com/2011/01/one-way-to-not-hate-dependency.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>7</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-968354216371605057</guid><pubDate>Mon, 10 Jan 2011 06:23:00 +0000</pubDate><atom:updated>2011-01-09T23:23:20.489-07:00</atom:updated><title>AAPL Part 7: Building a Generic Data Mapper</title><description>&lt;p&gt;I got bored with this series a while back and I haven’t been writing on it, mostly because I’ve been using the architecture (Agile ADO.Net Persistence layer) to write a bunch of code.&amp;#160; The basic idea of AAPL was to create an application architecture that embraced code changes instead of resisting them. I took a look back at all of the application architectures I’ve worked with over the years and thought carefully about the parts of those architectures that caused me pain whenever made changes to the application.&amp;#160; I then threw all those things out and tried to put together an application architecture based on ADO.Net (not LINQ, EF, or nHibernate) that was optimized for making code changes easy and reliable.&amp;#160; I’ve been using the AAPL architecture for a while now and I’m happy to report that those decisions have really paid off.&amp;#160; &lt;/p&gt;  &lt;h4&gt;Why are we doing this again?&lt;/h4&gt;  &lt;p&gt;Just a a quick reminder.&amp;#160; The point of this architecture is to give us the ability to write persistence code that looks like this.&lt;/p&gt;  &lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetFolderByFolderGuid&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;virtual&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt; GetFolderByFolderGuid(&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; folderGuid)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; sql = &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;SELECT * &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM [Folder]&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WHERE [FolderGuid] = @FolderGuid&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(sql);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Parameters.Add(dao.CreateParameter(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;@FolderGuid&amp;quot;&lt;/span&gt;, folderGuid));&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dao.GetSingle&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Folder&lt;/span&gt;&amp;gt;(command);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;Whenever we need a new data access method we want to just write the new query, package it up in an ADO.Net SqlCommand object, then pass both the SqlCommand and the desired model class type to a Data Access Object that will then handle all of the plumbing required to execute the command, get the data, package it up as whatever model type we passed in and then give it back. This is a very low friction way to handle data access.&amp;#160; It’s easy to maintain, new data methods take seconds to create, and we never have to touch the plumbing code.&amp;#160;&amp;#160; The end result is that maintaining code and making changes to our model should feel like this…&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://akorra.com/wp-content/uploads/2010/03/The-Naked-Clowns.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;instead of this…&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://1.bp.blogspot.com/_WvAhLAGzlZ4/S-G5L4j63wI/AAAAAAAAACw/sVoPsRExevc/s1600/Angry%2520Computer%2520Guy22.jpg&quot; width=&quot;408&quot; height=&quot;274&quot; /&gt;&lt;/p&gt;

&lt;h4&gt;What is a data mapper?&lt;/h4&gt;

&lt;p&gt;The key piece of plumbing code that makes this architecture so painless is the GenericDataMapper.&amp;#160; As described above, when we read data we execute an SQLCommand, get the resulting IDataReader, then map the data in that IDataReader to a model class (an entity class).&amp;#160; The DataMapper is the class that encapsulates the code that knows how to map the fields from an IDataReader to the properties in a model class.&amp;#160; For example, the UserMapper class contains the logic needed to take an IDataReader and use it’s data to populate a new object of type User.&lt;/p&gt;

&lt;h4&gt;What does the Generic Data Mapper do?&lt;/h4&gt;

&lt;p&gt;We could write a separate DataMapper class for each model type.&amp;#160; We could have a UserMapper, a CompanyMapper, a JobMapper, a WorkflowMapper, an ActionMapper, a StepMapper… you get the idea.&amp;#160; But doing it that way would require that 1) We write all those DataMapper&amp;#160; classes, and 2) We modify those DataMapper classes whenever we make a change to one of our models.&amp;#160; No thanks.&lt;/p&gt;

&lt;p&gt;It would be better to write a single DataMapper class that could handle the mapping for any model class that we passed to it. Remember that early on we made the decision that the columns in our database would have the exact same names as the data properties in our model classes.&amp;#160; That constraint makes the mapping logic pretty simple.&amp;#160; We can create a GenericDataMapper that uses reflection to find all the properties on our model class and matches them to any fields with the same name in our IDataReader.&amp;#160; No hard coded mapping logic needed.&amp;#160; Just a little bit of reflection and a standard naming convention and we can create a single GenericDataMapper that will handle mapping for all of our model classes.&lt;/p&gt;

&lt;h4&gt;DataMapperFactory&lt;/h4&gt;

&lt;p&gt;We use a DataMapperFactory to get the right DataMapper for a model type.&amp;#160; The logic is simple.&amp;#160; We have a GetMapper() method that takes a type as it’s parameter. It checks to see if we have a custom mapper for the type, if not it returns the GenericMapper .&amp;#160; In the example below we have just one custom data mapper, ListItemDTOMapper.&amp;#160; Every other model class is handled by the GenericMapper.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;DataMapperFactory&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt; GetMapper(&lt;span style=&quot;color: #2b91af&quot;&gt;Type&lt;/span&gt; dtoType)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;switch&lt;/span&gt;(dtoType.Name)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;case&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;ListItemDTO&amp;quot;&lt;/span&gt;:&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;ListItemDTOMapper&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;default&lt;/span&gt;:&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;GenericMapper&lt;/span&gt;(dtoType);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;h4&gt;IDataMapper&lt;/h4&gt;

&lt;p&gt;All of our DataMappers implement a custom IDataMapper interface. &lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;interface&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Main method that populates dto with data&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;Object&lt;/span&gt; GetData(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Gets the num results returned. Needed for data paging.&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; GetRecordCount(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;IDataMapper requires that we implement just two methods.&amp;#160; GetData() takes an IDataReader and returns a model class that contains the data for the current record in that reader (note that one call to GetData() returns one model object, lists can be created by calling GetData() multiple times ).&amp;#160; GetRecordCount() will return the total number of records returned by the IDataReader and is used for data paging.&amp;#160; So these are the only two public methods that we’ll need to implement.&lt;/p&gt;

&lt;h4&gt;GenericMapper&lt;/h4&gt;

&lt;p&gt;Now we get down to it.&amp;#160; Here is the complete code for the GenericMapper. &lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;GenericMapper&lt;/span&gt; : &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; System.&lt;span style=&quot;color: #2b91af&quot;&gt;Type&lt;/span&gt; DtoType { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; } &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;bool&lt;/span&gt; _isInitialized = &lt;span style=&quot;color: blue&quot;&gt;false&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt;&amp;gt; PropertyOrdinalMappings;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; GenericMapper(System.&lt;span style=&quot;color: #2b91af&quot;&gt;Type&lt;/span&gt; type)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DtoType = type;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; InitializeMapper(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PopulatePropertyOrdinalMappings(reader);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _isInitialized = &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; PopulatePropertyOrdinalMappings(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Get the PropertyInfo objects for our DTO type and map them to &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// the ordinals for the fields with the same names in our reader.&amp;#160; &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PropertyOrdinalMappings = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt;&amp;gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;PropertyInfo&lt;/span&gt;[] properties = DtoType.GetProperties();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;foreach&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyInfo&lt;/span&gt; property &lt;span style=&quot;color: blue&quot;&gt;in&lt;/span&gt; properties)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt; map = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; map.Property = property;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; map.Ordinal = reader.GetOrdinal(property.Name);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PropertyOrdinalMappings.Add(map);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; { }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Object&lt;/span&gt; GetData(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!_isInitialized) { InitializeMapper(reader); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;object&lt;/span&gt; dto = &lt;span style=&quot;color: #2b91af&quot;&gt;Activator&lt;/span&gt;.CreateInstance(DtoType);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;foreach&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt; map &lt;span style=&quot;color: blue&quot;&gt;in&lt;/span&gt; PropertyOrdinalMappings)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(map.Ordinal)) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; map.Property.SetValue(dto,reader.GetValue(map.Ordinal),&lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;); &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dto;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; GetRecordCount(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;Object&lt;/span&gt; count = reader[&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;RecordCount&amp;quot;&lt;/span&gt;];&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; count == &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt; ? 0 : &lt;span style=&quot;color: #2b91af&quot;&gt;Convert&lt;/span&gt;.ToInt32(count);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt;&amp;#160;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyInfo&lt;/span&gt; Property { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; Ordinal { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;This isn’t very much code, but the logic can be a little difficult to follow.&amp;#160; Let’s break it down and see how it works.&amp;#160; &lt;/p&gt;

&lt;p&gt;When the user creates an instance of GenericMapper, the constructor requires them to pass in the model type they want to map to.&amp;#160; That type is assigned to our local DtoType property (ModelType might have been a better name).&amp;#160; Note that we’re creating an instance, not using static methods.&amp;#160; This is important because once the GenericMapper is created it can be used multiple times for multiple records without having to recalculate the mappings.&lt;/p&gt;

&lt;p&gt;The user calls the GetData method which takes an IDataReader as a parameter. GetData is the heart of our mapper.&lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;Object&lt;/span&gt; GetData(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!_isInitialized) { InitializeMapper(reader); }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;object&lt;/span&gt; dto = &lt;span style=&quot;color: #2b91af&quot;&gt;Activator&lt;/span&gt;.CreateInstance(DtoType);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;foreach&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt; map &lt;span style=&quot;color: blue&quot;&gt;in&lt;/span&gt; PropertyOrdinalMappings)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(map.Ordinal)) &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; map.Property.SetValue(dto,reader.GetValue(map.Ordinal),&lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;); &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dto;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;GetData first checks to see if the GenericMapper has been initialized.&amp;#160; If not it calls InitializeMapper() which handles all initialization including a call to PopulatePropertyOrdinalMappings().&amp;#160; This is where the magic happens.&amp;#160; &lt;/p&gt;

&lt;pre style=&quot;font-family: consolas&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; PopulatePropertyOrdinalMappings(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Get the PropertyInfo objects for our DTO type and map them to &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// the ordinals for the fields with the same names in our reader.&amp;#160; &lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PropertyOrdinalMappings = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt;&amp;gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;PropertyInfo&lt;/span&gt;[] properties = DtoType.GetProperties();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;foreach&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyInfo&lt;/span&gt; property &lt;span style=&quot;color: blue&quot;&gt;in&lt;/span&gt; properties)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt; map = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&amp;#160;&lt;span style=&quot;color: #2b91af&quot;&gt;PropertyOrdinalMap&lt;/span&gt;();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; map.Property = property;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; map.Ordinal = reader.GetOrdinal(property.Name);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PropertyOrdinalMappings.Add(map);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; { }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;p&gt;We have a private class named PropertyOrdinalMap which is just a simple container for a System.Reflection.PropertyInfo object and an int. The int represents the ordinal (field index) in our IDataReader for the field that matches the name of the property stored in PropertyInfo.&amp;#160; The PopulatePropertyOrdinalMappings method just loops over all of the properties in our DtoType and tries to find an ordinal in IDataReader for a field with the same name.&amp;#160; If a match exists, a new PropertyOrdinalMap is created an added to the PropertyOrdinalMappings List. If a match doesn’t exist, no PropertyOrdinalMap is created.&amp;#160; The result is that we just skip any properties in our DtoType that the reader doesn’t have a value for.&amp;#160; That’s a key design decision.&amp;#160; We don’t throw an error if a mapping doesn’t exist, we just skip that property and get the data that we can map.&lt;/p&gt;

&lt;p&gt;Once the mappings are created it’s all pretty straight forward.&amp;#160; We return to GetData().&amp;#160; We use System.Activator() to create a new instance of our DtoType called dto (short for Data Transfer Object).&amp;#160; Then we iterate over our PropertyOrdinalMappings and use reflection to set the value of each dto property that we have a mapping for.&amp;#160; At that point we have a brand new object of whatever type was passed in to the constructor and it’s been populated with the data from the current record in the IDataReader.&amp;#160; We’re done!&amp;#160; We can just return the dto.&lt;/p&gt;

&lt;p&gt;So that’s it.&amp;#160; A little object oriented design, some super-simple reflection code, and I have a GenericMapper that allows me to never right another line of data mapping code (as long as I’m willing to use the same name for my model properties and data table columns).&amp;#160; I haven’t spent any time optimizing this.&amp;#160; In fact, I haven’t really looked at it since I first wrote it.&amp;#160; If anyone has suggestions for improvements please let me know. &lt;/p&gt;  </description><link>http://www.referencebits.com/2011/01/aapl-part-7-building-generic-data.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/_WvAhLAGzlZ4/S-G5L4j63wI/AAAAAAAAACw/sVoPsRExevc/s72-c/Angry%2520Computer%2520Guy22.jpg" height="72" width="72"/><thr:total>5</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-4348581941608054509</guid><pubDate>Mon, 03 Jan 2011 17:16:00 +0000</pubDate><atom:updated>2011-01-03T10:22:50.202-07:00</atom:updated><title>Type Argument Specification is Redundant</title><description>&lt;p&gt;One of the things I like about ReSharper is that it teaches me things I didn’t know.&amp;#160; Recently I was writing a property that calls a generic helper method that I’d created and ReSharper gave me the little popup below letting me know that I might want to refactor that line of code.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_6M6EoiuEFktxQGK3suWbXeNUmfzgotKJ6QLNEQQOH4j2LoCTAfPj_-BPYRcDjUAV3FB7dTFnJ0oHE5q_jQBNdo9YDVJjqwq1n06433ToGXaNKw97FYHZxGFMYj7-HWGjGmr6HTg1Jgo/s1600-h/image%5B4%5D.png&quot;&gt;&lt;img style=&quot;border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvKbCG1SqqWEpr72UHhF1215G2dWkY4uqm9UnyLdIRhQRpB1M_pEPMQmpfP4jzmGLBZMyTOHRf1tfuBR_qszDlfSP27vK5DF1wbXyz66_LKdlGmJfWXebkNNPd37WyVVVBWTqwee7U5C0/?imgmax=800&quot; width=&quot;508&quot; height=&quot;118&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;“Type argument specification is redundant”,&amp;#160; what the heck does that mean?&amp;#160; So I looked into it and learned something interesting that I didn’t know before.&amp;#160; To understand what’s going on let’s first look at my NewIfNull() method.&amp;#160; &lt;/p&gt;  &lt;p&gt;NewIfNull() is a simple generic helper method that takes an object of any type that has a parameterless constructor, it then checks to see if that object is null.&amp;#160; If it is null, the helper returns a new instance of that object’s type.&amp;#160; If the object isn’t null, it just returns the object.&amp;#160; It’s kind of like lazy loading for type instantiation.&amp;#160; It’s useful in situations where I have properties for things like data services that I don’t want to instantiate until the first time I call them (if they’re called at all).&amp;#160; Here’s the NewIfNull() code:&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXuyNCqpXTb5xTlfolthDTOiOzk8hdKKjAX2aqN5ZN2P_lPhZSJhyphenhyphen2ZY2RF5fU-149Nh93vKFlUtZ3UcbCECyHi8LyuwEYtHN4C-4vZVDMw6RgYAlFYt55nshNOLy8PENWeyzK8KYmZzw/s1600-h/image%5B9%5D.png&quot;&gt;&lt;img style=&quot;border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6C0IQX59DqcGjRF1_b9PGJiTZ1PAlmXLE8tUXqiyFxZvzUwPlUy4E5PkxvkYjuwFBxKJXzVUbnCHGKnLkONfXuB31uxElX4urSpq4nLz2ZS13UP09Qf-BBWY-_E1FrAaOeMFh-s0Ck5s/?imgmax=800&quot; width=&quot;435&quot; height=&quot;107&quot; /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You can see that NewIfNull() is a generic method that takes a type parameter T which represents the type of the object that I’m passing in.&amp;#160; A typical call to NewIfNull looks like&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEZRJ5kaQMn_sMdTe-Vjfri9v6FxyGznMWAMwm2RuOPWjmdhhetvwqDwYagcNLDBWpVsbc48BWQsOeevbdyhjoqcS8_OHJZMuCloga4r0kVCW6wGaIr1vT4pujDW1E5MkrnRv9aQCxb-I/s1600-h/image%5B14%5D.png&quot;&gt;&lt;img style=&quot;border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPuUDn2B87xM7wOzWaVlAlg_-DYw-eNATc2jHEpXxnuARMRpidb8Z2VLu6sHmDmhbwli5ghnT4ISSkA4UzYHLZX2WP4GOQAS5G2nA9FM8b3Npi_XjTIBku7a3BpBJiRiaSrQTM466e3XI/?imgmax=800&quot; width=&quot;324&quot; height=&quot;18&quot; /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This isn’t too bad, but it’s a little annoying that I have to pass _workflowService as a parameter and then I also have to specify WorkflowService as my type inside of the angle brackets.&amp;#160; Well apparently that part is completely unnecessary. That little helper message from ReSharper was trying to tell me that since T is just the type of my parameter obj,&amp;#160; .Net can infer the type of T from parameter obj.&amp;#160; Passing type T in the angle brackets is actually redundant. Brilliant!&amp;#160; Now my calls to NewIfNull() look like this.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYTKuICqxuCmGVCBeqXCndhAWnD9XOeI_4lyTGBiLkrHW6S6xGIJsCmpLWXSiXWQUkQPs-9ePBOXXOCnay3UC4NBACW9KUf7fRV3FuobjPfncUF-27sW79U3v3DKOASpaWIDHXHFyykjc/s1600-h/image%5B18%5D.png&quot;&gt;&lt;img style=&quot;border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQNJW9qw18ZdxWkTYvR9ojC91bJPtm3G4p2ecEJvdcp0ROV2N3AWyZI0r47b8H6Bh85GEHznuukD_EbYGZLYfUCViLM-jNzWf_g4B07e2gYX5J6i_X4tRvVdlnh-oThgKFegfkxCWR8kM/?imgmax=800&quot; width=&quot;208&quot; height=&quot;18&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Much cleaner. No angle brackets or generic type parameters needed.&amp;#160; I’ve been writing and using generic methods for quite a while and I never knew .Net would infer types like this.&amp;#160; Thank you ReSharper.&amp;#160; If you want to learn more, the MSDN documentation on Type Inference is pretty good.&amp;#160; Check out &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic10&quot;&gt;Section 26.3.3 Type Inference&lt;/a&gt;. &lt;/p&gt;  </description><link>http://www.referencebits.com/2011/01/type-argument-specification-is.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvKbCG1SqqWEpr72UHhF1215G2dWkY4uqm9UnyLdIRhQRpB1M_pEPMQmpfP4jzmGLBZMyTOHRf1tfuBR_qszDlfSP27vK5DF1wbXyz66_LKdlGmJfWXebkNNPd37WyVVVBWTqwee7U5C0/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-5185158208129672791</guid><pubDate>Mon, 02 Aug 2010 17:00:00 +0000</pubDate><atom:updated>2010-08-02T22:39:22.130-06:00</atom:updated><title>Muffin Top architecture is killing us</title><description>&lt;p&gt;I want to share a story. I recently encountered a company that had somehow navigated the many hazards that face technology startups and managed to create a solid, successful business.&amp;#160; They had gone public and now after years of struggling with a collection of applications that consisted largely of simple markup and script pages that directly accessed database queries, they were in the process of transitioning everything over to a real architecture, a single unified “Framework”.&amp;#160; &lt;/p&gt;  &lt;p&gt;The “Framework” was a thing of architectural beauty, object oriented design as far as the eye could see.&amp;#160; It had been designed by some incredibly bright developers.&amp;#160; So bright in fact that they had progressed beyond the point where they were even involved in writing code for a shipping application.&amp;#160; Some were so smart, that they skipped the mundane part of a programmer’s career where they spend years writing and maintaining application code, and instead they moved directly into the architecture world.&lt;/p&gt;  &lt;p&gt;The core of the “Framework” was the data access module which was based on LINQ to SQL, but hid all of the entities as well as the data context behind interfaces. These interfaces would of course need to be updated whenever changes were made to the LINQ data model, but they made the “Framework” feel a lot more object oriented and provided the additional benefit of hiding any functionality that the architecture group didn’t think regular application developers should have access to.&amp;#160; &lt;/p&gt;  &lt;p&gt;The LINQ data model also benefited from some extra architectural love.&amp;#160; Mapping directly to tables was out of the question, so every LINQ entity mapped to a collection of sprocs.&amp;#160; After all, the idea was to create an entity model, not just map directly to table data.&amp;#160; No, every entity was carefully planned and modeled, every field the application developers might need was anticipated and aggregated using those sprocs.&amp;#160; Now this did create yet another point (like the interfaces) at which data mapping logic could go wrong and throw errors, but that wasn’t a problem because the architecture team was able to use Visual Studio to generate a massive collection of database unit tests that would exhaustively test every possible crud scenario for every sproc and assert that it worked as expected.&amp;#160; Of course the massive collection of db unit tests and the sprocs themselves would need to be rewritten every time even the smallest change was made to the data model, but architecturally speaking it was just the right thing to do.&lt;/p&gt;  &lt;p&gt;Finally the day came when the “Framework” was pushed out to the unwashed masses of developers who would benefit from it’s glorious design.&amp;#160; But an unexpected thing happened.&amp;#160; New application development done on top of the “Framework” slowed to a crawl.&amp;#160; It was taking 3 to 5 times longer for application developers to build code with the “Framework”.&amp;#160; To make matters worse, the code that was produced, ran slower, sometimes much, much slower than the old legacy code that accessed the database directly. Also, the application developers kept coming back to the architecture team requesting that additional fields be added to the entities.&amp;#160;&amp;#160; They said these fields were required in order to provide features that were already available in the legacy applications.&amp;#160; It was as if these guys had never been part of the endless modeling meetings where everyone worked out in advance every field each entity would ever need.&amp;#160; The problems just kept piling up, and application ship dates slipped further and further.&amp;#160; Eventually the architects had to face up to an unpleasant conclusion… the company’s application developers just weren’t very good. I’m not kidding, that really was their conclusion. I actually heard that straight from the mouth of one of the architects.&amp;#160; (BTW, in case you’re wondering the application developers were actually some of the most skilled that I’ve ever run into.&amp;#160; They were very good developers.)&lt;/p&gt;  &lt;p&gt;The point of this story is that the problems we face as programmers in the .Net world are rapidly changing.&amp;#160; It used to be that the most common problem I ran into was horribly constructed spaghetti code that was then copied and pasted into 20 different pages of the application.&amp;#160; No code reuse, poor logic structure, and of course direct access to the database in the code behind or even in the markup pages.&amp;#160; The problems were largely the result of us as .Net developers just not being skilled enough to properly design our code.&amp;#160; But .Net is a developer-friendly technology and most of us were able to hack together code that worked even if it wasn’t pretty.&amp;#160; Note that I did say “us”.&amp;#160; Some of my early code is horrific.&lt;/p&gt;  &lt;p&gt;Now, increasingly I see a much different problem.&amp;#160; As a community we’ve learned a lot more about real software development. We understand all of those architectural things that our Java buddies used to talk about.&amp;#160; We’re using design patterns, object oriented design, automated testing, ORMs, true layered architectures, dependency injection, component architectures, class inheritance all over the place, interfaces, contract based programming, service oriented architecture and a bunch of stuff that sounds like it should enable us to ship more reliable code, and to ship it faster.&amp;#160; The problem is that in my experience, most of the time it doesn’t.&amp;#160; Don’t get me wrong, I&#39;m not saying that the practices listed above are bad, or that we should go back to writing simple markup pages with SQL queries written directly in our scripts.&amp;#160; What I am saying is that in our eagerness to flex our new-found coding muscles, we’re just throwing these things into our applications because we think they’re a “best practice” and the result is a lot of over-architected, impossible to maintain, fat, sprawling code.&amp;#160; I just get a sense that many very intelligent .Net developers are throwing this stuff into their applications because this is what a real architecture looks like, not because they are getting some measurable benefit.&amp;#160; At the end of the day it’s all about shipping code, not creating an architecture that makes us feel like real architects.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;img src=&quot;http://www.dailydadvice.com/uploaded_images/muffin_top-712047.jpg&quot; /&gt;&lt;/p&gt;  &lt;p&gt;A smart guy that I met at the unspecified company mentioned earlier put a name to it.&amp;#160; He called it Muffin Top Architecture.&amp;#160; I didn’t know what “muffin top” meant until he explained it to me. If you’re confused do a search on Google images for muffin top and you’ll figure it out.&amp;#160; Hint, it doesn’t refer to the images of real muffins.&amp;#160; Anyway, I’m starting to see quite a bit of Muffin Top code.&amp;#160; The company I told the story about is by no means atypical.&amp;#160; I could go on for pages and pages.&amp;#160; I’ve even encountered companies who are unable to make changes to their own code, that they wrote. There are just so many layers of architecture and class inheritance that changes are incredibly painful even for the original developers.&amp;#160; &lt;/p&gt;  &lt;p&gt;I don’t have an easy answer, but I do think we need to start producing code that is simpler, easier to maintain, and quicker to produce.&amp;#160; .Net is a great stack and it isn’t difficult to work with, but we’re developing this reputation of “.Net is a solid platform HOWEVER it takes so long to build on that it’s not appropriate for many projects, and definitely not for a startup project”.&amp;#160; I don’t think that’s true.&amp;#160; The problem isn’t .Net, the problem is that we’re over-architecting, and in many cases Microsoft is more than happy to help us further down the path to destruction with new persistence frameworks, designers, and component architectures.&amp;#160;&amp;#160; I think we should be looking more and more at technologies like Ruby on Rails and trying to figure out how they achieve the remarkable efficiencies that are possible on that platform.&amp;#160; It’s certainly not through a designer.&amp;#160; I also think that the answer lies in less code that is simpler and easier to understand, not piles of generated code and component architectures that make it impossible to figure out what code is executing when.&amp;#160; &lt;/p&gt;  &lt;p&gt;Another big part of the problem is the emergence of the non-coding architect.&amp;#160; This is the person who doesn’t write any shipping code themselves, they just tell everyone else how they should be writing code.&amp;#160; I think the role of architect is tremendously important.&amp;#160; I see the architect as the lead developer, the mentor, the overall system designer.&amp;#160; But let me make this clear, an architect who doesn’t write code isn’t a real programmer, they are an obstacle to real programmers.&amp;#160; It doesn’t matter how skilled that person is, if the architect isn’t writing shipping code then their incentives are not aligned with the other developers.&amp;#160; The real programmer’s number one priority in life is to ship working code.&amp;#160; The non-coding architect’s priority is to create beautiful architecture.&amp;#160; &lt;/p&gt;  &lt;p&gt;If you have any thoughts please don’t hesitate to post them.&amp;#160; Personally, I think that Muffin Top Architecture is killing us.&amp;#160; Is there a solution or am I just destined to go be a Rails developer like Mike Moore, Jeff Cohen, and Scott Bellware?&amp;#160; Check out &lt;a href=&quot;http://herdingcode.com/?p=256&quot;&gt;Herding Code 84&lt;/a&gt; for some interesting discussion by the .Net expatriates. &lt;/p&gt;  </description><link>http://www.referencebits.com/2010/08/muffin-top-architecture-is-killing-us.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>11</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-7683011498336324407</guid><pubDate>Tue, 27 Apr 2010 08:50:00 +0000</pubDate><atom:updated>2010-04-27T06:32:38.314-06:00</atom:updated><title>AAPL Part 6: Building a DataMapper and DataMapperFactory</title><description>&lt;p&gt;I recently decided that I needed to take a fresh look at how to build a persistence architecture that would provide the flexibility of an ORM, that would embrace change instead of resist it (making maintenance code easier), and would use ADO.Net.&amp;#160; I started building from the ground up, threw away any best practices that resulted in friction, and Agile ADO.Net Persistence Layer is the result.&amp;#160; Part 1 in the series can be found here: &lt;a href=&quot;http://rlacovara.blogspot.com/2010/01/agile-adonet-persistence-layer-part-1.html&quot;&gt;Agile ADO.Net Persistence Layer Overview&lt;/a&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;In this post, we’ll get into the DataMapper.&amp;#160; If you’ve been waiting to see some real ADO.Net code, this is the class you’ve been waiting for.&lt;/p&gt;  &lt;p&gt;Where the DataMapper classes fit in&lt;/p&gt;  &lt;p&gt;Let’s review our solution structure.&amp;#160; Core is our main business layer project (Note: in previous posts and in the sample code Core is actually the BAL project.&amp;#160; I never liked the name BAL so I’ve renamed it Core.&amp;#160; Same code, just a different name.&amp;#160; Sorry for any confusion.).&amp;#160; Core contains Service classes which are our one stop shop for all persistence (see &lt;a href=&quot;http://rlacovara.blogspot.com/2010/01/agile-adonet-persistence-layer-part-3.html&quot;&gt;AAPL Part 3&lt;/a&gt; and &lt;a href=&quot;http://rlacovara.blogspot.com/2010/01/agile-adonet-persistence-layer-part-4.html&quot;&gt;AAPL Part 4&lt;/a&gt; for more on how the service classes work). The Service classes use the SqlDao for all communication with the database.&amp;#160; The SqlDao uses the DataMapper classes to handle all mapping between the DataReader returned by executing a query, and the desired data shape.&amp;#160; Here’s what our project looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwZpxvW7SKNwrBoKLoJz5ZQB2QYF8vJyhMBh6ZzwQztPyOLInIzJODFTosbRebhFkYALBFPLrdmQbS54CisBEupY6VDRgFKixtQAnlIx34QIJE54y8-oq6x64-DP-N-i5DTHFe099KEIo/s1600-h/image11.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEahiNITofayeAfE94QZbuBNN1iVLwzUQxtE7Itl1_VXF8fXKQJ6Gms-ToKuES7shJO_DlK_rUMS9TmwQZc9owHcuA_ODNNXKZPOLL9hkCnJJPU6NWd-ZYwJEzFbDW23vWdydta9AynJw/?imgmax=800&quot; width=&quot;265&quot; height=&quot;287&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Last post we covered the SqlDao class and saw how it’s data access methods take a query wrapped in an SqlCommand, execute that query, then return either a Single&amp;lt;DTO&amp;gt; (where DTO is our desired data shape), List&amp;lt;DTO&amp;gt;, DataPage&amp;lt;DTO&amp;gt;,or a scalar value.&amp;#160; The data access methods in SqlDao handle all the boilerplate logic for getting a connection, executing a query, stuff like that, but the logic that is specific to a data shape, things like get the value from this column in my reader put it in this property on my returned data shape, that stuff get’s delegated to a DataMapper class. This is what that SqlDao code looks like:&lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; reader.Read();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt; mapper = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;DataMapperFactory&lt;/span&gt;().GetMapper(&lt;span style=&quot;color: blue&quot;&gt;typeof&lt;/span&gt;(T));&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; dto = (T)mapper.GetData(reader);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;h4&gt;What does the DataMapperFactory do?&lt;/h4&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;You’ll notice in the code above that we get our IDataMapper object from a DataMapperFactory.&amp;#160; If we need to get the DataMapper for type User, we just call the DataMapperFactory.GetMapper method, pass type(User) as a parameter, and GetMapper returns the appropriate concrete DataMapper (Note that the code example above uses generics, so we use generic T instead of a specific type like User, but the logic is the same). DataMapperFactory is a simple factory class with one method, GetMapper. The code looks like this:&amp;#160; &lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;DataMapperFactory&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt; GetMapper(&lt;span style=&quot;color: #2b91af&quot;&gt;Type&lt;/span&gt; dtoType)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;switch&lt;/span&gt;(dtoType.Name)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;ListItemDTO&amp;quot;&lt;/span&gt;:&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;ListItemDTOMapper&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;User&amp;quot;&lt;/span&gt;:&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;UserMapper&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;default&lt;/span&gt;:&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;GenericMapper&lt;/span&gt;(dtoType);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Whenever we create a new IDataMapper for a specific type, like User, we need to add it to this factory method.&amp;#160; The method is just a switch statement that uses the type.Name to look for a type-specific implementation of IDataMapper.&amp;#160; If it finds one, it returns it.&amp;#160; If it doesn’t find a type specific IDataMapper, it returns an instance of GenericMapper, which is our reflection based DataMapper that can be used to handle mappings for any query / data shape combination.&amp;#160; We’ll cover GenericMapper in the next post. &lt;/p&gt;  &lt;h4&gt;What does the DataMapper do?&lt;/h4&gt;  &lt;p&gt;The behavior of a DataMapper class is defined by the IDataMapper interface.&amp;#160; The interface requires DataMappers to implement two methods. &lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;interface&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Main method that populates dto with data&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;Object&lt;/span&gt; GetData(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Gets the num results returned. Needed for data paging.&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; GetRecordCount(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The GetData method is the important one.&amp;#160; That’s the method that our SqlDao calls whenever it needs to map the current record in an SqlDataReader to a specific data shape.&amp;#160; Remember, this method doesn’t do any mapping of an SqlDataReader to a List, or DataPage, or anything like that. That’s handled by the SqlDao data access methods. The purpose of the DataMapper GetData function is to take an SqlDataReader that’s already open and on a record, read that record, put the column data into an object (DTO) of the desired data shape, then return that single DTO. &lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;font color=&quot;#808080&quot;&gt;&lt;strong&gt;Note&lt;/strong&gt; that GetData returns an Object, so the SqlDao has to cast the GetData result to the proper data shape.&amp;#160; I tried all kinds of designs to get around this cast, I used interfaces, generics, base classes, but in the end all of these options added complexity to the code that I just couldn’t accept.&amp;#160; So, I opted to stick with GetData returning an object.&amp;#160; I think it was the right tradeoff, but if anyone has a better solution please let me know.&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Here is a diagram of our IDataMapper interface and a couple of concrete DataMapper implementations.&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkOenG3pUFq2gIGkO5aSKM2LJL6dBxLikXBB-PIf_pl24vMulT4td1C7VCIOP2meVH-EccxtX1l3CV6pAnYZQZRgwo9o7l_U4p9PZ8SRi4DASrTvGIcsGSnyVAkVTxLf1NcbZ9J-tggWc/s1600-h/image16.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyut_edNq5nhPa6F_yCayRgPelwKRLwVp6hpzRbSeSlUowkJW9e3eKnwjFoz2vVHZ-0vw0f7c0ZUFVAgigHY6PN8nAUG06Q4semchJUumDxRlfbGSJ9DpFksPyPfZxzhzSB3seiXlaSmg/?imgmax=800&quot; width=&quot;667&quot; height=&quot;450&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;Inside a DataMapper class&lt;/h4&gt;  &lt;p&gt;So let’s take a look inside the UserMapper class and see what an IDataMapper implementation looks like. The user class is a data shape that represents, wait for it… a user.&amp;#160; The class looks like this.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsnlbghgE8IKwkmtunsHAvrr0Fbf7DGj31K6Y5Yvq-YBxn_-eOpRGT40Q-1-m1gslTE2OtMFJO6Z0G91DcTn9YiSCmhjVz1DPV-Z_J6iJvqEROCCCM3LTUrBuzJbOXfwzF4Obwv1Q8bew/s1600-h/image25.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiYdG7IEeNE2D4HZB91WSiPho530kqMtLAiKtnPZCZMuSPCd_AA83yDAAKjPzW4uSLGWyOBItUpPA8vE312a3XFGLDfcU8vcPoghKvOVITc9Yqm3sqzNgdfXRH3LrWMiBrvJ3OpaFWH8o/?imgmax=800&quot; width=&quot;187&quot; height=&quot;390&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So the job of UserDataMapper is to take a SqlDataReader, get the values from it’s columns, and put those values into and instance of the&amp;#160; User class above.&amp;#160; The DataMapper implementation that I use accomplishes this task in a very structured way that is easy to copy and reproduce for new data shapes.&amp;#160; &lt;/p&gt;  &lt;p&gt;Before we look at the code, I should mention that I write a fair amount of extra code to get the ordinals for each data field instead of just using the string field names.&amp;#160; The reason for this is that using the ordinals allows me to use strongly typed methods on the SqlDataReader to get each column.&amp;#160; This allows me to avoid the cast to and from Object that occurs when you get a column by name which results in boxing and unboxing for value types.&amp;#160; It also gives me a little type safety.&amp;#160; If the query is returning a column called UserGuid that has a type of Int32, when I call reader.GetGuid() on that column, my code will throw an error and alert me that there is a problem with my query.&amp;#160; For more on the benefits of using ordinals take a look at &lt;a href=&quot;http://rlacovara.blogspot.com/2009/03/high-performance-data-access-layer-part.html&quot;&gt;High Performance Data Access Layer Architecture Part 3&lt;/a&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;Now, with no further ado, here’s the UserMapper implementation.&lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;UserMapper&lt;/span&gt; : &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;bool&lt;/span&gt; _isInitialized = &lt;span style=&quot;color: blue&quot;&gt;false&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_UserGuid;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_UserRole;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_CreatedUtc;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_ModifiedUtc;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_CompanyGuid;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_FirstName;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_LastName;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_Email;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_Password;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_PhoneWork;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; _ordinal_PhoneMobile;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; InitializeMapper(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PopulateOrdinals(reader);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _isInitialized = &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; PopulateOrdinals(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_UserGuid = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;UserGuid&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_UserRole = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;UserRole&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_CreatedUtc = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;CreatedUtc&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_ModifiedUtc = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;ModifiedUtc&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_CompanyGuid = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;CompanyGuid&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_FirstName = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_LastName = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;LastName&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_Email = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Email&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_Password = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Password&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_PhoneWork = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;PhoneWork&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _ordinal_PhoneMobile = reader.GetOrdinal(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;PhoneMobile&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Object&lt;/span&gt; GetData(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// This is where we define the mapping between the object properties and the &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// data columns. The convention that should be used is that the object property &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// names are exactly the same as the column names. However if there is some &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// compelling reason for the names to be different the mapping can be defined here.&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// We assume the reader has data and is already on the row that contains the data &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//we need. We don&#39;t need to call read. As a general rule, assume that every field must &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//be null&amp;#160; checked. If a field is null then the nullvalue for that&amp;#160; field has already &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;//been set by the DTO constructor, we don&#39;t have to change it.&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!_isInitialized) { InitializeMapper(reader); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;User&lt;/span&gt; dto = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;User&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Now we can load the data&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_UserGuid)) { dto.UserGuid = reader.GetGuid(_ordinal_UserGuid); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_UserRole)) { dto.UserRole = (&lt;span style=&quot;color: #2b91af&quot;&gt;UserRole&lt;/span&gt;)reader.GetInt32(_ordinal_UserRole); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_CreatedUtc)) { dto.CreatedUtc = reader.GetDateTime(_ordinal_CreatedUtc); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_ModifiedUtc)) { dto.ModifiedUtc = reader.GetDateTime(_ordinal_ModifiedUtc); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_CompanyGuid)) { dto.CompanyGuid = reader.GetGuid(_ordinal_CompanyGuid); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_FirstName)) { dto.FirstName = reader.GetString(_ordinal_FirstName); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_LastName)) { dto.LastName = reader.GetString(_ordinal_LastName); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_Email)) { dto.Email = reader.GetString(_ordinal_Email); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_Password)) { dto.Password = reader.GetString(_ordinal_Password); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_PhoneWork)) { dto.PhoneWork = reader.GetString(_ordinal_PhoneWork); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_PhoneMobile)) { dto.PhoneMobile = reader.GetString(_ordinal_PhoneMobile); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dto;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; GetRecordCount(&lt;span style=&quot;color: #2b91af&quot;&gt;IDataReader&lt;/span&gt; reader)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;Object&lt;/span&gt; count = reader[&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;RecordCount&amp;quot;&lt;/span&gt;];&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; count == &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt; ? 0 : &lt;span style=&quot;color: #2b91af&quot;&gt;Convert&lt;/span&gt;.ToInt32(count);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;You’ll notice that the first thing we do is declare an _ordinal_FieldName member for each property in our User class.&amp;#160; These local members are used to store the ordinal location for the column in the SqlDataReader that maps to that User field.&amp;#160; We also create an _isInitialized flag that let’s us know whether we’ve set our _ordinal_FieldName values yet.&amp;#160; This is important when the UserMapper is being used to get more than one User from a DataReader.&amp;#160; The SqlDao code will get a single IDataMapper instance and will then call that mapper over and over for each record that exists in the SqlDataReader. We only want to find the ordinals once, so we create our _isInitialized flag that can be set to true after we set all the ordinal values.&lt;/p&gt;  &lt;p&gt;Next is the InitializeMapper method.&amp;#160; This just calls our PopulateOrdinals method and then sets that _isInitialized flag, that we just discussed, to true.&lt;/p&gt;  &lt;p&gt;PopulateOrdinals is the method that takes our SqlDataReader, finds the ordinal location of each field we need, then stores those ordinal values in our local _ordinal_FieldName members. &lt;/p&gt;  &lt;p&gt;Finally we arrive at GetData, the heart of our class.&amp;#160; We’ve structured our code in such a way that GetData is actually pretty simple.&amp;#160; We check to make sure the _isInitialized flag has been set. If it hasn’t we call PopulateOrdinals.&amp;#160; We then create a new instance of our User class called dto. After that it’s just a matter of repeating the same line of code for each field.&lt;/p&gt;  &lt;p&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(_ordinal_UserGuid)) { dto.UserGuid = reader.GetGuid(_ordinal_UserGuid); }&amp;#160;&amp;#160; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This one line of code performs a null check on the ordinal, if the column exists it uses a strongly typed Get method to get the value of that column and assign it to the dto, if the column is null then we do nothing because the constructor for User already set each property to it’s null value on creation.&amp;#160; And that’s it, after setting each field value (or not setting it) we just return our dto and we’re done.&amp;#160; Simple maintainable, easy to copy and reproduce code.&lt;/p&gt;  &lt;p&gt;The last method is GetRecordCount.&amp;#160; We need this method to support the DataPage&amp;lt;DTO&amp;gt; shape.&amp;#160; Our SqlDao needs to know the total record count if it’s returning a data page.&amp;#160; If it’s not returning a data page this method isn’t used. &lt;/p&gt;  &lt;h4&gt;Conclusion&lt;/h4&gt;  &lt;p&gt;That’s it.&amp;#160; Now that I look at the DataMapper code its a little anti-climactic because it’s really very simple stuff.&amp;#160; But then that’s the idea, break a complex operation into smaller pieces so that each one is easy to understand, and if needed, reproduce.&amp;#160; To add a new DataMapper we just copy and paste the existing UserMapper and change the field names.&amp;#160; Easy.&amp;#160; We don’t need to touch any of the code at the SqlDao level that handles getting Single&amp;lt;DTO&amp;gt;, List&amp;lt;DTO&amp;gt;, or DataPage&amp;lt;DTO&amp;gt; return types.&amp;#160; We also don’t touch any code that affects any other data shape.&amp;#160; All of our code is encapsulated in this one easy to handle class.&amp;#160; &lt;/p&gt;  &lt;p&gt;Also, keep in mind that the real power of this architecture is that we don’t need to create a type specific DataMapper at all.&amp;#160; For the vast majority of tasks, the GenericMapper is going to work just fine.&amp;#160; In practice I never create any type-specific DataMappers during development because it seems like at that early stage data shapes are always in flux, and when a data shape changes, the last thing I want to do is go back and update a bunch of mapping code.&amp;#160; I look at the type specific DataMappers as something that I never want to use unless an app has gone into production and it’s experiencing performance issues related to the GenericMapper.&amp;#160; If that happens then I have a hook in the framework that I can use to create a simple DataMapper for that data shape that contains what I think is the fastest ADO.Net code possible.&amp;#160; &lt;/p&gt;  &lt;p&gt;Next time we’ll go over the GenericMapper.&lt;/p&gt;  </description><link>http://www.referencebits.com/2010/04/aapl-part-6-building-datamapper-and.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEahiNITofayeAfE94QZbuBNN1iVLwzUQxtE7Itl1_VXF8fXKQJ6Gms-ToKuES7shJO_DlK_rUMS9TmwQZc9owHcuA_ODNNXKZPOLL9hkCnJJPU6NWd-ZYwJEzFbDW23vWdydta9AynJw/s72-c?imgmax=800" height="72" width="72"/><thr:total>6</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-2269894453199267307</guid><pubDate>Mon, 19 Apr 2010 12:32:00 +0000</pubDate><atom:updated>2010-04-19T06:57:05.179-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">aapl</category><category domain="http://www.blogger.com/atom/ns#">ADO.Net</category><category domain="http://www.blogger.com/atom/ns#">DAO</category><category domain="http://www.blogger.com/atom/ns#">Data Access Object</category><category domain="http://www.blogger.com/atom/ns#">Data Access Patterns</category><category domain="http://www.blogger.com/atom/ns#">Data Transfer Objects</category><category domain="http://www.blogger.com/atom/ns#">DTO</category><title>AAPL Part 5: How to Write a Data Access Object (DAO)</title><description>&lt;p&gt;I recently decided that I needed to take a fresh look at how to build a persistence architecture that would provide the flexibility of an ORM, that would embrace change instead of resist it (making maintenance code easier), and would use ADO.Net.&amp;#160; I started building from the ground up, threw away any best practices that resulted in friction, and Agile ADO.Net Persistence Layer is the result.&amp;#160; Part 1 in the series can be found here: &lt;a href=&quot;http://rlacovara.blogspot.com/2010/01/agile-adonet-persistence-layer-part-1.html&quot;&gt;Agile ADO.Net Persistence Layer Overview&lt;/a&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;In this post, we’ll get into what is probably the most useful class in the framework, the Data Access Object (DAO).&lt;/p&gt;  &lt;h4&gt;Where does the DAO fit in?&lt;/h4&gt;  &lt;p&gt;Let’s backtrack a bit and take a quick look at the design of core.&amp;#160; Below we see a snapshot of Core’s folder structure.&amp;#160; We have a Persistence folder that contains our SqlDAO (a DAO written specifically for SqlServer), as well as our DataMappers and Persisters (we’ll go into these later).&amp;#160; The Rules folder contains business rules and validation rules for different entities / data shapes.&amp;#160; Security has a little plumbing code for business layer authorization.&amp;#160; Finally, the Services folder contains the service classes that we’ve focused on during the previous few posts.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpzOMmNvhmJGZ533LSdi2bHg0-BMWGM2t0xI6yTU0SbeYma6imZFYC7IrXbeciuuEq5HIbpeEbkYv269I-m4BT-MycNRCBaBpZknUc3OYtrEYElFRA_4KDcP3jLR_bt8QYZ8wL_JoLHEE/s1600-h/image3.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiYa_8J9wXvbFqSZrH4ejeoLJpOCIAjr9C9sKLWcclRFPvipVFnWY_B8l_APMaXhHrFUx9d-I7AlcFFuwdYK7w_zN8BtGc-JOo0rmLa0oc39oEPWDwaUu4rlUpGXiC9Z5ak0HFuOaRI38/?imgmax=800&quot; width=&quot;184&quot; height=&quot;184&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The service classes are the one stop shop that our UI code uses to get and save data.&amp;#160; A typical data access method looks like this.&lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;User&lt;/span&gt; GetUserByEmail(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; email)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; sql = &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;SELECT * &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM [User] &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WHERE [Email] = @Email&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(sql);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; command.Parameters.Add(dao.CreateParameter(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;@Email&amp;quot;&lt;/span&gt;, email, 100));&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return &lt;/span&gt;dao.GetSingle&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;User&lt;/span&gt;&amp;gt;(command);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;You’ll notice that this is a pretty simple method.&amp;#160; There’s no real Ado.Net code. We’re just defining a query, wrapping it in an SqlCommand, and then passing both the SqlCommand and our desired data shape &amp;lt;User&amp;gt; to our DAO.&amp;#160; This mystery DAO object does all the heavy lifting for us and even helps us create our query parameter in very efficient, single line of code.&amp;#160; This is a very intentional design.&amp;#160; After writing data access code for a few years, I finally noticed that the only things I really change from one data access method to the next are: the query; the parameters; and the return data shape. Everything else is just boilerplate plumbing code that gets repeated over and over again.&amp;#160; So, I took all that data access boilerplate code and extracted it out to a helper class called SqlDao.&amp;#160; The main functions of the DAO are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The DAO is the single access point to our database. All communication with the database goes through the DAO.&amp;#160; No code goes around it. &lt;/li&gt;    &lt;li&gt;The DAO encapsulates common ADO.Net tasks like getting a connection, creating query parameters, executing queries, etc. &lt;/li&gt;    &lt;li&gt;The DAO defines methods that take an SqlCommand as a parameter, execute it, and return the data in one of our standard formats, a single DTO, as List&amp;lt;DTO&amp;gt;, a DataPage&amp;lt;DTO&amp;gt;, or a string value.&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There’s a lot of good stuff in there that makes writing data access method almost frictionless.&amp;#160; I can’t go over everything so I included a complete listing for the SqlDao at the end of this post. &lt;/p&gt;  &lt;h4&gt;The GetSingle&amp;lt;T&amp;gt; and GetList&amp;lt;T&amp;gt; methods&lt;/h4&gt;  &lt;p&gt;Now let’s look at something a little more interesting. The SqlDao.GetSingle&amp;lt;T&amp;gt; method takes an SqlCommand as a parameter and returns the restults of that command as a single T where T is the class of our desired datashape.&amp;#160; So, the line dao.GetSingle&amp;lt;User&amp;gt;(command); will execute command and then return an object of type User.&amp;#160; If no User was found then the method just returns null.&amp;#160; Here’s how it works.&lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: green&quot;&gt;// GetSingle&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; T GetSingle&amp;lt;T&amp;gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command) &lt;span style=&quot;color: blue&quot;&gt;where&lt;/span&gt; T : &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; T dto = &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Read();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt; mapper = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;DataMapperFactory&lt;/span&gt;().GetMapper(&lt;span style=&quot;color: blue&quot;&gt;typeof&lt;/span&gt;(T));&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dto = (T)mapper.GetData(reader);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// return the DTO, it&#39;s either populated with data or null.&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dto;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The code is pretty simple.&amp;#160; We declare a new object of type T called dto (Data Transfer Object) and initialize it to null. We then check the connection on our command and make sure it’s open.&amp;#160; We call ExecuteReader on the command which gets us an SqlDataReader.&amp;#160; If the reader has any rows then we go to our DataMapperFactory and get an IDataMapper for the type T.&amp;#160; A cool thing that is happening behind the scenes is that the DataMapperFactory checks to see if we have an IDataMapper that has been created specifically for type T, if we don’t it returns a GenericMapper that uses reflection to map SqlDataReader fields to fields on our type T.&amp;#160; Once we have a mapper it’s just a matter of calling the mapper.GetData(reader) method and returning the dto.&amp;#160; &lt;/p&gt;  &lt;p&gt;Now let’s take a quick look at GetList&amp;lt;T&amp;gt;.&amp;#160; This method is very similar to GetSingle&amp;lt;T&amp;gt;, but it returns a generic list of objects of type T instead of a single object of type T.&amp;#160; If no results are returned when we execute the SqlCommand passed to GetList&amp;lt;T&amp;gt;, we don’t return null, instead we return an empty list.&amp;#160; Here’s the code.&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: green&quot;&gt;// GetList&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;T&amp;gt; GetList&amp;lt;T&amp;gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command) &lt;span style=&quot;color: blue&quot;&gt;where&lt;/span&gt; T : &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;T&amp;gt; dtoList = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;T&amp;gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt; mapper = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;DataMapperFactory&lt;/span&gt;().GetMapper(&lt;span style=&quot;color: blue&quot;&gt;typeof&lt;/span&gt;(T));&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;while&lt;/span&gt; (reader.Read())&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; T dto = &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dto = (T)mapper.GetData(reader);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dtoList.Add(dto);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// We return either the populated list if there was data,&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// or if there was no data we return an empty list.&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dtoList;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;It’s really the same as GetSingle&amp;lt;T&amp;gt;.&amp;#160; We just have a different return type List&amp;lt;T&amp;gt;, and a while loop that get’s our individual DTOs and adds them to our return dtoList. The main logic for getting an IDataMapper from the DataMapperFactory and calling mapper.GetData(reader) to parse the reader results into an object of type T remains the same.&lt;/p&gt;  &lt;h4&gt;Conclusion&lt;/h4&gt;  &lt;p&gt;At this point you may be thinking, “That’s it???, you didn’t answer anything!&amp;#160; All you did is get some mapper object and call the GetData method on it.”&amp;#160; That’s true to some extent.&amp;#160; Our GetSingle&amp;lt;T&amp;gt; and GetList&amp;lt;T&amp;gt; methods just contain the logic that is always going to be common to those tasks regardless of the data shape passed to them.&amp;#160; Anything that is data shape specific, things like how do I get data from this field in my SqlDataReader and put it in this field in my returned data shape, are delegated to the IDataMapper.&amp;#160; This separation of logic means that it’s very easy to maintain our code.&amp;#160; We can add DataMapper logic for new data shapes and never touch any of the logic for GetSingle, GetList, and GetPage.&amp;#160; In fact, our logic for a specific data shape is encapsulated in a single IDataMapper class that touches nothing else in the application. That means it’s easy to change and maintenance friendly.&amp;#160; Plus, don’t forget that we have the reflection-based GenericMapper, so we never have to create a new IDataMapper if we don’t want to.&lt;/p&gt;  &lt;p&gt;Next time we’ll take a look at a data shape specific IDataMapper and the DataMapperFactory. In the meantime, the full source for SqlDao is included below.&amp;#160; Have a look at the implementation for GetDataPage&amp;lt;T&amp;gt; and see if it makes sense now that we’ve gone over GetSingle&amp;lt;T&amp;gt; and GetList&amp;lt;T&amp;gt;.&amp;#160; The CreateParameter methods are worth a look too.&amp;#160;&amp;#160; I went with a design where I just overload the method CreateParameter for each type of parameter that I need to create, so the usage syntax for CreateParameter(Guid value) and CreateParameter(int value) are almost identical. Each CreateParameter method was designed with the target of enabling that single line syntax that I use: &lt;font color=&quot;#0000ff&quot;&gt;command.Parameters.Add(dao.CreateParameter(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;@Email&amp;quot;&lt;/span&gt;, email, 100));&lt;/font&gt; so each method contains automatic null value checking.&amp;#160; You’ll also see some methods that weren’t in my original spec.&amp;#160; While using this framework on different projects I’ve found it convenient to have methods like GetSingleInt32(command) and GetStringList(command).&amp;#160; &lt;/p&gt;  &lt;p&gt;Finally you can download a small, one page sample app that uses aapl at &lt;a href=&quot;http://aapl.codeplex.com/&quot;&gt;http://aapl.codeplex.com/&lt;/a&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;using&lt;/span&gt; System;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;using&lt;/span&gt; System.Linq;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;using&lt;/span&gt; System.Data.SqlClient;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;using&lt;/span&gt; System.Data;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;using&lt;/span&gt; System.Configuration;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;using&lt;/span&gt; hf.Common;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;using&lt;/span&gt; hf.Common.DataShapes;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;using&lt;/span&gt; hf.Core.Persistence.DataMappers;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;namespace&lt;/span&gt; hf.Core.Persistence&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #region&lt;/span&gt; &amp;quot;Database Helper Methods&amp;quot;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Connection&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlConnection&lt;/span&gt; _sharedConnection;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlConnection&lt;/span&gt; SharedConnection&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (_sharedConnection == &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _sharedConnection = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlConnection&lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;ConfigurationManager&lt;/span&gt;.ConnectionStrings[&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;hf&amp;quot;&lt;/span&gt;].ConnectionString);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; _sharedConnection;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _sharedConnection = &lt;span style=&quot;color: blue&quot;&gt;value&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// Constructors&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; SqlDao() { }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; SqlDao(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlConnection&lt;/span&gt; connection)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;this&lt;/span&gt;.SharedConnection = connection;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetDbSqlCommand&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; GetSqlCommand(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; sqlQuery)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection = SharedConnection;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.CommandType = &lt;span style=&quot;color: #2b91af&quot;&gt;CommandType&lt;/span&gt;.Text;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.CommandText = sqlQuery;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; command;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetDbSprocCommand&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; GetSprocCommand(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; sprocName)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt;(sprocName);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection = SharedConnection;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.CommandType = &lt;span style=&quot;color: #2b91af&quot;&gt;CommandType&lt;/span&gt;.StoredProcedure;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; command;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateNullParameter&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateNullParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt; paramType)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = paramType;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Value = &lt;span style=&quot;color: #2b91af&quot;&gt;DBNull&lt;/span&gt;.Value;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Input;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateNullParameter - with size for nvarchars&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateNullParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt; paramType, &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; size)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = paramType;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Size = size;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Value = &lt;span style=&quot;color: #2b91af&quot;&gt;DBNull&lt;/span&gt;.Value;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Input;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateOutputParameter&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateOutputParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt; paramType)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = paramType;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Output;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateOuputParameter - with size for nvarchars&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateOutputParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt; paramType, &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; size)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = paramType;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Size = size;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Output;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateParameter - uniqueidentifier&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; value)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (value.Equals(&lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullGuid))&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// If value is null then create a null parameter&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; CreateNullParameter(name, &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.UniqueIdentifier);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;else&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.UniqueIdentifier;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Value = value;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Input;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateParameter - int&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; value)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (value == &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullInt)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// If value is null then create a null parameter&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; CreateNullParameter(name, &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.Int);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;else&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.Int;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Value = value;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Input;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateParameter - bool&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: blue&quot;&gt;bool&lt;/span&gt; value)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.Bit;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Value = value ? 1 : 0;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Input;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateParameter - datetime&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: #2b91af&quot;&gt;DateTime&lt;/span&gt; value)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (value == &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullDateTime)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// If value is null then create a null parameter&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; CreateNullParameter(name, &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.DateTime);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;else&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.DateTime;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Value = value;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Input;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateParameter - nvarchar&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; value, &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; size)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;.IsNullOrEmpty(value))&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// If value is null then create a null parameter&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; CreateNullParameter(name, &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.NVarChar);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;else&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.NVarChar;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Size = size;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Value = value;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Input;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// CreateTextParameter&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; CreateTextParameter(&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; name, &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; value)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;.IsNullOrEmpty(value))&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// If value is null then create a null parameter&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; CreateNullParameter(name, &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.Text);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;else&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt; parameter = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlParameter&lt;/span&gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.SqlDbType = &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDbType&lt;/span&gt;.Text;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.ParameterName = name;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Value = value;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parameter.Direction = &lt;span style=&quot;color: #2b91af&quot;&gt;ParameterDirection&lt;/span&gt;.Input;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; parameter;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #endregion&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #region&lt;/span&gt; &amp;quot;Data Projection Methods&amp;quot;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// ExecuteNonQuery&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;void&lt;/span&gt; ExecuteNonQuery(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.ExecuteNonQuery();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error executing query&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// ExecuteScalar&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Object&lt;/span&gt; ExecuteScalar(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; command.ExecuteScalar();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error executing query&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetSingleValue&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; T GetSingleValue&amp;lt;T&amp;gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; T returnValue = &lt;span style=&quot;color: blue&quot;&gt;default&lt;/span&gt;(T);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Read();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(0)) { returnValue = (T)reader[0]; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; returnValue;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetSingleInt32&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Int32&lt;/span&gt; GetSingleInt32(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;Int32&lt;/span&gt; returnValue = &lt;span style=&quot;color: blue&quot;&gt;default&lt;/span&gt;(&lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt;);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Read();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(0)) { returnValue = reader.GetInt32(0); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; returnValue;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetSingleString&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; GetSingleString(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; returnValue=&lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Read();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(0)) { returnValue = reader.GetString(0); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; returnValue;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetGuidList&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;&amp;gt; GetGuidList(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;&amp;gt; returnList = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;&amp;gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; returnList = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;&amp;gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;while&lt;/span&gt; (reader.Read())&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(0)) { returnList.Add(reader.GetGuid(0)); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; returnList;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetStringList&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;&amp;gt; GetStringList(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;&amp;gt; returnList = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; returnList = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;while&lt;/span&gt; (reader.Read())&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (!reader.IsDBNull(0)) { returnList.Add(reader.GetString(0)); }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; returnList;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetSingle&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; T GetSingle&amp;lt;T&amp;gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command) &lt;span style=&quot;color: blue&quot;&gt;where&lt;/span&gt; T : &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; T dto = &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Read();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt; mapper = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;DataMapperFactory&lt;/span&gt;().GetMapper(&lt;span style=&quot;color: blue&quot;&gt;typeof&lt;/span&gt;(T));&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dto = (T)mapper.GetData(reader);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// return the DTO, it&#39;s either populated with data or null.&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dto;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetList&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;T&amp;gt; GetList&amp;lt;T&amp;gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command) &lt;span style=&quot;color: blue&quot;&gt;where&lt;/span&gt; T : &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;T&amp;gt; dtoList = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;T&amp;gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt; mapper = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;DataMapperFactory&lt;/span&gt;().GetMapper(&lt;span style=&quot;color: blue&quot;&gt;typeof&lt;/span&gt;(T));&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;while&lt;/span&gt; (reader.Read())&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; T dto = &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dto = (T)mapper.GetData(reader);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dtoList.Add(dto);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// We return either the populated list if there was data,&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// or if there was no data we return an empty list.&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dtoList;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// GetDataPage&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;DataPage&lt;/span&gt;&amp;lt;T&amp;gt; GetDataPage&amp;lt;T&amp;gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command, &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; pageIndex, &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; pageSize) &lt;span style=&quot;color: blue&quot;&gt;where&lt;/span&gt; T : &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;DataPage&lt;/span&gt;&amp;lt;T&amp;gt; page = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;DataPage&lt;/span&gt;&amp;lt;T&amp;gt;();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; page.PageIndex = pageIndex;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; page.PageSize = pageSize;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (command.Connection.State != &lt;span style=&quot;color: #2b91af&quot;&gt;ConnectionState&lt;/span&gt;.Open)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Open();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDataReader&lt;/span&gt; reader = command.ExecuteReader();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (reader.HasRows)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;IDataMapper&lt;/span&gt; mapper = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;DataMapperFactory&lt;/span&gt;().GetMapper(&lt;span style=&quot;color: blue&quot;&gt;typeof&lt;/span&gt;(T));&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;while&lt;/span&gt; (reader.Read())&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// get the data for this row&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; T dto = &lt;span style=&quot;color: blue&quot;&gt;null&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dto = (T)mapper.GetData(reader);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; page.Data.Add(dto);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// If we haven&#39;t set the RecordCount yet then set it&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt; (page.RecordCount == 0) { page.RecordCount = mapper.GetRecordCount(reader); }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt; e)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Error populating data&amp;quot;&lt;/span&gt;, e);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;finally&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; command.Connection.Close();&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; page;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #endregion&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  </description><link>http://www.referencebits.com/2010/04/aapl-part-5-how-to-write-data-access.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiYa_8J9wXvbFqSZrH4ejeoLJpOCIAjr9C9sKLWcclRFPvipVFnWY_B8l_APMaXhHrFUx9d-I7AlcFFuwdYK7w_zN8BtGc-JOo0rmLa0oc39oEPWDwaUu4rlUpGXiC9Z5ak0HFuOaRI38/s72-c?imgmax=800" height="72" width="72"/><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-5526946537888788371</guid><pubDate>Fri, 29 Jan 2010 19:29:00 +0000</pubDate><atom:updated>2010-01-29T12:29:17.291-07:00</atom:updated><title>Agile ADO.Net Persistence Layer Download is on CodePlex</title><description>&lt;p&gt;I’ve have quite a few requests for sample code so I pulled the AAPL code out of my app, put it in a stripped down ASP.Net MVC sample app and published it on CodePlex.&amp;#160;&amp;#160; You will need Visual Studio 2008,&amp;#160; ASP.Net MVC Framework version 1.0, and Sql Server 2008 to run the sample app.&amp;#160; A db backup file is included in the zip and in the source code. You can download the code at:&lt;/p&gt;  &lt;p&gt;&lt;a title=&quot;http://aapl.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=39653&quot; href=&quot;http://aapl.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=39653&quot;&gt;http://aapl.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=39653&lt;/a&gt;&lt;/p&gt;  </description><link>http://www.referencebits.com/2010/01/agile-adonet-persistence-layer-download.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2629608947246129428.post-4811917776745730460</guid><pubDate>Mon, 25 Jan 2010 16:17:00 +0000</pubDate><atom:updated>2010-01-25T09:17:51.204-07:00</atom:updated><title>Agile ADO.Net Persistence Layer Part 4: Writing data access for new data shapes</title><description>&lt;p&gt;I recently decided that I needed to take a fresh look at how to build a persistence architecture that would provide the flexibility of an ORM, that would embrace change instead of resist it (making maintenance code easier), and would use ADO.Net.&amp;#160; I started building from the ground up, threw away any best practices that resulted in friction, and Agile ADO.Net Persistence Layer is the result.&amp;#160; Part 1 in the series can be found here: &lt;a href=&quot;http://rlacovara.blogspot.com/2010/01/agile-adonet-persistence-layer-part-1.html&quot;&gt;Agile ADO.Net Persistence Layer Overview&lt;/a&gt;. &lt;/p&gt;  &lt;h4&gt;What’s a new data shape?&lt;/h4&gt;  &lt;p&gt;Has this ever happened to you?&amp;#160; You have a persistence architecture that works beautifully.&amp;#160; It’s effortless to get and save entities, everything works great.&amp;#160; Then you get a requirement for a search results page that will display data from your entity, fine. But then they also want the grid to display fields from another entity entirely.&amp;#160; It’s not even in the same aggregate!&amp;#160; You find yourself thinking things like what a stupid requirement, why would anyone want to do that. But the real problem isn’t with the requirement.&amp;#160; The problem is that your beautiful architecture is inflexible. It works great as long as you’re dealing with nicely segmented entities, but as soon as you need to handle data that is a composite of fields from two different entities, things fall apart.&amp;#160; The worst part of it is, in the back of your mind you’re thinking “it would be so easy to just write this in TSQL”.&lt;/p&gt;  &lt;p&gt;Another scenario&amp;#160; you once again have your beautiful architecture.&amp;#160; This time you need to add a couple of fields to an entity.&amp;#160; It’s a pretty simple change, but you’re dreading it because you know that in order to make this simple change, first you have to modify all of the CRUD sprocs, then you need to modify the method signatures on every data access method for this entity in the DAL, then you need to modify the parser (or data mapper methods), then you need to modify the BAL methods that call the modified DAL methods, then you need to modify the entity class itself.&amp;#160; Once again, the architecture is inflexible.&amp;#160; Changes are painful.&amp;#160; I call this friction.&lt;/p&gt;  &lt;p&gt;Changes like this happen all the time.&amp;#160; The majority of our time as developers is spent coding changes to existing systems.&amp;#160; Perversely, we usually design architectures to be really easy to use for green field projects, but really hard to use when we need to make changes to those same systems. Making it easy to deal with change was my main objective when coming up with this architecture.&amp;#160; In this post I’m going to go over solutions to the two scenarios above using Agile ADO.Net Persistence Layer.&lt;/p&gt;  &lt;h4&gt;Scenario 1:&amp;#160; Adding&amp;#160; fields to an existing entity &lt;/h4&gt;  &lt;p&gt;Just to refresh your memory, our solution looks like the screenshot below. Common contains a DataShapes folder that contains the class definition for every DTO (Data Transfer Object) used in our BAL.&amp;#160; For this scenario we’re going to be working with the Category DTO.&amp;#160; Our BAL contains a Services folder that contains a service class that provide business logic and persistence logic for each aggregate (that’s service in the DDD sense, not a web service).&amp;#160; We’ll be working with the CategoryService.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicrhuQGDyPkcxYffx2KqxLYSlEYfMoW_-eY7IoXJWX0bsUD2h9qqJ6Wn-hvkw-VJxjJ5bUvySfIkdhbf7VZZaqZOY6WwMSVyimDvRbIxr0lLuW_tn7ZqUfg1ccGe0_EVO4dBpi5OGaRoA/s1600-h/image%5B9%5D.png&quot;&gt;&lt;img style=&quot;border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_4b59mUYfQbPO0XW-9rvlasx76As42qFx3s5qFlDEFypU8WXJMAMIF2wphMnxPTMUYaXnptwoZQu1Sg5DzxyamSHohXfW-p1xqb096q___lXgdnCDsVa766lHC9Te_dqT21hyphenhyphenlRvLWxw/?imgmax=800&quot; width=&quot;335&quot; height=&quot;609&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So, our Category data shape is a straight DTO. It has no methods, just properties.&amp;#160; It looks like this.&lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Category&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; CategoryGuid { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; CategoryName { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; CategoryKey { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; Category()&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CategoryGuid = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullGuid; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CategoryName = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullString;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CategoryKey = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullString;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Our CategoryService class contains business logic and data access methods.&amp;#160; Each data access method does three things, it defines a query, it selects a data shape to return, then it passes both the query and data shape to our DAO (Data Access Object) which executes the query and maps the results to the data shape. Our data access methods in the Category Service look like this.&amp;#160; You can see both methods returns a List&amp;lt;Category&amp;gt;.&lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Category&lt;/span&gt;&amp;gt; GetAllCategories&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; query = &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;SELECT * &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM Category &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ORDER BY CategoryName&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(query);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dao.GetList&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Category&lt;/span&gt;&amp;gt;(command);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Category&lt;/span&gt;&amp;gt; GetAllCategoriesInUse&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; query = &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;SELECT DISTINCT c.*&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM Category c&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; JOIN PostCategory p on p.CategoryGuid=c.CategoryGuid&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ORDER BY CategoryName&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(query);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dao.GetList&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Category&lt;/span&gt;&amp;gt;(command);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Now for our simple change.&amp;#160; We forgot to add a CreatedUtc field to our Category entity to store the date when a category was created.&amp;#160; Crap.&amp;#160; We already have a bunch of code written that uses these classes.&amp;#160; How hard is it going to be to make this change without breaking any of our data access methods and UI code.&amp;#160; Well the first step is obvious, we need to add CreatedUtc to the database table.&amp;#160; So we do that.&amp;#160; Now for the tough part.&amp;#160; Do we need to modify any sprocs? No.&amp;#160; Do we need to modify any DAL methods? No.&amp;#160; How about the data access methods in our service class, we need to change those right? Nope. The queries defined in the data access methods use “SELECT *” so they’ll pick up any new fields added to the Category table.&amp;#160; How about the DAO, surely we need to change some mapping code there?&amp;#160; Nope, it uses a little reflection to automatically map query results to a data shape based on field name.&amp;#160; As long as we use CateogryUtc for both our database column name and our Category DTO field name, no code changes are needed.&amp;#160; So what do we need to do?? Just one thing, we need to add CreatedUtc to our Category data shape like this.&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Category&lt;/span&gt;&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; CategoryGuid { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt;&amp;#160;&lt;font color=&quot;#2b91af&quot;&gt;DateTime&lt;/font&gt; CreatedUtc { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; CategoryName { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; CategoryKey { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; Category()&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CategoryGuid = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullGuid;&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CreatedUtc = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullDateTime; &lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CategoryName = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullString;&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CategoryKey = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullString;&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;  &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt;  &lt;p&gt;Done.&amp;#160; You have to admit, even for a simple change, that was pretty easy.&amp;#160; Just add the new field to the Category DTO, add the new column to the table, and the persistence layer handles the rest.&amp;#160; Now I know some of you are thinking “But what if I don’t want to use the same names for my columns and DTO fields” or “No way would I ever use reflection in my DAL, it’s too slow”.&amp;#160; Those are valid concerns, and I have an easy way to tune the DAO to address both of those issues, but the nice part is that if you’re willing to work within these constraints, your development can go quickly and with very little friction.&lt;/p&gt;  &lt;h4&gt;Scenario 2: Adding a new data shape&lt;/h4&gt;  &lt;p&gt;Now let’s look at the case where we need a new composite data shape that consists of data from two different aggregates.&amp;#160; Our site has Categories, Blogs, and BlogPosts. The customer tells us that we need to display a list of the the most popular categories along with a count of how many BlogPosts are in each Category. This doesn’t really fit with how we did our initial modeling for this project.&amp;#160; A BlogPost contains a collection of Categories not the other way around.&amp;#160; Still, the data model will support it, we just have a simple join table between the Category table and the BlogPost table.&amp;#160; There’s no reason we can’t write a query that uses the relationship in the other direction. &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgA0bfPgwhCoVvpHK1e_Q7EBBp0IwzMYVm2AFuZPQfGDtoeoQqb75ilc8RT9M9ruajg2OcSSZHmvkcwemXIeEaRCEHEpoOFLt1DNp_rJcQyAhs8Uxg5VKOIr0Ih4ZO80sWbElxq6qIwplw/s1600-h/image%5B19%5D.png&quot;&gt;&lt;img style=&quot;border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px&quot; title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg14wHkaeCTmdXGCr8-PeUZqqsZA3UDsauhfR5ZMhIWCUKSGEM0_JCWDt39wC8K-uf5fUSU1aRMPYlNBLkJOBNRdVJ8bUB6JfqDPxZOoGd92ZkX5aOB8Heeaw8k78zBZ32dS-2_qXCDWgI/?imgmax=800&quot; width=&quot;390&quot; height=&quot;291&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So, our solution is to add a new data shape called CategoryWithPostCount that will contain the category data plus a count of blog posts that are using that category.&amp;#160; Because we’re using an architecture that anticipates and embraces change, adding this new data shape will be a simple 2 step process.&amp;#160; First we create the CategoryWithPostCount DTO and add it to our Common/DataShapes folder.&lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;CategoryWithPostCount&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt; CategoryGuid { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; CategoryName { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; CategoryKey { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; PostCount { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; CategoryWithPostCount()&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CategoryGuid = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullGuid; &lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CategoryName = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullString;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CategoryKey = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullString;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PostCount = &lt;span style=&quot;color: #2b91af&quot;&gt;NullValues&lt;/span&gt;.NullInt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Second, we open up our CategoryService class and add a new GetTopCategoryList data access method.&amp;#160; Remember that our framework does most of the heavy lifting for us.&amp;#160; All we need to do is define the query, pick a data shape, and then pass both to the DAO which will automatically map them by field name.&amp;#160; Writing the method is easy.&amp;#160; We fire up Sql Server Management Studio, write and test our query, copy one of the other data access methods, and paste our new query into it.&amp;#160; Here’s the result.&lt;/p&gt;  &lt;div style=&quot;font-family: courier new; background: white; color: black; font-size: 9pt&quot;&gt;   &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;CategoryWithPostCount&lt;/span&gt;&amp;gt; GetTopCategoryList(&lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt; pageSize)&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;{&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt; query = &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;.Format(&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #a31515&quot;&gt;@&amp;quot;SELECT TOP {0} c.*, q1.PostCount&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM Category c&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; JOIN (&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SELECT c.CategoryGuid, count(pc.PostGuid) as PostCount&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM PostCategory pc &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; JOIN Category c ON c.CategoryGuid = pc.CategoryGuid &lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Group By c.CategoryGuid&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ) q1 ON q1.CategoryGuid = c.CategoryGuid&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ORDER BY q1.PostCount DESC&lt;/span&gt;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;&lt;/span&gt;, pageSize);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlDao&lt;/span&gt; dao = SharedSqlDao;&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;SqlCommand&lt;/span&gt; command = dao.GetSqlCommand(query);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt; dao.GetList&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;CategoryWithPostCount&lt;/span&gt;&amp;gt;(command);&lt;/p&gt;    &lt;p style=&quot;margin: 0px&quot;&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;That’s it, we’re done.&amp;#160; A brand new data shape complete with data access method ready to use in minutes.&amp;#160; When it’s this easy to add new data shapes, it really frees you up to work in a much more agile way.&amp;#160; You don’t need to worry about getting entity classes designed exactly right the first time, or missing pieces of data like a count or date created.&amp;#160; If you do, the architecture is designed to make it painless to add that stuff as it’s needed.&amp;#160; And none of this is hard, none of it is magic, we’ve just made some decisions and accepted some limitations (like DTO field names and table column names will be the same) that allow us to take the friction out of design changes.&lt;/p&gt;  &lt;p&gt;Next time we’ll get into the DOA and the associated DataMapper classes. &lt;/p&gt;  </description><link>http://www.referencebits.com/2010/01/agile-adonet-persistence-layer-part-4.html</link><author>noreply@blogger.com (Rudy Lacovara)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_4b59mUYfQbPO0XW-9rvlasx76As42qFx3s5qFlDEFypU8WXJMAMIF2wphMnxPTMUYaXnptwoZQu1Sg5DzxyamSHohXfW-p1xqb096q___lXgdnCDsVa766lHC9Te_dqT21hyphenhyphenlRvLWxw/s72-c?imgmax=800" height="72" width="72"/><thr:total>8</thr:total></item></channel></rss>