<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="https://imaun.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://imaun.github.io/" rel="alternate" type="text/html" /><updated>2020-01-23T14:05:20+00:00</updated><id>https://imaun.github.io/feed.xml</id><title type="html">Iman Nemati</title><subtitle>Personal Weblog</subtitle><author><name>Iman Nemati</name></author><entry><title type="html">Is .NET Core Built-in container enough for your DI?</title><link href="https://imaun.github.io/Microsoft-Dependency-Injection/" rel="alternate" type="text/html" title="Is .NET Core Built-in container enough for your DI?" /><published>2020-01-23T00:00:00+00:00</published><updated>2020-01-23T00:00:00+00:00</updated><id>https://imaun.github.io/Microsoft-Dependency-Injection</id><content type="html" xml:base="https://imaun.github.io/Microsoft-Dependency-Injection/">&lt;h3 id=&quot;is-net-core-built-in-container-enough-for-your-di&quot;&gt;Is .NET Core Built-in container enough for your DI?&lt;/h3&gt;
&lt;p&gt;One of the first choices you have to make when starting a .NET core project is whether to use the built-in DI container from Microsoft, or to use third-party DI containers for injecting and resolving dependencies in your project.
One of the advantage that the built-in Microsoft dependency injection will gives you, is that the .NET core framework libraries themselves register their dependencies using it. Actually the goal of the built-in container is to provide DI for the framework itself and to keep it simple as possible to integrate other third-party DI containers with it. For small projects o projects that doesn’t have much depended services to run, the built-in DI will do the work just fine, but if you need more advanced, convention based registration and injections, then you will need to look for third-party containers like &lt;a href=&quot;https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html&quot;&gt;AutoFac&lt;/a&gt; or &lt;a href=&quot;https://www.nuget.org/packages/Unity.Microsoft.DependencyInjection&quot;&gt;Unity&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But after all, here in this post we gonna talk about Microsoft.Extensions.DependencyInjection library that ships with default ASP.NET core projects, and in .NET core console apps, you need to install it via Nuget. Setting up the container in web projects is pretty straightforward. In Startup.cs file you’ll see a method ConfigureServices with a parameter named services (IServiceCollection). You can add your dependencies here, by adding interfaces and implementations classes to the services parameter.&lt;/p&gt;

&lt;div class=&quot;language-cs highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ConfigureServices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IServiceCollection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Add framework services.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Add application services.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AddTransient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEmailSender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AuthMessageSender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AddScoped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEmailSender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AuthMessageSender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AddSingleton&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDbService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DbService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The difference between Add methods are the lifetime. Depending on how the lifetime of an operation service is configured, the container provides the same or different instance of the service when requested by a class.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AddTransient()&lt;/code&gt;: a new instance is provided by the container, every time a controller or service requested it.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AddScoped()&lt;/code&gt;: a new instance created per client request, the operationId of the service is the same in and individual request.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AddSingleton()&lt;/code&gt;: a singleton instance created only once and used across all client requests and services.&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Iman Nemati</name></author><summary type="html">Is .NET Core Built-in container enough for your DI? One of the first choices you have to make when starting a .NET core project is whether to use the built-in DI container from Microsoft, or to use third-party DI containers for injecting and resolving dependencies in your project. One of the advantage that the built-in Microsoft dependency injection will gives you, is that the .NET core framework libraries themselves register their dependencies using it. Actually the goal of the built-in container is to provide DI for the framework itself and to keep it simple as possible to integrate other third-party DI containers with it. For small projects o projects that doesn’t have much depended services to run, the built-in DI will do the work just fine, but if you need more advanced, convention based registration and injections, then you will need to look for third-party containers like AutoFac or Unity.</summary></entry><entry><title type="html">Console.Write(“Hello World!”)</title><link href="https://imaun.github.io/hello-world/" rel="alternate" type="text/html" title="Console.Write(&quot;Hello World!&quot;)" /><published>2020-01-20T00:00:00+00:00</published><updated>2020-01-20T00:00:00+00:00</updated><id>https://imaun.github.io/hello-world</id><content type="html" xml:base="https://imaun.github.io/hello-world/">&lt;p&gt;This is my first post on this blog using jekyll hosted on &lt;a href=&quot;http://github.com&quot;&gt;Github&lt;/a&gt;. For a long time I wanted to write an English blog in my spare time, and that’s it, this is my blog.&lt;/p&gt;

&lt;p&gt;I’m going to post technical stuff related to software development specially about .NET Core, node.js, APIs and other Back-end related subjects.&lt;/p&gt;</content><author><name>Iman Nemati</name></author><summary type="html">This is my first post on this blog using jekyll hosted on Github. For a long time I wanted to write an English blog in my spare time, and that’s it, this is my blog.</summary></entry></feed>