{"id":26,"date":"2024-12-04T16:20:00","date_gmt":"2024-12-04T16:20:00","guid":{"rendered":"https:\/\/programming.rexthestrange.com\/?p=26"},"modified":"2024-12-04T16:20:00","modified_gmt":"2024-12-04T16:20:00","slug":"accessing-data-in-a-model-binder","status":"publish","type":"post","link":"https:\/\/programming.rexthestrange.com\/?p=26","title":{"rendered":"Accessing Data in a Model Binder"},"content":{"rendered":"\n<p>In <a href=\"https:\/\/programming.rexthestrange.com\/?p=24\">this post<\/a> I discussed creating a custom Model Binder. The next question I had was, &#8220;how can I access my database in that binder?&#8221; Before you spend hours beating your head against the wall, let me just say that it&#8217;s pretty easy.<\/p>\n\n\n\n<p>I&#8217;m going to assume that you have something like this in your program.cs file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>builder.Services.AddDbContext&lt;DataContext> (options => options.UseMySQL (builder.Configuration.GetConnectionString (\"MySqlConnection\")!));<\/code><\/pre>\n\n\n\n<p>This creates a Data Context which you can use to access your database. Your controller class may have something like this as a class parameter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SomeControllerClass (DataContext context) {\n\t...\n}\/\/ SomeControllerClass;<\/code><\/pre>\n\n\n\n<p>And then, in your API method you may have something that accesses that data context using, say, a linq (yes, that is a complete sentence &#8211; &#8220;linq&#8221; stands for &#8220;Language-Integrated Query&#8221;, so a &#8220;linq query&#8221; would be a &#8220;Language-Integrated Query query&#8221;, which is a tautology):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;SomeType> result = (from tbl in context.table select tbl).ToList ();<\/code><\/pre>\n\n\n\n<p>The method has access to the data context because of Dependency Injection. For those old school programmers like myself (who use the term &#8220;programmer&#8221; instead of &#8220;developer&#8221;), don&#8217;t freak out. Dependency Injection (or DI) is not some new voodoo. It&#8217;s merely a buzz-term for &#8220;passing a parameter to a method&#8221;. Whenever you see &#8220;Dependency Injection&#8221; or DI, you can substitute the concept, pass a parameter.<\/p>\n\n\n\n<p>Anyway, it works the same way. We&#8217;re passing (or rather, the .Net framework) is passing a parameter to the class. We can define our binding class like so:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CustomBinder (DataContext data_context): IModelBinder {\n\t...\n}\/\/ CustomBinder;<\/code><\/pre>\n\n\n\n<p>And, bingo, we have access to the data context. Note that we&#8217;re declaring the parameter on the class. This is a new feature of .Net, and it&#8217;s pretty cool. We could just as easily have done this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CustomBinder: IModelBinder {\n\tprivate DataContext context;\n\n\tpublic CustomBinder (DataContext context) {\n\t\tthis.context = context\n\t}\/\/ Constructor;\n}\/\/ CustomBinder;<\/code><\/pre>\n\n\n\n<p>That would have exactly the same effect, but the new syntax allows us to dispense with declaring the private variable and constructor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I discussed creating a custom Model Binder. The next question I had was, &#8220;how can I access my database in that binder?&#8221; Before you spend hours beating your head against the wall, let me just say that it&#8217;s pretty easy. I&#8217;m going to assume that you have something like this in your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-c-net"],"_links":{"self":[{"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26"}],"version-history":[{"count":1,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":27,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions\/27"}],"wp:attachment":[{"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programming.rexthestrange.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}