/atom.xml

https://github.com/efvincent/efvincent.github.io · XML · 3407 lines · 2953 code · 441 blank · 13 comment · 0 complexity · c1da7adda36d3fc8816553280e9e26fc MD5 · raw file

Large files are truncated click here to view the full file

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <feed xmlns="http://www.w3.org/2005/Atom">
  3. <title><![CDATA[Curried Functions]]></title>
  4. <link href="http://efvincent.github.io/atom.xml" rel="self"/>
  5. <link href="http://efvincent.github.io/"/>
  6. <updated>2014-08-18T13:03:44-04:00</updated>
  7. <id>http://efvincent.github.io/</id>
  8. <author>
  9. <name><![CDATA[Eric F. Vincent]]></name>
  10. </author>
  11. <generator uri="http://octopress.org/">Octopress</generator>
  12. <entry>
  13. <title type="html"><![CDATA[Debounce as a Directive]]></title>
  14. <link href="http://efvincent.github.io/blog/2014/08/18/debounce-as-a-directive/"/>
  15. <updated>2014-08-18T01:28:33-04:00</updated>
  16. <id>http://efvincent.github.io/blog/2014/08/18/debounce-as-a-directive</id>
  17. <content type="html"><![CDATA[<p>Some months ago a co-worker asked if there were a way to buffer the input on a text box
  18. so that the handler would not be called more than every X milliseconds. His app was doing
  19. the fairly typical chore of searching through a long list as the user types into a
  20. search box. Firing a <code>digest</code> (thats Angulars re-bind and re-render loop) for each
  21. keystroke causes a jumpy, jittery refresh storm.</p>
  22. <p>What he was looking for is commonly referred to as to <em>debounce</em> or <em>throttle</em> a
  23. function. There are many implementations around, but his question was specifically
  24. about debounce in the context of Angular, preferably as a directive.</p>
  25. <!-- more -->
  26. <p>I found a snippet authored by <a href="http://tommaitland.net">Tom Maitland</a> that does just
  27. what we need. I tweaked it slightly (also as a <a href="https://gist.github.com/efvincent/9784923">Gist</a>
  28. and <a href="http://jsfiddle.net/efvincent/vkphp2fa/">JSFiddle</a>):</p>
  29. <div><table class="CodeRay"><tr>
  30. <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
  31. <a href="#n2" name="n2">2</a>
  32. <a href="#n3" name="n3">3</a>
  33. <a href="#n4" name="n4">4</a>
  34. <a href="#n5" name="n5">5</a>
  35. <a href="#n6" name="n6">6</a>
  36. <a href="#n7" name="n7">7</a>
  37. <a href="#n8" name="n8">8</a>
  38. <a href="#n9" name="n9">9</a>
  39. <strong><a href="#n10" name="n10">10</a></strong>
  40. <a href="#n11" name="n11">11</a>
  41. <a href="#n12" name="n12">12</a>
  42. <a href="#n13" name="n13">13</a>
  43. <a href="#n14" name="n14">14</a>
  44. <a href="#n15" name="n15">15</a>
  45. <a href="#n16" name="n16">16</a>
  46. <a href="#n17" name="n17">17</a>
  47. <a href="#n18" name="n18">18</a>
  48. <a href="#n19" name="n19">19</a>
  49. <strong><a href="#n20" name="n20">20</a></strong>
  50. <a href="#n21" name="n21">21</a>
  51. <a href="#n22" name="n22">22</a>
  52. <a href="#n23" name="n23">23</a>
  53. <a href="#n24" name="n24">24</a>
  54. <a href="#n25" name="n25">25</a>
  55. <a href="#n26" name="n26">26</a>
  56. </pre></td>
  57. <td class="code"><pre><span class="comment">// Defines the module as &quot;app&quot;, this is not best practice for module</span>
  58. <span class="comment">// definition or project structure, focus on the directive</span>
  59. angular.module(<span class="string"><span class="delimiter">'</span><span class="content">app</span><span class="delimiter">'</span></span>, []).directive(<span class="string"><span class="delimiter">'</span><span class="content">tlDebounce</span><span class="delimiter">'</span></span>, <span class="keyword">function</span>(<span class="predefined">$timeout</span>) {
  60. <span class="keyword">return</span> {
  61. <span class="key">restrict</span>: <span class="string"><span class="delimiter">'</span><span class="content">A</span><span class="delimiter">'</span></span>,
  62. <span class="key">require</span>: <span class="string"><span class="delimiter">'</span><span class="content">ngModel</span><span class="delimiter">'</span></span>,
  63. <span class="key">priority</span>: <span class="integer">99</span>,
  64. <span class="function">link</span>: <span class="keyword">function</span>(scope, elm, attr, ngModelCtrl) {
  65. <span class="keyword">if</span> (attr.type === <span class="string"><span class="delimiter">'</span><span class="content">radio</span><span class="delimiter">'</span></span> || attr.type === <span class="string"><span class="delimiter">'</span><span class="content">checkbox</span><span class="delimiter">'</span></span>) <span class="keyword">return</span>;
  66. elm.unbind(<span class="string"><span class="delimiter">'</span><span class="content">input</span><span class="delimiter">'</span></span>);
  67. <span class="keyword">var</span> debounce;
  68. elm.bind(<span class="string"><span class="delimiter">'</span><span class="content">input</span><span class="delimiter">'</span></span>, <span class="keyword">function</span>() {
  69. <span class="predefined">$timeout</span>.cancel(debounce);
  70. ngModelCtrl.<span class="predefined">$setViewValue</span>(elm.val());
  71. }, attr.tlDebounce || <span class="integer">1000</span>);
  72. });
  73. elm.bind(<span class="string"><span class="delimiter">'</span><span class="content">blur</span><span class="delimiter">'</span></span>, <span class="keyword">function</span>() {
  74. scope.<span class="predefined">$apply</span>(<span class="keyword">function</span>() {
  75. ngModelCtrl.<span class="predefined">$setViewValue</span>(elm.val());
  76. });
  77. });
  78. }
  79. }
  80. });
  81. </pre></td>
  82. </tr></table>
  83. </div>
  84. <h2 id="examining-the-directive">Examining the directive</h2>
  85. <p>The directive starts at line 3 by defining a module and calling the <code>directive</code> function
  86. which causes Angular to register a directive in that module. This directive is called <code>tlBounce</code>.
  87. To define a directive we pass a function and our function returns a directive definition object.</p>
  88. <h3 id="injecting-angulars-timer-service">Injecting Angulars Timer service</h3>
  89. <p>The debounce directive will use a timer to assure that when attached to a text box, the underlying
  90. model will only be updated every X milliseconds. Well see the algorithm in a bit.</p>
  91. <p>When Angular needs an instance of our directive, it will call the function weve provided. Angular
  92. will inspect the function and detect that it has a dependency; to call the function Angular must
  93. provide, or <em>inject</em> something called <code>$timeout</code>, which is one of Angulars services. Angular offers
  94. many <em>services</em> to you as the application developer to use in creating a directive (or factory,
  95. filter, controller, or the other Angular things). These services are objects or functions provided by the framework.</p>
  96. <p>The tip-off that <code>$timeout</code> is a service is the leading dollar sign. Angular will use its
  97. <a href="https://docs.angularjs.org/api/auto/service/$injector#!">Injector</a> service to find a <code>$timeout</code> and pass it
  98. to us. Well then use the <code>$timeout</code> in our <em>link function</em>.</p>
  99. <h3 id="the-link-function">The link function</h3>
  100. <p>Without getting into the guts of directive development, suffice it to say that in most
  101. cases when writing a directive youll want to focus on the <code>link</code> function. For more information
  102. on the link function, theres the <a href="http://angularjs.org/">Angular Docs</a>, and another good source of information is
  103. <a href="http://www.angularjshub.com/examples/customdirectives/compilelinkfunctions/">AngularJS Hub</a>.</p>
  104. <p>The <code>link</code> function sets up each instance of the directive. You supply the function that can have
  105. up to four parameters. <code>link: function(scope, elm, attr, ngModelCtrl)</code>.</p>
  106. <h4 id="link-function-parameters">Link function parameters</h4>
  107. <p>The first is the directives local scope, which is usually used like you use scope in a controller
  108. to maintain and bind to the internal state of the directive. Well see how <code>scope</code> is used here in a second.</p>
  109. <p>Second is the <code>elm</code> or <em>element</em> parameter. This is the DOM element that the directive to which the
  110. directive is attached. For debounce, the directive is attached to an input, usually a text box. You can do the
  111. usual DOM-stuff to the element, attach event handlers, change content, add children, etc. AAMOF you actually
  112. have an Angular wrapper around the element, so you get some additional JQuery like functions on the element.</p>
  113. <p>Third is <code>attr</code>, the attributes. This is a map of the attributes on the element to which our directive is
  114. attached. In our case, were using the <code>attr</code> to detect if were attached to a radio button or check box; our
  115. deBounce doesnt have a meaning for those controllers, so we bail if we see that were attached to one (line 9).</p>
  116. <p>Lastly is the <code>ngModelCtrl</code>, which is the least intuitive. This is a controller requirement of our directive,
  117. it says that any directive were attached to needs to have an <code>ngModelCtrl</code> controller. This sort of limits
  118. our deBouncer, but the target use case is to put this directive on a text box thats using AngularJSs binding.</p>
  119. <h3 id="debounce-algorithm">DeBounce Algorithm</h3>
  120. <p>The strategy is as follows:</p>
  121. <ol>
  122. <li>Detach the input handler (line 11) that usually updates the model</li>
  123. <li>Bind a new input handler
  124. <ol>
  125. <li>Cancel any pending debounce timer (line 15)</li>
  126. <li>Set up a new debounce timer
  127. <ol>
  128. <li>It should go off in the time specified by the <code>tl-debounce</code> attribute, or 1,000ms if the attribute is
  129. not specified (line 18).</li>
  130. </ol>
  131. </li>
  132. <li>When it goes off, it should tell the model controller <code>ngModelCtrl</code> to set its value <code>.$setViewValue(elm.val())</code></li>
  133. </ol>
  134. </li>
  135. <li>Bind a new blur handler, so when th user leaves the field the model is always updated.
  136. <ol>
  137. <li>Put the call to <code>$setNewValue()</code> inside a <code>scope.apply()</code> so the change causes a digest (Angular rebinds everything).</li>
  138. </ol>
  139. </li>
  140. </ol>
  141. <h3 id="the-digest">The Digest</h3>
  142. <p>One important point - you may be thinking that you could have used the standard JavaScript timer. Why
  143. use Angulars <code>$timer</code> service? Its because Angular needs to know when the model changes so that it
  144. can perform its two way binding / model-view synchronization. By using Angulars timer, you can be assured
  145. that Angular will know when the timer goes off and will do all the right Angular binding stuff at that time.</p>
  146. <h3 id="but-eric-youre-wrong-and-oh-so-stupid">But Eric, youre wrong and oh so stupid!</h3>
  147. <blockquote>
  148. <p>You <strong>could</strong> use the normal timer, because when you call <code>.$setViewValue()</code>
  149. youre letting Angular know something needs to change, right? I mean, it starts in a dollar sign,
  150. so its all Angulary, right? </p>
  151. </blockquote>
  152. <p>Heh. Youd think so, but youd be wrong. This is the kind of thing that makes you scratch your head, then waste
  153. fifteen minutes, then look up the docs, then unleash a stream of profanities. It happens that <code>.$setViewValue()</code> does
  154. <strong>not</strong> cause a digest, probably because of performance or some other really good reason. Doesnt make it fun though, and
  155. its the kind of undiscoverable crap that qualifies as a legit complaint about AngularJS. Take your medicine.</p>
  156. <p>So thats DeBounce - it actually works pretty well for things like text boxes that do searches and stuff like that.
  157. I use it in production, but theres no warrentee so YMMV. Have a good one</p>
  158. <p>-e</p>
  159. ]]></content>
  160. </entry>
  161. <entry>
  162. <title type="html"><![CDATA[GitHubify(Blog)]]></title>
  163. <link href="http://efvincent.github.io/blog/2014/08/15/Octopress-github-ruby-windows/"/>
  164. <updated>2014-08-15T12:00:11-04:00</updated>
  165. <id>http://efvincent.github.io/blog/2014/08/15/Octopress-github-ruby-windows</id>
  166. <content type="html"><![CDATA[<p>For my shiney new blog I decided to go full hipster and host on GitHub. This means that
  167. the entire site, source code as well as the blog website itself, can be seen at
  168. <a href="https://github.com/efvincent/efvincent.github.io">my blogs repository</a>. Of course all of this will be painfully
  169. uncool by this time next year. But for the time being its fun and the tech is relevant.</p>
  170. <p>Dig in if you want to see the ins and outs of setting all this up on a Windows workstation,
  171. because Windows is so far out of fashion stuff is starting to not work on it by default. Karma.
  172. <!--more -->
  173. ## Static Site Generation
  174. The way GitHub hosted web sites work is that the entire web site is pure static HTML,
  175. JavaScript, and CSS. If you work in the industry you realize that all the web site
  176. frameworks (server side) like JSP, ASP.NET, Rails, PHP, etc. all <em>generate</em> pages on
  177. the fly as the requests come in. This requires a server be running somewhere that can
  178. run .NET, Java, Ruby, or whatever.</p>
  179. <p>A static site everything is pre-generated, so any server that can respond to requests
  180. by serving up a file can host a static site. But no one wants to hand craft HTML pages
  181. for every blog post. So what you have is a generator; some kind of program that can take
  182. blog posts written in plain text and <strong>poof!</strong> Generate a static website.</p>
  183. <p>The basic idea is that you have a <em>source</em> directory that has your blog posts written
  184. in (typically) <a href="https://en.wikipedia.org/wiki/Markdown">markdown</a>, CSS, templates for HTML,
  185. and the Ruby / JavaScript / whatever that the generator is written in.</p>
  186. <p>Theres typically a command line interface; so you issue the proper command the
  187. generator takes your markdown and creates a complete, static web site.</p>
  188. <h2 id="github">GitHub</h2>
  189. <p>Thats where <a href="www.github.com">GitHub</a> comes in. If you dont know what GitHub is, or
  190. if youre completely lost at this point, then you probably need to hit up this
  191. <a href="https://www.youtube.com/watch?v=oHg5SJYRHA0">everything you need to know to become a modern developer</a>
  192. tutorial.</p>
  193. <p>Think about it for two seconds, it makes perfect sense. GitHub hosts files in a repository and is a web site.
  194. So if you put a static web sites worth of files <em>in a repository</em>, and GitHub does a tiny bit of magic, it
  195. can serve those web pages up. And thats all a GitHub hosted static site is. You write posts in
  196. markdown, you use a generator to create a static site, and you check it into GitHub.</p>
  197. <h2 id="octopress">OctoPress</h2>
  198. <p>I admittedly dont have a ton of experience with static site generators. I played with
  199. a couple before committing to <a href="www.octopress.org">Octopress</a>. At first I wanted to
  200. use a <a href="http://nodejs.org">Node.js</a> based static site generator. I googled around a bit
  201. and ended up playing with <a href="https://github.com/jnordberg/wintersmith">Wintersmith</a> for
  202. a night. It worked, but GitHub makes it easiest to use a Ruby based generator called <a href="http://jekyllrb.com">Jekyll</a>.
  203. A bit more poking around and I ended up with <a href="www.octopress.org">Octopress</a>, which is
  204. built on top of Jekyll, offers additional features, and incredibly well written, well
  205. documented, and easy to use.</p>
  206. <p>When you go to their site youll get walked through the installation step by step,
  207. and there are some nice Ruby build tools available to get things moving, including
  208. configuring your new Octopress generator to work with GitHub web sites. <strong>Highly recommended</strong></p>
  209. <h2 id="ruby">Ruby</h2>
  210. <p>Ok so <a href="https://www.youtube.com/watch?v=gBkDvqIGSaE">Ruby hates Windows</a>. It might be passive,
  211. but the ill feelings are there. Which is fine, everyone cant love everyone, and Microsoft
  212. has kinda earned it by being a dick until very recently. But whatever, Im not with starting
  213. flame war. It happens that I still use Windows for all kinds of reasons, so I needed to
  214. get Ruby on my workstation.</p>
  215. <p>Actually I had installed Ruby using <a href="http://chocolatey.org/">Chocolately</a> because I
  216. love playing with all the languages, but to run Jekyll youll need the
  217. <a href="http://rubyinstaller.org/downloads/">Ruby Development Kit</a>. The RDK is platform
  218. specific, and is needed to build <em>gems</em> (Ruby packages) that are platform specific,
  219. which Jekyll either is or depends on indirectly (I didnt check).</p>
  220. <p>There are <a href="https://github.com/oneclick/rubyinstaller/wiki/Development-Kit">very clear instructions</a>
  221. that you can follow for getting this set up. Once thats done you can play along with
  222. the <a href="www.octopress.org">Octopress</a> setup instructions, which are great. Just follow the
  223. track that has you deploy to GitHub.</p>
  224. ]]></content>
  225. </entry>
  226. <entry>
  227. <title type="html"><![CDATA[Abandonment Issues]]></title>
  228. <link href="http://efvincent.github.io/blog/2014/08/15/abandonment-issues/"/>
  229. <updated>2014-08-15T11:52:11-04:00</updated>
  230. <id>http://efvincent.github.io/blog/2014/08/15/abandonment-issues</id>
  231. <content type="html"><![CDATA[<p>Ive moved my blog from WordPress on my own personal site to GitHub, because GitHub of course. <em>No One</em> is going
  232. to miss my other blog. It had a couple of what I thought were interesting posts on F# and dependency injection in C#. But its time
  233. for a fresh start.</p>
  234. <p>Expect posts on JavaScript and front end development for a little while. After a career spent designing relational
  235. databases for finance, insurance and other LOB domains, Ive been working on SPA style front end enterprise applications for
  236. a couple of years. And its fun. I may actually have a couple of interesting things to say. Well see.</p>
  237. ]]></content>
  238. </entry>
  239. <entry>
  240. <title type="html"><![CDATA[DI Constructor Injection, Bootstrapping]]></title>
  241. <link href="http://efvincent.github.io/blog/2011/06/24/di-bootstrap/"/>
  242. <updated>2011-06-24T02:07:31-04:00</updated>
  243. <id>http://efvincent.github.io/blog/2011/06/24/di-bootstrap</id>
  244. <content type="html"><![CDATA[<h2 id="constructor-injection">Constructor Injection</h2>
  245. <p>The idea of dependency injection is that classes are defined such that any dependencies on other classes or services, are <em>injected</em> into the class by some external mechanism, as opposed to being newed up directly. The most common form of DI is constructor injection, where a class defines a constructor that has as its parameters the external dependencies required by the class.
  246. <!-- more -->
  247. There are several benefits to this particular method of injection; the most obvious is that in a well designed system the dependencies of a class are clearly visible in the constructor. In the <a href="http://blog.efvincent.com/practical-di-101">DI 101</a> post a data provider was defined like this:</p>
  248. <div><table class="CodeRay"><tr>
  249. <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
  250. <a href="#n2" name="n2">2</a>
  251. <a href="#n3" name="n3">3</a>
  252. <a href="#n4" name="n4">4</a>
  253. <a href="#n5" name="n5">5</a>
  254. <a href="#n6" name="n6">6</a>
  255. <a href="#n7" name="n7">7</a>
  256. <a href="#n8" name="n8">8</a>
  257. <a href="#n9" name="n9">9</a>
  258. <strong><a href="#n10" name="n10">10</a></strong>
  259. <a href="#n11" name="n11">11</a>
  260. <a href="#n12" name="n12">12</a>
  261. <a href="#n13" name="n13">13</a>
  262. <a href="#n14" name="n14">14</a>
  263. </pre></td>
  264. <td class="code"><pre><span class="directive">public</span> <span class="type">class</span> <span class="class">DevDataProvider</span> : IDataProvider {
  265. <span class="directive">private</span> readonly IIdentService _identService;
  266. <span class="directive">private</span> readonly ILogService _logSvc;
  267. <span class="directive">private</span> <span class="directive">static</span> readonly <span class="predefined-type">List</span>&lt;Employee&gt; EmployeeStore = <span class="keyword">new</span> <span class="predefined-type">List</span>&lt;Employee&gt;();
  268. <span class="directive">public</span> DevDataProvider(IIdentService identService, ILogService logSvc) {
  269. <span class="keyword">if</span> (identService == <span class="predefined-constant">null</span>) <span class="keyword">throw</span> <span class="keyword">new</span> ArgumentNullException(<span class="string"><span class="delimiter">&quot;</span><span class="content">identService</span><span class="delimiter">&quot;</span></span>);
  270. <span class="keyword">if</span> (logSvc == <span class="predefined-constant">null</span>) <span class="keyword">throw</span> <span class="keyword">new</span> ArgumentNullException(<span class="string"><span class="delimiter">&quot;</span><span class="content">logSvc</span><span class="delimiter">&quot;</span></span>);
  271. _identService = identService;
  272. _logSvc = logSvc;
  273. }
  274. <span class="comment">// Remaining implementation omitted for brevity</span>
  275. }
  276. </pre></td>
  277. </tr></table>
  278. </div>
  279. <p>The constructor is on line 6. From this constructor we can see that the DevDataProvider has dependencies on an IIdentityService and an ILogService. There should be no other dependencies in the class other than to well known, stable libraries like the <a href="http://msdn.microsoft.com/en-us/library/hfa3fa08.aspx">BCL</a>.</p>
  280. <p>There are other advantages to using constructor injection. Should the list of dependencies get too long, say longer than four parameters, youve got a code smell that perhaps the class is doing too much, violating the single responsibility principal.</p>
  281. <h2 id="bootstrapping">Bootstrapping</h2>
  282. <p>In order to be able to resolve dependencies, the DI container must be configured. This set up is done during the <strong>bootstrapping</strong> phase. Typically this only needs to be done once, but changes to the container make sense in some scenarios like when a DI container is being used to support extensions or plug-ins. In that case components might be added or removed from the DI container while the app is running. These scenarios are out of scope for this post.</p>
  283. <p>The container may be configured in several ways Auto configuring, configuration in code, and configuration files (typically XML / app or web.config files). My current favorite DI framework is AutoFac, and I typically configure in code, but different projects will have different demands, so familiarize yourself with the specifics of your selected framework and understand the tradeoffs involved in the different types of configuration. You can even configure the DI container using more than one method perhaps Auto configuring for the bulk of the registrations, then code or XML for more specific configuration needs.</p>
  284. <h2 id="bootstrapping-a-console-application">Bootstrapping a Console Application</h2>
  285. <p>Depending on the type of application youre working on, there are specific places for bootstrapping to take place. The <em>place</em> to do configuration and bootstrapping is sometimes referred to as the <strong>composition root</strong> <em>(you can read about these concepts in more detail in <a href="http://www.manning.com/seemann/">Mark Seemans Dependency Injection</a> book, published by Manning)</em>.</p>
  286. <p>In a console application, the static Main() method is a typical place to configure the container. While we rarely write console apps in production (at least I rarely do), the simplicity makes it easy to see the implications of the bootstrapping procedure.</p>
  287. <p>In the following sequence diagram, in step one [1] the Main() entry point is called on the console application. Main() is serving as the composition root. From there a private Bootstrap() methods is called [2] and the DI container is configured. The exact mechanism varies by framework.</p>
  288. <p><a href="http://blog.efvincent.com/wp-content/uploads/2011/06/Capture.png"><img src="http://blog.efvincent.com/wp-content/uploads/2011/06/Capture_thumb.png" alt="Capture" /></a></p>
  289. <p>Once the container is configured, the main entry point requests that the DI container resolve the App type [3]. The DI container creates whatever dependencies are required by the App [4]. This happens hierarchically; dependencies may themselves have dependencies and so on. The DI container sorts all this out and is also responsible for lifetimes of create objects etc. The DI container can create and return the instance off the App [5]. The Main() function can then pass control to the app [6] which will leverage the injected dependencies [7] to do the real work.</p>
  290. <h2 id="only-directly-reference-the-di-container-in-the-bootstrapper">Only Directly Reference the DI Container in the Bootstrapper</h2>
  291. <p>This is an important point, and if you get nothing else from this post, understand this.</p>
  292. <ul>
  293. <li>The DI container is configured in the composition root (Main() in this case)</li>
  294. <li>The DI container is used to resolve or build the App</li>
  295. <li>The app is then run to do the work</li>
  296. </ul>
  297. <p>Once the app is instantiated, it should have all of its dependencies injected. <strong>The app should not have a reference to the DI container!</strong> If we allow the app or any of its dependencies to have access to the container, then several bad things happen:</p>
  298. <h4 id="weve-taken-on-a-dependency-to-the-di-container-itself">Weve taken on a dependency to the DI Container itself</h4>
  299. <p>Yes its true that the assembly has a dependency on the DI container. But for purposes of this discussion the assembly is not the application. The App class and the services (other classes) it depends on is the application. We dont want to take a dependency on the DI container in those classes; rather, we should be able to switch to a different DI container if needed and not effect the App and the dependent services.</p>
  300. <p>In any kind of a significant application the apps classes would be in a different assembly, and services might be scattered across even more assemblies, and those should not have a dependency on a DI container. They should however be designed and built with the DI pattern in mind with the dependencies specified in the constructor, with references to abstract types or interfaces, rather than to concrete implementations.</p>
  301. <h4 id="were-hiding-a-dependency-inside-the-app">Were hiding a dependency inside the App</h4>
  302. <p>Earlier I mentioned that a benefit of constructor injection is that the dependencies are clearly visible (even <em>documented</em> if you will) in the signature of the constructor. We really dont want to see lines like this buried in the methods of the classes:</p>
  303. <div><table class="CodeRay"><tr>
  304. <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
  305. <a href="#n2" name="n2">2</a>
  306. <a href="#n3" name="n3">3</a>
  307. <a href="#n4" name="n4">4</a>
  308. <a href="#n5" name="n5">5</a>
  309. <a href="#n6" name="n6">6</a>
  310. <a href="#n7" name="n7">7</a>
  311. <a href="#n8" name="n8">8</a>
  312. </pre></td>
  313. <td class="code"><pre><span class="comment">// Anti-pattern - don't use DI container except</span>
  314. <span class="comment">// in composition root</span>
  315. var dal = <span class="predefined-type">Container</span>.Resolve&lt;IDataAccessService&gt;();
  316. <span class="comment">// And defintely don't do this</span>
  317. var dal = <span class="keyword">new</span> SqlDataAccessService(connectString);
  318. </pre></td>
  319. </tr></table>
  320. </div>
  321. <p>A class that that has these lines buried inside somewhere has hidden dependencies on both the DI container and IDataAccessService (or worse, by using the new keyword directly, on the SqlDataAccessService). These hidden dependencies undermine the benefits of using DI containers at all.</p>
  322. <h3 id="bootstrapping-in-other-application-types">Bootstrapping in other Application Types</h3>
  323. <p>Other types of apps have different places for bootstrapping and application roots. Unlike a console app, an ASP.NET MVC 3 application isnt top-down linear, the application must respond to web requests. It does so by creating instances of controllers, and calling methods on those controllers to respond to web requests.</p>
  324. <p>A controller in MVC3 is like the app was in our console example above. It will be resolved, or created, by the DI container. Controllers are different in that there will likely be several different controllers in an MVC application. Also, we dont get to resolve a controller and tell it to run right from the composition root, the ASP.NET MVC framework will be receiving web requests and will need to resolve controllers later, after bootstrapping.</p>
  325. <p>In ASP.NET MVC 3 this is accomplished by providing a <em>hook</em>, or a place where we can supply a DI container for ASP.NET MVC 3 to use when creating controllers. The developer configures the DI container, and then wires that container into the MVC framework via an instance of IControllerActivator. In the case of AutoFac, theres a <a href="http://nuget.org/List/Packages/Autofac.Mvc3">NuGet package called AutoFac.Mvc3</a> that includes classes to integrate with MVC3. The implementation details are beyond the scope of this post just <a href="http://duckduckgo.com/">DuckDuckGo</a> AutoFac.Mvc and find a wealth of additional detail. Same goes for WCF, WPF, and Silverlight applications. There are best practices for configuring DI containers for each app type.</p>
  326. <h3 id="di-unfriendly-application-types">DI Unfriendly Application Types</h3>
  327. <p>Some application types just do not lend themselves very easily to dependency injection patterns. Classic ASP.NET pops into mind immediately. It was written before Microsoft was as willing to accept OSS, community driven concepts such as DI Containers. A big red flag with ASP.NET is that all subclasses to the Page class (which is what all your ASP.NET pages are) must have a parameterless default constructor. Well there goes parameter injection!</p>
  328. <p>There are other mechanisms for implementing DI patterns in this case, but theyre sub-optimal. Again Id refer you to <a href="http://www.manning.com/seemann/">Mark Seemans Dependency Injection</a> book, which is far and away the best DI book in the .NET space, for advice and examples in dealing with DI unfriendly application types.</p>
  329. <h3 id="in-summary">In Summary</h3>
  330. <p>Hopefully this was helpful in your understanding of a couple of key aspects of using DI containers. Practice a few console applications, and write some tests too. Once you get the idea, move on to more interesting application types. Before long youll be shocked you ever wrote applications <em>without</em> some degree of dependency injection. Yea its that good for you.</p>
  331. ]]></content>
  332. </entry>
  333. <entry>
  334. <title type="html"><![CDATA[A Taste of Dependency Injection, Testing, and Mocking]]></title>
  335. <link href="http://efvincent.github.io/blog/2011/05/27/di-mock/"/>
  336. <updated>2011-05-27T20:28:53-04:00</updated>
  337. <id>http://efvincent.github.io/blog/2011/05/27/di-mock</id>
  338. <content type="html"><![CDATA[<p><a href="http://blog.efvincent.com/practical-di-101">My last post</a> provided a brief introduction into dependency injection. To review, the example included a data provider for Employee objects, which included a feature to return the object corresponding to the currently logged in user. In the end the following interfaces were defined:</p>
  339. <p><strong>IDataProvider</strong> the function is obvious from the name. One implementation, the DevDataProvider, uses a static List<employee> as a data store.</employee></p>
  340. <p><strong>IIdentityService</strong> describes a service that supplies the <em>current</em> identity. What current is depends on the implementation of course. A concrete WindowsIdentService defines current as the currently logged in Windows user. The TestIdentService implementation always returned the same username, which is useful for testing as we will see.</p>
  341. <p><strong>ILogService</strong> describes a simple logging service. The ConsoleLogService implementation prints logs to the console.</p>
  342. <!-- more -->
  343. <p>### Dependency Injection &amp;Testing</p>
  344. <p>For this post Ive added a standard MSTest project and a couple of tests for the data provider. The use of dependency injection patterns in the design of this simple example allows us to easily isolate the code under test.</p>
  345. <div><table class="CodeRay"><tr>
  346. <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
  347. <a href="#n2" name="n2">2</a>
  348. <a href="#n3" name="n3">3</a>
  349. <a href="#n4" name="n4">4</a>
  350. <a href="#n5" name="n5">5</a>
  351. <a href="#n6" name="n6">6</a>
  352. <a href="#n7" name="n7">7</a>
  353. <a href="#n8" name="n8">8</a>
  354. <a href="#n9" name="n9">9</a>
  355. <strong><a href="#n10" name="n10">10</a></strong>
  356. <a href="#n11" name="n11">11</a>
  357. <a href="#n12" name="n12">12</a>
  358. <a href="#n13" name="n13">13</a>
  359. <a href="#n14" name="n14">14</a>
  360. </pre></td>
  361. <td class="code"><pre><span class="directive">static</span> IContainer afContainer;
  362. [ClassInitialize]
  363. <span class="directive">public</span> <span class="directive">static</span> <span class="type">void</span> TestInit(TestContext ctx) {
  364. var idSvc = A.Fake&lt;IIdentService&gt;();
  365. A.CallTo(() =&gt; idSvc.GetCurrentUserName())
  366. .Returns(<span class="string"><span class="delimiter">&quot;</span><span class="content">FAKE-ID</span><span class="delimiter">&quot;</span></span>);
  367. var bldr = <span class="keyword">new</span> ContainerBuilder();
  368. bldr.RegisterInstance(idSvc);
  369. bldr.RegisterInstance(A.Fake&lt;ILogService&gt;());
  370. bldr.RegisterType&lt;DevDataProvider&gt;().As&lt;IDataProvider&gt;();
  371. afContainer = bldr.Build();
  372. }
  373. </pre></td>
  374. </tr></table>
  375. </div>
  376. <p>The test class has a static DI container instance, initialized in the class initializer. Im using <a href="http://code.google.com/p/fakeiteasy/">FakeItEasy</a> to create a fake IIdentService at line five. Like six tells the FakeItEasy framework what to return when the GetCurrentUserName() method is called on the fake ident service. Having a fixed response makes testing the data provider a piece of cake.</p>
  377. <p>I then register the fake ident service as well as a fake log service. For the log service, we dont need to specify any behavior for the methods. The FakeItEasy framework will effectively sink any calls to the methods of the fake log service, which is fine for this test.</p>
  378. <p>Lastly the data provider we want to test is registered with the DI container builder, and then container is built. The tests go on to use the DI container to resolve an instance of the data provider. The DI container will configure the data providers dependencies for a log service and an identity service with the fakes we built.</p>
  379. <div><table class="CodeRay"><tr>
  380. <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
  381. <a href="#n2" name="n2">2</a>
  382. <a href="#n3" name="n3">3</a>
  383. <a href="#n4" name="n4">4</a>
  384. <a href="#n5" name="n5">5</a>
  385. <a href="#n6" name="n6">6</a>
  386. <a href="#n7" name="n7">7</a>
  387. <a href="#n8" name="n8">8</a>
  388. </pre></td>
  389. <td class="code"><pre>[TestMethod()]
  390. <span class="directive">public</span> <span class="type">void</span> GetCurrentEmployeeTest() {
  391. var e = <span class="keyword">new</span> Employee { WindowsUsername = <span class="string"><span class="delimiter">&quot;</span><span class="content">FAKE-ID</span><span class="delimiter">&quot;</span></span> };
  392. var dal = afContainer.Resolve&lt;IDataProvider&gt;();
  393. dal.AddEmployee(e);
  394. var result = dal.GetCurrentEmployee();
  395. Assert.AreEqual(e.WindowsUsername, result.WindowsUsername);
  396. }
  397. </pre></td>
  398. </tr></table>
  399. </div>
  400. <p>This is just a small example of using a DI container in combination with a mock / fake framework for testing. The AutoFac DI container can handle much more complex scenarios than what weve thrown at it here. The same is true for the FakeItEasy component. Both of these components are well used, well maintained open source projects. You can find lots of documentation and examples for both. Or you can use any number of other DI containers and mocking frameworks to achieve equivalent results.</p>
  401. <p>The source code for the example is available <a href="https://bitbucket.org/efvincent/blog-post-dependency-injection-101">here</a>, and the blog entry the precedes this one is available <a href="http://blog.efvincent.com/practical-di-101/">here</a>.</p>
  402. ]]></content>
  403. </entry>
  404. <entry>
  405. <title type="html"><![CDATA[Practical Dependency Injection 101]]></title>
  406. <link href="http://efvincent.github.io/blog/2011/05/27/practical-di-101/"/>
  407. <updated>2011-05-27T13:08:27-04:00</updated>
  408. <id>http://efvincent.github.io/blog/2011/05/27/practical-di-101</id>
  409. <content type="html"><![CDATA[<p>In this post we take a look at dependency injection (DI). Target audience is competent .NET developers, C# specifically (but VBers who read C# can benefit just as much), whove heard of DI but havent gotten around to figuring out how it fits in their day to day.</p>
  410. <!-- more -->
  411. <p>### What is Dependency Injection</p>
  412. <p>The first question that we need to address is: What is it that DI does for us? What problem is being solved? DI is about coupling; the degree to which program unit refers to other units. In .NET the units were typically worried about are classes, interfaces, components, and assemblies. Dependency injection facilitates reduction these interdependencies. Are DI patterns a silver bullet? Of course not. You can always write bad code regardless of patterns. That being said, if youre already writing decent code and have good fundamentals, but are not using DI patterns, youve got the opportunity to take a leap forward.</p>
  413. <p>How does DI reduce help reduce coupling? The easiest way to describe it is by diving directly into an example.</p>
  414. <h3 id="example-scenario">Example Scenario</h3>
  415. <p>Well work on a hypothetical in-house app where the Windows AD authenticates employees, and their Windows username is used to index a database with Employee information. Its pretty common to see stuff like this happening in-house with line of business applications.</p>
  416. <p>The example uses a provider pattern all the data access will go through a data access provider, allowing us to build a simple provider that stores records in memory during this, our prototype phase. Theoretically wed replace this as development continued with a provider that leverages persistent storage later.</p>
  417. <p>Heres the base level example program with no consideration for dependency injection:</p>
  418. <div><table class="CodeRay"><tr>
  419. <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
  420. <a href="#n2" name="n2">2</a>
  421. <a href="#n3" name="n3">3</a>
  422. <a href="#n4" name="n4">4</a>
  423. <a href="#n5" name="n5">5</a>
  424. <a href="#n6" name="n6">6</a>
  425. <a href="#n7" name="n7">7</a>
  426. <a href="#n8" name="n8">8</a>
  427. <a href="#n9" name="n9">9</a>
  428. <strong><a href="#n10" name="n10">10</a></strong>
  429. <a href="#n11" name="n11">11</a>
  430. <a href="#n12" name="n12">12</a>
  431. <a href="#n13" name="n13">13</a>
  432. <a href="#n14" name="n14">14</a>
  433. <a href="#n15" name="n15">15</a>
  434. <a href="#n16" name="n16">16</a>
  435. <a href="#n17" name="n17">17</a>
  436. <a href="#n18" name="n18">18</a>
  437. <a href="#n19" name="n19">19</a>
  438. <strong><a href="#n20" name="n20">20</a></strong>
  439. <a href="#n21" name="n21">21</a>
  440. <a href="#n22" name="n22">22</a>
  441. <a href="#n23" name="n23">23</a>
  442. <a href="#n24" name="n24">24</a>
  443. <a href="#n25" name="n25">25</a>
  444. <a href="#n26" name="n26">26</a>
  445. <a href="#n27" name="n27">27</a>
  446. <a href="#n28" name="n28">28</a>
  447. <a href="#n29" name="n29">29</a>
  448. <strong><a href="#n30" name="n30">30</a></strong>
  449. <a href="#n31" name="n31">31</a>
  450. <a href="#n32" name="n32">32</a>
  451. <a href="#n33" name="n33">33</a>
  452. <a href="#n34" name="n34">34</a>
  453. <a href="#n35" name="n35">35</a>
  454. <a href="#n36" name="n36">36</a>
  455. <a href="#n37" name="n37">37</a>
  456. <a href="#n38" name="n38">38</a>
  457. <a href="#n39" name="n39">39</a>
  458. <strong><a href="#n40" name="n40">40</a></strong>
  459. <a href="#n41" name="n41">41</a>
  460. <a href="#n42" name="n42">42</a>
  461. <a href="#n43" name="n43">43</a>
  462. <a href="#n44" name="n44">44</a>
  463. <a href="#n45" name="n45">45</a>
  464. <a href="#n46" name="n46">46</a>
  465. <a href="#n47" name="n47">47</a>
  466. <a href="#n48" name="n48">48</a>
  467. <a href="#n49" name="n49">49</a>
  468. <strong><a href="#n50" name="n50">50</a></strong>
  469. <a href="#n51" name="n51">51</a>
  470. <a href="#n52" name="n52">52</a>
  471. <a href="#n53" name="n53">53</a>
  472. <a href="#n54" name="n54">54</a>
  473. <a href="#n55" name="n55">55</a>
  474. <a href="#n56" name="n56">56</a>
  475. <a href="#n57" name="n57">57</a>
  476. <a href="#n58" name="n58">58</a>
  477. <a href="#n59" name="n59">59</a>
  478. <strong><a href="#n60" name="n60">60</a></strong>
  479. <a href="#n61" name="n61">61</a>
  480. </pre></td>
  481. <td class="code"><pre><span class="type">class</span> <span class="class">Program</span> {
  482. <span class="directive">static</span> <span class="type">void</span> Main(string<span class="type">[]</span> args) {
  483. <span class="comment">// ** Without using an DI Container approach **</span>
  484. <span class="comment">// Create a new provider aka data access layer</span>
  485. var dal = <span class="keyword">new</span> DevDataProvider();
  486. <span class="comment">// New up an employee that's supposed to represent the currently logged in user</span>
  487. var e = <span class="keyword">new</span> Employee() {
  488. WindowsUsername = <span class="string"><span class="delimiter">&quot;</span><span class="content">thanos</span><span class="char">\\</span><span class="content">efvincent</span><span class="delimiter">&quot;</span></span>,
  489. EmployeeId = <span class="string"><span class="delimiter">&quot;</span><span class="content">0001</span><span class="delimiter">&quot;</span></span>,
  490. FName = <span class="string"><span class="delimiter">&quot;</span><span class="content">Eric</span><span class="delimiter">&quot;</span></span>,
  491. LName = <span class="string"><span class="delimiter">&quot;</span><span class="content">Vincent</span><span class="delimiter">&quot;</span></span>
  492. };
  493. <span class="comment">// Add it to the data access layer</span>
  494. dal.AddEmployee(e);
  495. <span class="comment">// See if the dal can find the current user</span>
  496. e = dal.GetCurrentEmployee();
  497. Console.WriteLine(
  498. <span class="string"><span class="delimiter">&quot;</span><span class="content">Current logged in person is: {0}</span><span class="delimiter">&quot;</span></span>, e == <span class="predefined-constant">null</span> ? <span class="string"><span class="delimiter">&quot;</span><span class="content">unknown</span><span class="delimiter">&quot;</span></span> : e.FName);
  499. <span class="comment">// End</span>
  500. Console.Write(<span class="string"><span class="delimiter">&quot;</span><span class="content">Press any key...</span><span class="delimiter">&quot;</span></span>);
  501. Console.ReadKey(<span class="predefined-constant">true</span>);
  502. }
  503. }
  504. <span class="directive">public</span> <span class="type">class</span> <span class="class">DevDataProvider</span> {
  505. <span class="directive">private</span> <span class="directive">static</span> readonly <span class="predefined-type">List</span>&lt;Employee&gt; EmployeeStore = <span class="keyword">new</span> <span class="predefined-type">List</span>&lt;Employee&gt;();
  506. <span class="directive">public</span> Employee GetCurrentEmployee() {
  507. var emp = EmployeeStore.FirstOrDefault(
  508. e =&gt; e.WindowsUsername.Equals(GetCurrentUserName(), StringComparison.OrdinalIgnoreCase));
  509. <span class="keyword">return</span> emp;
  510. }
  511. <span class="directive">public</span> <span class="type">void</span> AddEmployee(Employee e) {
  512. EmployeeStore.Add(e);
  513. }
  514. <span class="directive">public</span> IQueryable&lt;Employee&gt; Employees() {
  515. <span class="keyword">return</span> EmployeeStore.AsQueryable();
  516. }
  517. <span class="directive">private</span> <span class="directive">static</span> string GetCurrentUserName() {
  518. var wu = WindowsIdentity.GetCurrent();
  519. <span class="keyword">return</span> wu == <span class="predefined-constant">null</span> ? string.Empty : wu.Name;
  520. }
  521. }
  522. <span class="directive">public</span> <span class="type">class</span> <span class="class">Employee</span> {
  523. <span class="directive">public</span> string WindowsUsername { get; set; }
  524. <span class="directive">public</span> string EmployeeId { get; set; }
  525. <span class="directive">public</span> string FName { get; set; }
  526. <span class="directive">public</span> string LName { get; set; }
  527. }
  528. </pre></td>
  529. </tr></table>
  530. </div>
  531. <p>In Main() we new up the data access layer, create a new employee, and add it to our store using the data access layer. At line 21 we ask the data access layer to retrieve the employee record for the currently logged in user. Looks pretty typical, so how can IoC help? Lets look at the coupling here what classes are dependent on what other classes?</p>
  532. <p><a href="http://blog.efvincent.com/wp-content/uploads/2011/05/image.png"><img src="http://blog.efvincent.com/wp-content/uploads/2011/05/image_thumb.png" alt="image" /></a></p>
  533. <p>Our main program depends on the DevDataProvider class, and that depends on System.Security to find the Windows username of the currently logged in user. Asking the data access layer to determine the currently logged in user isnt the best idea, but this is blog post code created to check out dependency injection, so deal with that for the moment.</p>
  534. <p>Why are these dependencies undesirable? First consider how flexible this software is. Or rather, inflexible. We created a quick DevDataProvider that stores stuff in a static list. As we continue to build a system, wed have to refer to DevDataProvider from more and more classes, creating a brittle, tightly coupled system. Replacing DevDataProvider becomes more of a maintenance problem.</p>
  535. <p>Next think about testability. In real life there are unit tests (there should be anyway). One reason why people find excuses not to unit test is because their code is difficult to test. In this example, if we want to test DevDataProvider.GetCurrentEmployee() we have to consider that under the covers its calling the Windows API to get the current username. This makes that method harder to than it needs to be.</p>
  536. <h3 id="step-one--leveraging-interfaces">Step One Leveraging Interfaces</h3>
  537. <p>In this version, weve factored out an interface called IDataProvider, and one called IIdentService. The IDataProvider should be pretty obvious but IIdentService? The idea here is to decouple from the Windows API itself. A developer should understand <em>everywhere _that the application makes contact with _any</em> external modules, including the operating system, and then consider what the repercussions of that contact are. In this example, coupling to the Windows API to get then logged in username so directly is undesirable. We want to use a <em>service</em> that would supply us with credentials. That way if were testing, we can create a fake service that provides a predictable answer, and is therefore easier to test.</p>
  538. <p>Coding to an interface also allows us to radically change the behavior of the service without having to alter its dependencies. If we move to a ASP.NET environment for example, we wont want to use the current Windows Identity, we may want to use user information from the http context.</p>
  539. <div><table class="CodeRay"><tr>
  540. <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
  541. <a href="#n2" name="n2">2</a>
  542. <a href="#n3" name="n3">3</a>
  543. <a href="#n4" name="n4">4</a>
  544. <a href="#n5" name="n5">5</a>
  545. <a href="#n6" name="n6">6</a>
  546. <a href="#n7" name="n7">7</a>
  547. <a href="#n8" name="n8">8</a>
  548. <a href="#n9" name="n9">9</a>
  549. <strong><a href="#n10" name="n10">10</a></strong>
  550. <a href="#n11" name="n11">11</a>
  551. <a href="#n12" name="n12">12</a>
  552. <a href="#n13" name="n13">13</a>
  553. <a href="#n14" name="n14">14</a>
  554. <a href="#n15" name="n15">15</a>
  555. <a href="#n16" name="n16">16</a>
  556. <a href="#n17" name="n17">17</a>
  557. <a href="#n18" name="n18">18</a>
  558. <a href="#n19" name="n19">19</a>
  559. <strong><a href="#n20" name="n20">20</a></strong>
  560. <a href="#n21" name="n21">21</a>
  561. <a href="#n22" name="n22">22</a>
  562. <a href="#n23" name="n23">23</a>
  563. <a href="#n24" name="n24">24</a>
  564. <a href="#n25" name="n25">25</a>
  565. <a href="#n26" name="n26">26</a>
  566. <a href="#n27" name="n27">27</a>
  567. <a href="#n28" name="n28">28</a>
  568. <a href="#n29" name="n29">29</a>
  569. <strong><a href="#n30" name="n30">30</a></strong>
  570. <a href="#n31" name="n31">31</a>
  571. <a href="#n32" name="n32">32</a>
  572. <a href="#n33" name="n33">33</a>
  573. <a href="#n34" name="n34">34</a>
  574. <a href="#n35" name="n35">35</a>
  575. <a href="#n36" name="n36">36</a>
  576. <a href="#n37" name="n37">37</a>
  577. <a href="#n38" name="n38">38</a>
  578. <a href="#n39" name="n39">39</a>
  579. <strong><a href="#n40" name="n40">40</a></strong>
  580. <a href="#n41" name="n41">41</a>
  581. <a href="#n42" name="n42">42</a>
  582. <a href="#n43" name="n43">43</a>
  583. <a href="#n44" name="n44">44</a>
  584. <a href="#n45" name="n45">45</a>
  585. <a href="#n46" name="n46">46</a>
  586. <a href="#n47" name="n47">47</a>
  587. </pre></td>
  588. <td class="code"><pre><span class="comment">// Interface defining an identity service</span>
  589. <span class="directive">public</span> <span class="type">interface</span> <span class="class">IIdentService</span> {
  590. string GetCurrentUserName();
  591. }
  592. <span class="comment">// Implementation of an identity service that returns the current</span>
  593. <span class="comment">// logged in windows username</span>
  594. <span class="directive">public</span> <span class="type">class</span> <span class="class">WindowsIdentService</span> : IIdentService {
  595. <span class="directive">public</span> string GetCurrentUserName() {
  596. var wu = WindowsIdentity.GetCurrent();
  597. <span class="keyword">return</span> wu == <span class="predefined-constant">null</span> ? string.Empty : wu.Name;
  598. }
  599. }
  600. <span class="comment">// Interface defining a data provider service</span>
  601. <span class="directive">public</span> <span class="type">interface</span> <span clas