<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://shazwazza.com/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Shazwazza</title>
    <link>https://shazwazza.com/</link>
    <description>My blog which is pretty much just all about coding</description>
    <generator>Articulate, blogging built on Umbraco</generator>
    <image>
      <url>/media/0libq25y/frog.png?rmode=max&amp;v=1da0e911f4e6890</url>
      <title>Shazwazza</title>
      <link>https://shazwazza.com/</link>
    </image>
    <item>
      <guid isPermaLink="false">1263</guid>
      <link>https://shazwazza.com/post/using-linqpad-with-umbraco/</link>
      <category>Umbraco</category>
      <title>Using LinqPad with Umbraco</title>
      <description>&lt;p&gt;During some spare time last weekend I decided to look into booting up Umbraco outside of a web context. I know this has been done before in various different ways but for this I wanted to just try to use the Umbraco core. There are 2 things that are required to boot Umbraco:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;IBootManager&lt;/li&gt;
&lt;li&gt;UmbracoApplicationBase&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In Umbraco source, there are a couple of these instances: &lt;em&gt;CoreBootManager&lt;/em&gt;, &lt;em&gt;WebBootManager&lt;/em&gt; &amp;amp; &lt;em&gt;UmbracoApplication&lt;/em&gt;.  Essentially the &lt;em&gt;WebBootManager&lt;/em&gt; starts up anything that has web requirements and inherits from &lt;em&gt;CoreBootManager&lt;/em&gt; which starts up the rest of Umbraco and all of that is bootstrapped with &lt;em&gt;UmbracoApplication&lt;/em&gt;, which is actually the instance of the global.asax in an Umbraco website. To boot just the core of Umbraco it would seem clear that I just want &lt;em&gt;CoreBootManager&lt;/em&gt; and then bootstrap it myself with a custom &lt;em&gt;UmbracoApplicationBase&lt;/em&gt;. All of this is actually very simple and the bare bones would look something like:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ConsoleApplication : UmbracoApplicationBase
{  
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; StartApplication()
    {
        GetBootManager()
            .Initialize()
            .Startup(appContext =&amp;gt; OnApplicationStarting(&lt;span class="kwrd"&gt;this&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; EventArgs()))
            .Complete(appContext =&amp;gt; OnApplicationStarted(&lt;span class="kwrd"&gt;this&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; EventArgs()));
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; IBootManager GetBootManager()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ConsoleBootManager();
    }
}&lt;/pre&gt;
&lt;p&gt;Then to use it could look something like:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main()
{
    &lt;span class="kwrd"&gt;using&lt;/span&gt; (var app = &lt;span class="kwrd"&gt;new&lt;/span&gt; ConsoleApplication())
    {
        app.StartApplication();
        &lt;span class="kwrd"&gt;using&lt;/span&gt; (var appCtx = ApplicationContext.Current)
        {
            &lt;span class="rem"&gt;//do stuff&lt;/span&gt;
        }
    }
}&lt;/pre&gt;
&lt;p&gt;Let’s just say there are still some issues with this that we’ll iron out in the future. Almost all of the issues have to do with loading in plugins and plugins not being capable of running without web context, and various other things. The good news is that I got it working with only a tiny bit of reflection which basically just removes all &lt;em&gt;IApplicationEventHandlers&lt;/em&gt; from starting up apart from the ones found in the Umbraco Core assembly.&lt;/p&gt;
&lt;h2&gt;LinqPad &amp;amp; Umbraco is real!&lt;/h2&gt;
&lt;p&gt;Knowing that I could boot Umbraco and interact with it’s data via the ApplicationContext, I figured the next best thing would be to see if I could get that working with LinqPad… and I did !  Awesome!! :)&lt;/p&gt;
&lt;p&gt;So here you go, a shiny new v1.0 LinqPad driver for Umbraco and all of the source code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Source &amp;amp; Documentation: &lt;a href="https://github.com/Shazwazza/UmbracoLinqPadDriver" title="https://github.com/Shazwazza/UmbracoLinqPadDriver"&gt;https://github.com/Shazwazza/UmbracoLinqPadDriver&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Download:  &lt;a href="https://github.com/Shazwazza/UmbracoLinqPadDriver/releases/tag/v1.0.0" title="https://github.com/Shazwazza/UmbracoLinqPadDriver/releases/tag/v1.0.0"&gt;https://github.com/Shazwazza/UmbracoLinqPadDriver/releases/tag/v1.0.0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Be sure to read the docs, there’s probably a lot of stuff it can’t do, but there’s certainly a lot of stuff it could do!!! The next version will hopefully have some proper IQueryable support which I’ve been working on as well.&lt;/p&gt;
&lt;p&gt;See it in action:&lt;/p&gt;
&lt;div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:e259a83e-27ad-4463-9824-1c5c0bdea97d" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;"&gt;
&lt;div&gt;&lt;object width="448" height="252"&gt;&lt;param name="movie" value="https://www.youtube.com/v/ypPvOQY8sF0?hl=en&amp;amp;hd=1" /&gt;&lt;embed type="application/x-shockwave-flash" width="448" height="252" src="https://www.youtube.com/v/ypPvOQY8sF0?hl=en&amp;amp;hd=1" /&gt;&lt;/object&gt;&lt;/div&gt;
&lt;div style="width: 448px; clear: both; font-size: .8em;"&gt;Umbraco LinqPad driver&lt;/div&gt;
&lt;/div&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:15 Z</pubDate>
      <a10:updated>2023-03-23T15:08:15Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1172</guid>
      <link>https://shazwazza.com/post/linq2umbraco-driver-for-linqpad/</link>
      <category>Umbraco</category>
      <title>Linq2Umbraco driver for LINQPad</title>
      <description>&lt;div class="imported-post"&gt;This post was imported from FARMCode.org which has been discontinued. These posts now exist here as an archive. They may contain broken links and images.&lt;/div&gt;Having used Linq2Umbraco for a while now to retrieve &lt;a href="http://www.umbraco.com" target="_blank"&gt;Umbraco&lt;/a&gt; data in custom DALs I really needed a way to build queries and execute them against the &lt;a href="http://www.umbraco.com" target="_blank"&gt;Umbraco&lt;/a&gt; web site’s data without each time compiling the application and maybe even firing up the debugger. Enter &lt;a href="http://www.linqpad.net/" target="_blank"&gt;LINQPad&lt;/a&gt; by by &lt;a href="http://www.albahari.com/" target="_blank"&gt;Joseph Albahari&lt;/a&gt;, a fantastic program which has pretty much replaced SQL Server Management Studio on my workstation for querying databases. Although you can query the Umbraco database of your web site LINQPad can’t natively operate on an UmbracoDataContext, which is what Linq2Umbraco is using when you export the &lt;a href="http://www.umbraco.com/" target="_blank"&gt;Umbraco&lt;/a&gt; document types to .Net and which you will be using when querying the data in the DAL. So it would be really helpful if you can use the UmbracoDataContext directly in LINQPad as you can use these queries 1:1 in your code. Thankfully LINQPad can be extended to work with custom DataContexts, all that needs to be done is to write a custom data context driver as described &lt;a href="http://www.linqpad.net/Extensibility.aspx" target="_blank"&gt;here&lt;/a&gt;. The documentation is exceptionally good and after a short while the first Linq2Umraco driver for LINQPad was ready and is now free to download:  &lt;p&gt;&lt;a href="http://farmcode.org/file.axd?file=2011%2f2%2fTheFarm+Linq2UmbracoDataContextDriver+1.0.lpx"&gt;Download TheFarm Linq2Umbraco Driver for LINQPad 1.0&lt;/a&gt; (16.08kb)&lt;/p&gt; &lt;p&gt;The driver has been compiled against LINQPad 2.31 using .Net 3.5, so it should work fine on both current versions of LINQPad (.Net 3.5 and 4). The umbraco.Linq.Core.dll, which the driver needs as it contains the UmbracoDataContext, will be dynamically loaded. The driver assumes that it exists in the same directory as the dll which contains the custom Linq2Umbraco DataContext. The dynamic loading has 2 advantages:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;The size of the driver is only 16kb! &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="/image.axd?picture=wlEmoticon-smile_2.png"&gt; No need to pack all the dlls in there as they are all present in your &lt;a href="http://www.umbraco.com" target="_blank"&gt;Umbraco&lt;/a&gt; installation anyway. And this way the driver isn’t bound to one specific &lt;a href="http://www.umbraco.org" target="_blank"&gt;Umbraco&lt;/a&gt; version.  &lt;li&gt;LINQPad requires it’s driver assemblies to be strongly signed, and .Net requires every referenced assembly to be strongly signed as well. However umbraco.Linq.Core is not strongly signed, and although I managed to strongly sign the assembly by using a little tool it did create havoc to the Umbraco installation, so dynamically loading the assembly magically resolves all that headache! &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Following is a quick demonstration of the driver in use. I’ve created a small test &lt;a href="http://www.umbraco.com" target="_blank"&gt;Umbraco&lt;/a&gt; installation with a couple of document types and content nodes:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_doctypes.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_doctypes" border="0" alt="demo_doctypes" src="/image.axd?picture=demo_doctypes_thumb.png" width="218" height="146"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_content.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_content" border="0" alt="demo_content" src="/image.axd?picture=demo_content_thumb.png" width="183" height="248"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;The doc types contain just a couple of standard fields like text string, data,&amp;nbsp; plus one Ultimate picker instance on the Products page (checkbox list, saved as comma delimited string). Now I’ll export the document types to .Net by right clicking on ‘Document Types’, the popup window will be populated like this:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_export.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_export" border="0" alt="demo_export" src="/image.axd?picture=demo_export_thumb.png" width="414" height="365"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;It doesn’t matter if you choose POCO with or without abstractions. It is also very advisable to install &lt;a href="http://our.umbraco.org/member/5518" target="_blank"&gt;Matt Brailford’s&lt;/a&gt; fantastic &lt;a href="http://our.umbraco.org/projects/developer-tools/autoexport2dotnet" target="_blank"&gt;AutoExport2DotNet&lt;/a&gt; package, it will perform an export to .Net every time you modify the document types.&lt;/p&gt; &lt;p&gt;After downloading and renaming the UmbracoDataContext files I’ll include them in the custom DAL like so:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_solution_explorer.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_solution_explorer" border="0" alt="demo_solution_explorer" src="/image.axd?picture=demo_solution_explorer_thumb.png" width="270" height="514"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;As you can see at the moment the DAL project doesn’t contain anything else but the two automatically generated files plus the necessary link to umbraco.Linq.Core. The Umbraco 4.6.1 test installation is showing up right next to it, I need the /App_Data/umbraco.config file from it later.&lt;/p&gt; &lt;p&gt;Now let’s start up LINQPad and add a connection using the custom driver:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_add_connecton.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_add_connecton" border="0" alt="demo_add_connecton" src="/image.axd?picture=demo_add_connecton_thumb.png" width="620" height="395"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Clicking on View more drivers… will reveal the following dialog, where Browse… let’s you select the custom LINQPad driver:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_add_driver.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_add_driver" border="0" alt="demo_add_driver" src="/image.axd?picture=demo_add_driver_thumb.png" width="620" height="480"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Opening the driver will shortly lead to the message ‘Driver successfully loaded’, the driver will now appear in the list of drivers from above:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_driver.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_driver" border="0" alt="demo_driver" src="/image.axd?picture=demo_driver_thumb.png" width="630" height="169"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;After hitting ‘Next’ the following dialog will appear:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_dialog.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_dialog" border="0" alt="demo_dialog" src="/image.axd?picture=demo_dialog_thumb.png" width="340" height="268"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Here the properties for the Linq2Umbraco connection are set.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Path to custom assembly: This is the assembly that contains the exported UmbracoDataContext, in this case it’s TheFarm.DAL.dll  &lt;li&gt;Full name of custom type: A popup window will present you a list of all UmbracoDataContexts in the assembly, in 99% of the cases there will only be one and in this case it’s the TheFarmDemoDataContext as specified above  &lt;li&gt;/Appdata/umbraco.config: The umbraco.config file of the Umbraco installation found in /App_Data.  &lt;li&gt;Surpress lazy secondary queries: UmbracoDataContext uses the AssociationTree class to list children, e.g. the class Products will contain a list of ProductCategories. When querying Products you will most likely not want to generate a list of all ProductCategories for each Product, Surpress lazy secondary queries will tell the program to stop evaluating these expressions as well. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;After hitting OK the connection will be added to LINQPad and you can start querying the custom UmbracoDataContext:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_query1.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_query1" border="0" alt="demo_query1" src="/image.axd?picture=demo_query1_thumb.png" width="620" height="290"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Notice here the output of TopProducts (Ultimate picker), Introduction (Richtext editor) and PricesValidUntil (date). ProductCategorys and Productss are the above mentioned children and grand children which are here surpressed with ‘Surpress lazy secondary queries’. Another example:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_query2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_query2" border="0" alt="demo_query2" src="/image.axd?picture=demo_query2_thumb.png" width="620" height="374"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;You can also easily create custom Linq Extension methods e.g. in the DAL and use them in your queries like so:&lt;/p&gt; &lt;p&gt;&lt;a href="/image.axd?picture=demo_query3.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="demo_query3" border="0" alt="demo_query3" src="/image.axd?picture=demo_query3_thumb.png" width="215" height="145"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;[Credits for the &lt;a href="http://www.extensionmethod.net/Details.aspx?ID=250" target="_blank"&gt;ToCSV&lt;/a&gt; extension go to Muhammad Mosa.]&lt;/p&gt; &lt;p&gt;&lt;a href="/file.axd?file=2011%2f2%2fTheFarm+Linq2UmbracoDataContextDriver+1.0.lpx"&gt;TheFarm Linq2UmbracoDataContextDriver 1.0.lpx (16.08 kb)&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:09 Z</pubDate>
      <a10:updated>2023-03-23T15:08:09Z</a10:updated>
    </item>
  </channel>
</rss>