<?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>Forem: jacob</title>
    <description>The latest articles on Forem by jacob (@djeikyb).</description>
    <link>https://forem.com/djeikyb</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%2F326748%2F2f0eb38a-25dd-4c7d-a3b9-c433b700c992.jpeg</url>
      <title>Forem: jacob</title>
      <link>https://forem.com/djeikyb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/djeikyb"/>
    <language>en</language>
    <item>
      <title>Make your own docker command!</title>
      <dc:creator>jacob</dc:creator>
      <pubDate>Thu, 05 Mar 2020 20:40:49 +0000</pubDate>
      <link>https://forem.com/djeikyb/make-your-own-docker-command-59k9</link>
      <guid>https://forem.com/djeikyb/make-your-own-docker-command-59k9</guid>
      <description>&lt;p&gt;I saw this post about a custom cli app to make it just a little nicer to quickly shell in to a local docker container:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/pobb" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5SFsDkWr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--QmIugvef--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/345644/852a39d5-8a57-45d7-8d53-f3f6eefbc7e1.jpg" alt="pobb image"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/pobb/docket-connect-to-your-docker-s-container-easily-10gc" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Docket : connect to your docker's container easily !&lt;/h2&gt;
      &lt;h3&gt;pob ・ Mar  5 ・ 2 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#docker&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#container&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#bash&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#ruby&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;And I thought, what if instead of a brand new command, it was part of the docker cli? It's not well advertised, but you can! So I installed it as a custom docker command. &lt;a href="https://docs.docker.com/engine/extend/cli_plugins/"&gt;Official docs here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are a couple rules.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Where does the script go? I wanted to run &lt;code&gt;docker goto&lt;/code&gt;. The easiest choice for me was &lt;code&gt;~/.docker/cli-plugins/docker-goto&lt;/code&gt;. Whatever subcommand name you want, just prefix it with "docker-"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You must have a subcommand called "docker-cli-plugin-metadata". When I run &lt;code&gt;docker goto docker-cli-plugin-metadata&lt;/code&gt;, it has to output specific json. This block is enough:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"docker-cli-plugin-metadata"&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;HERE&lt;/span&gt;&lt;span class="sh"&gt;
{
  "SchemaVersion": "0.1.0",
  "Vendor": "example.com",
}
&lt;/span&gt;&lt;span class="no"&gt;HERE
&lt;/span&gt;&lt;span class="nb"&gt;exit
&lt;/span&gt;&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make it executable: &lt;code&gt;chmod +x ~/.docker/cli-plugins/docker-goto&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now you can make it do whatever you want! &lt;a href="https://gist.github.com/djeikyb/b88bf10acba88797f72eabe9c7c414fb"&gt;Full code in this gist.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>bash</category>
    </item>
    <item>
      <title>Configure entity framework with dependency injection</title>
      <dc:creator>jacob</dc:creator>
      <pubDate>Mon, 03 Feb 2020 23:25:41 +0000</pubDate>
      <link>https://forem.com/djeikyb/configure-entity-framework-with-dependency-injection-2726</link>
      <guid>https://forem.com/djeikyb/configure-entity-framework-with-dependency-injection-2726</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You're following the configuration options pattern. To configure an entity framework context, you need to inject an IOptions container. But during ConfigureServices, the dependency injection container isn't ready.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is this useful to me?
&lt;/h2&gt;

&lt;p&gt;Should be relevant to at least asp core 2.2 and 3.x.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Entity framework lets you provide a context factory. The factory is defined during ConfigureServices, but it's only used after. Use the overload with a callback that includes an IServiceProvider. The IServiceProvider gives access to the DI container, so you can access any IOptions.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void ConfigureServices(IServiceCollection services)
{
  services.Configure&amp;lt;DatabaseSettings&amp;gt;(o =&amp;gt;
  {
    o.Database = "some db";
    o.Username = "some user";
    o.Password = "some password";
  });

  services.AddDbContextPool&amp;lt;MyDbContext&amp;gt;((IServiceProvider sp, DbContextOptionsBuilder builder) =&amp;gt;
  {
    var dbsettings = sp.GetRequiredService&amp;lt;IOptions&amp;lt;DatabaseSettings&amp;gt;&amp;gt;();
    var env = sp.GetRequiredService&amp;lt;IWebHostEnvironment&amp;gt;();
    builder.UseNpgsql(dbsettings.Value.GetConnectionString());
    builder.EnableSensitiveDataLogging(env.IsDevelopment());
  });
}

public class DatabaseSettings
{
  public string Database { get; set; }
  public string Username { get; set; }
  public string Password { get; set; }
  public string GetConnectionString() =&amp;gt; new NpgsqlConnectionStringBuilder
  {
    // ...
  }.ConnectionString;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For this to work, your context must have a single constructor. That constructor must accept a DbContextOptions object:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MyDbContext : DbContext
{
  // ...

  public MyDbContext(DbContextOptions options) : base(options) { }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can override OnConfiguring if you like, the docs say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If both are used, OnConfiguring is applied last and can overwrite options supplied to the constructor argument.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Can I use IConfigureOptions?
&lt;/h2&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Lots of things need to be configured inside ConfigureServices, but also need IOptions. Entity framework uses a factory callback pattern. Another pattern asp core uses is IConfigureOptions. Ef core 3.1 does not use the IConfigureOptions pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext#using-dbcontext-with-dependency-injection"&gt;https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext#using-dbcontext-with-dependency-injection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1"&gt;https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1"&gt;https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>dotnet</category>
      <category>entityframework</category>
      <category>aspcore</category>
    </item>
  </channel>
</rss>
