<?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: Scăueru Cristian-Ștefăniță</title>
    <description>The latest articles on DEV Community by Scăueru Cristian-Ștefăniță (@cristianscaueru).</description>
    <link>https://dev.to/cristianscaueru</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%2F39302%2Fe2cf16af-fa17-41f5-911e-8b867f79710e.jpeg</url>
      <title>DEV Community: Scăueru Cristian-Ștefăniță</title>
      <link>https://dev.to/cristianscaueru</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cristianscaueru"/>
    <language>en</language>
    <item>
      <title>A new small opensource library - Autojector.</title>
      <dc:creator>Scăueru Cristian-Ștefăniță</dc:creator>
      <pubDate>Mon, 14 Mar 2022 07:50:40 +0000</pubDate>
      <link>https://dev.to/cristianscaueru/a-new-small-opensource-library-autojector-3584</link>
      <guid>https://dev.to/cristianscaueru/a-new-small-opensource-library-autojector-3584</guid>
      <description>&lt;p&gt;In the past couple of weeks, I’ve been working on a small library that (I hope) will make the dependency injection from Microsoft easier to use.&lt;/p&gt;

&lt;p&gt;The purpose of this library would be to declare in the definition of the class (service) the intended life span. &lt;/p&gt;

&lt;p&gt;Either by implementing an interface&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ISimpleInjectedService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleInjectedService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; 
        &lt;span class="n"&gt;ISimpleInjectedService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;ITransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ISimpleInjectedService&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"SomeData"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or by adding an attribute to the service stating which interface represents&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ISimpleInjectedByAttribute&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Transient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ISimpleInjectedByAttribute&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleInjectedByAttribute&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ISimpleInjectedByAttribute&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"SimpleInjectedByAttribute"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, you can just add this to your startup&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddAutojector&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are all the steps in order for your ISimpleInjectedService to be injected anywhere by the dependency injection system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;MapGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/simple-injected-service"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ISimpleInjectedService&lt;/span&gt; &lt;span class="n"&gt;simpleInjectedService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;simpleInjectedService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the &lt;a href="https://automaticinjector.com/simple-injection"&gt;Simple injection&lt;/a&gt; is the simpler way you can use the Autoinjector you can also use :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;a href="https://automaticinjector.com/factories"&gt;Factory&lt;/a&gt; to provide your service in a more elegant way
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IFactoryInjectedService1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Factory1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ITransientFactory&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IFactoryInjectedService1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IFactoryInjectedService1&lt;/span&gt; &lt;span class="nf"&gt;GetService&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FactoryInjectedService1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;An &lt;a href="https://automaticinjector.com/async-factories"&gt;Async factory&lt;/a&gt; to provide something that could take longer&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://automaticinjector.com/decorators"&gt;Decorators&lt;/a&gt; where you can implement the same interface multiple times and keep the previous implementation with composition&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://automaticinjector.com/configs"&gt;Configs&lt;/a&gt; which can be used to declare classes or even interfaces as configs &lt;/li&gt;
&lt;li&gt;And &lt;a href="https://automaticinjector.com/chains"&gt;Chains&lt;/a&gt; the most complex feature that still needs direction. With this feature, I would like to implement the chains of responsibility.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here are the benefits that I think this library would bring:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Would make it easier to see the lifetime of the service right from the implementation&lt;/li&gt;
&lt;li&gt;Would make you get rid of the file where you have to add all the services from your class libraries&lt;/li&gt;
&lt;li&gt;Would make it easier to come up in the future with abstraction over which DI library you are going to use&lt;/li&gt;
&lt;li&gt;All the other features would make it simpler to inject a lot more elegant and structured&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a list of cons that I see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You get rid of DI library dependency but you will be dependent on Autojector.Abstraction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can check everything for this library here on the &lt;a href="https://automaticinjector.com/"&gt;website,&lt;/a&gt; &lt;a href="https://github.com/Net-splash/Autojector"&gt;GitHub&lt;/a&gt; or &lt;a href="https://www.nuget.org/packages?q=Autojector+"&gt;Nuget&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Would be nice to hear your opinion in regards to this so: If you have any input please leave it anywhere reachable by me. &lt;/p&gt;

&lt;p&gt;It would be nice if you could say :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why would you use it? Why would you not use something like this?&lt;/li&gt;
&lt;li&gt;What more needs to be added in order to be needed?&lt;/li&gt;
&lt;li&gt;Other prons? Other cons?&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>library</category>
    </item>
  </channel>
</rss>
