<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joe Lowrance</title>
    <description>The latest articles on DEV Community by Joe Lowrance (@joelowrance).</description>
    <link>https://dev.to/joelowrance</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F119846%2Ffb88fcec-8041-4e75-b85c-b7e981eefc33.png</url>
      <title>DEV Community: Joe Lowrance</title>
      <link>https://dev.to/joelowrance</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joelowrance"/>
    <language>en</language>
    <item>
      <title>Dependency Injection</title>
      <dc:creator>Joe Lowrance</dc:creator>
      <pubDate>Thu, 09 Jan 2020 11:30:00 +0000</pubDate>
      <link>https://dev.to/joelowrance/dependency-injection-181i</link>
      <guid>https://dev.to/joelowrance/dependency-injection-181i</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Q: What is dependency injection?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Dependency injection is providing the dependencies to a class instead of having that class be responsible for creating it’s own instances&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Service 
{
   private DependencyOne _dependency1;
   private DependencyTwo _dependency2;

   public Service() 
   {
    _dependency1 = new DependencyOne(new Logger(), new DatabaseClass())
    _dependency2 = new DependencyTwo();
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With Dependency Injection this becomes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Service 
{
   private DependencyOne _dependency1;
   private DependencyTwo _dependency2;

   public Service(DependencyOne dependency1, Dependency2 dependency2) 
   {
    _dependency1 = dependency1;
    _dependency2 = dependency2;
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can see the first benefit immediately: If the constructor of &lt;code&gt;DependencyOne&lt;/code&gt; changes to require additional child dependencies, we will not need to change our &lt;code&gt;Service&lt;/code&gt; class like we would in the first example.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: OK, but it looks like all I did was bump my “new” statement up a level. Now instead of &lt;code&gt;new Service()&lt;/code&gt;, I need to create &lt;code&gt;new Service(new DependencyOne(), new DependencyTwo()&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At some point you are going to have what’s known as a “composition root” where you need to make the decisions of how new instances are created. To help with this, we can use dependency injection frameworks such as Lamar, StructureMap or AutoFac. These frameworks are known as “containers” and they are responsible for creating the “dependency graph” (there are a lot of terms associated with this concept, any they will start to make sense as you get more comfortable with it).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: Why do I want to do this?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is a design guideline known as Dependency Inversion that states that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is part of a fairly well know set of principles known as &lt;a href="%5Bhttps://en.wikipedia.org/wiki/SOLID%5D(https://en.wikipedia.org/wiki/SOLID)"&gt;SOLID&lt;/a&gt; and Dependency Injection is a way of implementing Dependency Inversion.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: That’s very vague.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider our second example above. We are using Dependency Injection but not Dependency Inversion. Dependency Inversion would look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Service 
{
   private IDependencyOne _dependency1;
   private IDependencyTwo _dependency2;

   public Service(IDependencyOne dependency1, IDependencyTwo dependency2) 
   {
    _dependency1 = dependency1;
    _dependency2 = dependency2;
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Where we are now passing in Interfaces instead of implementations. We have “inverted” the dependencies and our service class now depends on abstractions (the Interfaces) instead of concrete implementations. The &lt;code&gt;Service&lt;/code&gt; class is now only very loosely coupled to it’s dependencies. The creator of the &lt;code&gt;Service&lt;/code&gt; class is now responsible for providing those dependencies.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: That kind of makes sense, but what are the benefits?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The primary benefit is loose coupling. It is much cleaner to depend on an &lt;code&gt;IWeatherService&lt;/code&gt; that defines what a weather service should do than on a concrete implementation like &lt;code&gt;SimpleWeatherService&lt;/code&gt; or &lt;code&gt;WeatherChannelWeatherService&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
When we only care about the abstraction, we can easily swap in any implementation of &lt;code&gt;IWeatherService&lt;/code&gt; without having to make any changes to the class consuming this.&lt;/p&gt;

&lt;p&gt;Additionally, there are some other benefits such as testable code. Dependency Inversion allows you to break apart complex problems into small isolated units that can be tested individually.&lt;/p&gt;

&lt;p&gt;You also end up with a substantial reduction in “new”. When using a container, object initialization is taken care of for you. Without a container, If you had &lt;code&gt;SimpleWeatherService(Logger)&lt;/code&gt; and you added &lt;code&gt;SimpleWeatherService(Logger, Location)&lt;/code&gt; you would need to update every usage. When the container takes care of this for you, you won’t find yourself updating multiple files across your application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: When would I want to use this kind of approach vs sticking to what I’ve been doing?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Generally, you’d switch to this approach when you start feeling any type of discomfort from managing dependencies. If you have a small application, there’s hardly every a good case for adding additional complexity, but if that application grows and you want to take advantage of the benefits listed above it might be time to switch.&lt;/p&gt;

&lt;p&gt;Additionally, if you are doing any kind of test driven development you will probably start with this approach.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: You mentioned something about frameworks earlier. Do I need one? And how do I pick one?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Good news: You don’t need one&lt;/p&gt;

&lt;p&gt;Bad news: You should probably use one.&lt;/p&gt;

&lt;p&gt;You can definitely do all of this manually, but given the sheer variety of things that an IoC container can do and the number of things they simplify it’s probably better to use something that’s been tested and used in multiple differing production environments by other people. For example - how should lifecycles be managed? Do you want a single instances per HTTP request? Or a new instance for every class that needs one? Or do you just want a single instance to be created for the entire application?&lt;/p&gt;

&lt;p&gt;As for how to pick a container: Microsoft provides one with .net core that will work with web or console applications. It’s a solid choice, but it does lack some of the more advanced features that other containers provide. However, you may never need these features so it’s probably better to start simple. The “wiring up” of these containers is generally done during application start up, so switching one out for another should usually be a relatively quick task.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: There is a lot of jargon and acronyms.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is, and a lot of the &lt;a href="https://stackoverflow.com/a/6551303/190592"&gt;terms&lt;/a&gt; being used around this concept are interchangeable&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: This sounds interesting. Can you show me some examples?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://github.com/joelowrance/DIDemo"&gt;Yes.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dependencyinjection</category>
      <category>beginners</category>
    </item>
    <item>
      <title>SQL Server Row Versions</title>
      <dc:creator>Joe Lowrance</dc:creator>
      <pubDate>Sat, 15 Jun 2019 11:30:00 +0000</pubDate>
      <link>https://dev.to/joelowrance/sql-server-row-versions-4bef</link>
      <guid>https://dev.to/joelowrance/sql-server-row-versions-4bef</guid>
      <description>&lt;p&gt;The &lt;a href="https://docs.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql?view=sql-server-2017"&gt;&lt;code&gt;rowversion&lt;/code&gt;&lt;/a&gt; data type is simple to implement, but provides some powerful functionality.&lt;/p&gt;

&lt;p&gt;Consider the following, where we create a pair of simple tables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE ExampleTable2 (PriKey int PRIMARY KEY, Other uniqueidentifier, VerCol rowversion) ;  
GO
insert into ExampleTable2(PriKey, Other)
values 
(1, newid()),
(2, newid()),
(3, newid()),
(4, newid()),
(5, newid()),
(6, newid()),
(7, newid()),
(8, newid()),
(9, newid()),
(10, newid())
GO
CREATE TABLE ExampleTable3 (PriKey int PRIMARY KEY, Other uniqueidentifier, VerCol rowversion) ;  
GO
insert into ExampleTable3(PriKey, Other)
values 
(11, newid()),
(12, newid()),
(13, newid()),
(14, newid()),
(15, newid()),
(16, newid()),
(17, newid()),
(18, newid()),
(19, newid()),
(20, newid())
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we can see, the &lt;code&gt;VerCol&lt;/code&gt; column is populated with sequential data. Note that the value is &lt;strong&gt;sequential across the database, not the table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mr9FoF3t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://testedwithf5.github.io/assets/images/rowversion_1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mr9FoF3t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://testedwithf5.github.io/assets/images/rowversion_1.PNG" alt="initial values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we update a row&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;update ExampleTable2 set Other = NEWID() where PriKey = 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can see that the &lt;code&gt;VerCol&lt;/code&gt; was automatically updated to the next number in the sequence.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X17BG1UE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://testedwithf5.github.io/assets/images/rowversion_2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X17BG1UE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://testedwithf5.github.io/assets/images/rowversion_2.png" alt="initial values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The usefulness is immediateley obvious in two scenarios that come to mind. The first would be a scenario where a user is editing a record that another user is also editing. If User A saves the record, the &lt;code&gt;VerCol&lt;/code&gt; will be updated. When User B goes to save the record, we could check the value of this column against the record User B is working with and prevent the save operation with a warning that the data is out of date.&lt;/p&gt;

&lt;p&gt;The second scenario is around batch jobs and/or data exports. If CustomerA and CustomerB occasionally ask for exports of data that have changed since they last asked, we can provide the data and the &lt;code&gt;Max(VerCol)&lt;/code&gt; at the time the export happens. When they next ask for a new set of data, we can simply add criteria stating &lt;code&gt;where VerCol &amp;gt; 0x00000 00000000824&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>sqlserver</category>
    </item>
  </channel>
</rss>
