<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nick Chapsas on Medium]]></title>
        <description><![CDATA[Stories by Nick Chapsas on Medium]]></description>
        <link>https://medium.com/@nick.chapsas?source=rss-a5fdd7e51866------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9B79BWiNaQ2MWSKvonp2Ag.jpeg</url>
            <title>Stories by Nick Chapsas on Medium</title>
            <link>https://medium.com/@nick.chapsas?source=rss-a5fdd7e51866------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 12:59:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nick.chapsas/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implementing server side CosmosDB pagination in a Blazor Web App (Part 1: Page Number and Page…]]></title>
            <link>https://medium.com/@nick.chapsas/implementing-server-side-cosmosdb-pagination-in-a-blazor-web-app-part-1-page-number-and-page-161edb5c20ea?source=rss-a5fdd7e51866------2</link>
            <guid isPermaLink="false">https://medium.com/p/161edb5c20ea</guid>
            <category><![CDATA[blazor]]></category>
            <category><![CDATA[cosmosdb]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Nick Chapsas]]></dc:creator>
            <pubDate>Tue, 30 Oct 2018 15:00:00 GMT</pubDate>
            <atom:updated>2018-11-16T09:47:53.131Z</atom:updated>
            <content:encoded><![CDATA[<h3>Implementing server side CosmosDB pagination in a Blazor Web App (Part 1: Page Number and Page Size)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HsYeP1LWGrLTUzRB7QmcSQ.png" /></figure><p>So let’s go straight to the point. A few blogs ago <a href="https://chapsas.com/cosmosdb-fluent-pagination-with-cosmonaut/">I wrote a blog</a> about the fluent pagination capabilities of <a href="https://github.com/Elfocrash/Cosmonaut">Cosmonaut</a>.</p><p>In this 2-part blog series we will go in detail on how you can implement different types of server side pagination with Cosmos DB in your application.</p><p>The pagination types that I will cover in these 2 blogs are the following:</p><ul><li>Page number/Page size (aka Skip/Take) pagination</li><li>Next/Previous pagination</li></ul><p>I will be implementing these solutions <a href="https://github.com/Elfocrash/CosmosDBPaginationSample">in a single Blazor project</a> with one page per pagination type. I’m going with Blazor because it’s quick to setup and so easy to use. I also wanna give it as much more visibility I can as possible because I think it’s an incredible project and I wanna see it succeed. Mega props to <a href="https://twitter.com/stevensanderson">Steve Sanderson</a> and to Microsoft.</p><p><em>(You can find instructions on how to install it on their GibHub page)</em></p><p><a href="https://github.com/aspnet/Blazor">Blazor</a> will allow us to write C# on the front end and have our view automatically update without postbacks whenever our data changes.</p><p>Blazor generates 3 projects out of the box and they are more than enough for us to work with.</p><ul><li>Client, is where the views and the pages of our site are located</li><li>Server, is the service that is running to expose the API</li><li>Shared, is where our shared contracts between Client and Server will live</li></ul><p>On the Shared project we will need to install 2 Nuget packages.</p><p><a href="https://www.nuget.org/packages/Cosmonaut">Cosmonaut</a> in order to add Cosmonaut support and <a href="https://www.nuget.org/packages/Cosmonaut.Extensions.Microsoft.DependencyInjection">Cosmonaut.Extensions.Microsoft.DependencyInjection</a> in order to add the dependency injection extensions that we will be using.</p><h3>Page number/Page size (aka Skip/Take) pagination</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/0*YbbMQUGrhZNEDz-7.JPG" /></figure><p>This is probably the most common type of pagination you’ve seen. It’s what you are using every time you use Google.</p><p>The idea is simple. You have 3 main properties.</p><ul><li>Current page number</li><li>Page size</li><li>Total items</li></ul><p>These three properties are enough for us to use in order to generate all the info we need for our Skip/Take pager.</p><p>Here is what the class holding the pagination logic looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab68e189dd9faa57696cf55b71bda8f9/href">https://medium.com/media/ab68e189dd9faa57696cf55b71bda8f9/href</a></iframe><p><em>The reason for the parameter-less constructor and the public setter on the properties is so I didn’t have to create another pager DTO and map it in order to use on the front end. However, that’s what you’d normally do.</em></p><p>It is not just the Pager that we are serving back though. We also need the entity that we will be retrieving. For this example I will use something simple like a Student class. It will only have an id, a first name and a last name. It looks like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d77a8eab49af3dd2bac4af6685293d2/href">https://medium.com/media/3d77a8eab49af3dd2bac4af6685293d2/href</a></iframe><p>The final model that our endpoint will be returning is a collection of students and the current pagination logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35c9d970982c461ca58c1c52b3296b6f/href">https://medium.com/media/35c9d970982c461ca58c1c52b3296b6f/href</a></iframe><p><strong>The backend code</strong></p><p>Now that we have all that set-up let’s see how our endpoint will be looking like.</p><p>First, we will need to register our Student CosmosStore on our Server&#39;s Startup.cs file inside the ConfigureServices method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85b43151e877334160bcea695c254f41/href">https://medium.com/media/85b43151e877334160bcea695c254f41/href</a></iframe><p>Adding those lines allows us to get ICosmosStore&lt;Student&gt; from .NET Core&#39;s dependency injection as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fc1efeb685ca24c0e7e646e925c54f7/href">https://medium.com/media/8fc1efeb685ca24c0e7e646e925c54f7/href</a></iframe><p>Our endpoint needs to able to do 2 things.</p><p>First it needs to get two query string parameters in order to return data relevant to the paged request. Those parameters are the page number and the page size.</p><p>The second thing it needs to do is to query for the total count of the documents in the collection and use that in order to generate the page logic.</p><p>Here’s the final version of the endpoint.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31bfd19a188dc27c3698083e12a80cd4/href">https://medium.com/media/31bfd19a188dc27c3698083e12a80cd4/href</a></iframe><p><strong>The frontend code</strong></p><p>First we need to add our functions code in our .cshtml file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2bc5be13bb67abfc51ee1204e1e65d8/href">https://medium.com/media/f2bc5be13bb67abfc51ee1204e1e65d8/href</a></iframe><p>As you can see we are using 2 methods and 2 fields.</p><p>We are overriding the OnInitAsync Blazor method which is automatically called on page load and we are calling the LoadDataAsync method we created to hit our Server&#39;s API endpoint and get our data back in order to visualise it.</p><p>Visualising the results with Blazor is as simple as doing the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ad159ebe9a6758658a39bfefd82dfa8/href">https://medium.com/media/8ad159ebe9a6758658a39bfefd82dfa8/href</a></iframe><p>Now every time our _students field changes in any way, the changes will be automatically reflected in our frontend (GOD I LOVE BLAZOR).</p><p>Now the pagination part of the code is a bit more complicated but at it’s core it’s a series of checks that ensure that we are seeing relevant pagination buttons based on our current state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a205c90fff04df7dde79072e8116e60d/href">https://medium.com/media/a205c90fff04df7dde79072e8116e60d/href</a></iframe><p>At the top we have a input field that allows us to update the page size of the pagination. <br> Under that we have the pagination numbered buttons including a First, Last, Previous and Next buttons. When the user clicks the button the LoadDataAsync method is called with the appropriate data and the endpoint is called, returning the new data which are automatically reflected in our web app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/0*XwH_s0t6jFxx0TG-.PNG" /></figure><p>Here is what the end result looks like. Sorry for the poor quality but Giphy didn’t keep my original quality (which makes sense to be honest). I hope you can still see what’s going on.</p><p>Keep in mind this is 100% server side pagination. There is nothing cached on the client side.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FSiK2hMivatFmC1tx25%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2FSiK2hMivatFmC1tx25&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FSiK2hMivatFmC1tx25%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="259" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f3ee266c706a18af7817d3f517816d4/href">https://medium.com/media/4f3ee266c706a18af7817d3f517816d4/href</a></iframe><h3>Performance concerns</h3><p>Even though this works great with small or limited data sets, it doesn’t scale well because the way Cosmonaut adds support for this is by going over each page until it reaches the requested. There are many ways to improve the performance with the most obvious one being caching, but we will actually go in depth on how we can use a combination of page number, page size, continuation token and caching to achieve optimal performance so stay tuned.</p><p>In the next part of the series we will look into Next and Previous pagination which are way more efficient and fast.</p><p>You can read more about pagination recommendations <a href="https://github.com/Elfocrash/Cosmonaut#pagination-recommendations">here</a>.</p><p>You can find a live version of the pagination app here: <a href="https://cosmosdbpage.azurewebsites.net/">https://cosmosdbpage.azurewebsites.net</a></p><p><strong>You can find the complete solution for this blog post here</strong>: <a href="https://github.com/Elfocrash/CosmosDBPaginationSample">https://github.com/Elfocrash/CosmosDBPaginationSample</a></p><p><em>Originally published at </em><a href="https://chapsas.com/implementing-skiptake-server-side-cosmosdb-pagination-in-a-blazor-web-app/"><em>chapsas.com</em></a><em> on October 30, 2018.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=161edb5c20ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CosmosDB Fluent Pagination with Cosmonaut]]></title>
            <link>https://medium.com/@nick.chapsas/cosmosdb-fluent-pagination-with-cosmonaut-4806f67c26cf?source=rss-a5fdd7e51866------2</link>
            <guid isPermaLink="false">https://medium.com/p/4806f67c26cf</guid>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[cosmosdb]]></category>
            <category><![CDATA[nosql]]></category>
            <dc:creator><![CDATA[Nick Chapsas]]></dc:creator>
            <pubDate>Thu, 27 Sep 2018 10:45:50 GMT</pubDate>
            <atom:updated>2018-09-27T10:45:50.592Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Ever wondered why the Azure Portal has scroll forward pagination and next/previous pagination but no page size + page number pagination? Go on take a wild guess. If you said “Because it runs on CosmosDB” then you’re right. The Azure portal is getting it’s results from CosmosDB that’s why you don’t see any form of server side skip and take pagination.</p><p>If you ever see a page size and page number combination in the portal then the pagination is happening on the client side. An example of that would be the “Logs” section of Application Insights, which is able to return paged results with page size and page number but those results are pre-fetched and they are just rendered on the spot. We know that because you can find all the results returned in pages of 100 in the network traffic tool of our browser of choice. The client side code will do the rest of the paging job.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8l2tS_TzOEczQNj_.png" /></figure><h3>CosmosDB pagination support</h3><p>CosmosDB doesn’t support your traditional type of skip/take pagination. Instead results are always paged with a maximum page size.</p><p>Skip take pagination is however, the most requested CosmosDB feature since August of 2014. The official ComosDB Team account replied in March of 2018 that they are planning to implement it, so we’re good.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/0*A16lhncLk0iq6oiT.png" /></figure><p>Judging based on the pace features are being delivered currently, I would not expect it to be delivered anytime soon so we need an alternative, at least for now.</p><p>So what does CosmosDB support as of today in terms of pagination?</p><p>Well, you have two values related to how results are being returned.</p><ul><li>MaxItemCount</li><li>RequestContinuation</li></ul><p>The MaxItemCount is the maximum nnumber of items to be returned in the enumeration operation. They can be less, but they won’t exceed that number. <br>The RequestContinuation is the continuation token which identifies which was the last result set for this operation and points CosmosDB to the next set of data it needs to return.</p><p>This is enough functionality for someone to workaround the lack of skip take pagination. This is where Cosmonaut kicks in and does the heavy lifting for you.</p><h3>Cosmonaut’s implementation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/0*O-I2jwBj1xkb9GAj.png" /></figure><p>Since I started working on <a href="https://github.com/Elfocrash/Cosmonaut">Cosmonaut</a> I knew that, eventually, I’d have to add fluent pagination support.</p><p>I did not want to mess with the Skip and Take LINQ methods because I know that eventually CosmosDB will support both of them (only supports Take for now).</p><p>Cosmonaut supports an IQueryable extension called WithPagination(). This method has two overloads.</p><ul><li>WithPagination(int pageNumber, int pageSize)</li><li>WithPagination(string continuationToken, int pageSize)</li></ul><p>As you can see, the first signature looks very much like a clear skip/take type implementation of the pagination. The second one is the direct CosmosDB approach.</p><p><strong>How do they work?</strong></p><p>The first signature, that has both page number and page size, can get inefficient and expensive as the page number value increases. This is because the only way this method can work is by going through all the pages from the start until it hits that requested page number and page size.</p><p>The second signature is both efficient and fast because it points straight to the next set of results. That’s because you provide the continuation token.</p><p>These methods work in Cosmonaut with both LINQ and SQL queries.</p><p><strong>How can you use them?</strong></p><p>The first signature is as simple to use as the following.</p><pre>var firstPage = await booksStore.Query().WithPagination(1, 10).OrderBy(x =&gt; x.Name).ToListAsync();  <br>var secondPage = await booksStore.Query().WithPagination(2, 10).OrderBy(x =&gt; x.Name).ToListAsync();</pre><p>As you can see, this reads very simple and I think is very straightforward. The same thing with SQL would look like this:</p><pre>var firstPage = await booksStore.Query(&quot;select * from c order by c.Name asc&quot;).WithPagination(1, 10).ToListAsync();  <br>var secondPage = await booksStore.Query(&quot;select * from c order by c.Name asc&quot;).WithPagination(2, 10).ToListAsync();</pre><p><em>These methods work with both the </em><em>CosmonautClient and the </em><em>CosmosStore methods.</em></p><p>However, as I mentioned before, this is the method that can get inefficient as you go to higher page numbers. That’s why the WithPagination signature that supports the continuation token is recommended.</p><p><strong>“But how do I get the continuation token?”</strong></p><p>The pagination update also introduced a new results extension method called ToPagedListAsync(). This method returns a CosmosPagedResults object. This object contains 3 very handy properties for your paged operations:</p><ul><li>Results, which is a list of the results for your query</li><li>HasNextPage, which is a boolean indicating if there are more pages</li><li>NextPageToken, which is a string providing you the next page continuation token, if any</li></ul><p>Let’s see how this works. I’ll write the same two page code that I have above but this time I’ll use the continuation token approach.</p><pre>var firstPage = await booksStore.Query().WithPagination(1, 10).OrderBy(x =&gt; x.Name).ToPagedListAsync();  <br>var secondPage = await booksStore.Query().WithPagination(firstPage.NextPageToken, 10).OrderBy(x =&gt; x.Name).ToPagedListAsync();</pre><p>Simple? I hope so. In a real life scenario, you would get the continuation token, store it on the client side (web app, desktop app, mobile app) and provide it for the next page.</p><p>CosmosPagedResults also support a GetNextPageAsync method which automatically gets the token and size from the last page. The same two queries would look like this:</p><pre>var firstPage = await booksStore.Query().WithPagination(1, 10).OrderBy(x =&gt; x.Name).ToPagedListAsync();  <br>var secondPage = await firstPage.GetNextPageAsync();</pre><p>Note:</p><p>All of the above works with the CosmonautClient querying methods as well:</p><pre>var result = await cosmonautClient.Query&lt;Book&gt;(&quot;localtest&quot;, &quot;shared&quot;).WithPagination(1, 1).ToListAsync();</pre><h3>Pagination recommendations</h3><p>Because page number and page size pagination goes though all the documents until it gets to the requested page, it can get slow and expensive. The recommended approach would be to use the page number and page size method once for the first page and get the results using the .ToPagedListAsync() method. This method will return the next continuation token and it will also tell you if there are more pages for this query. Then use the continuation token alternative of WithPagination to continue from your last query.</p><p>Keep in mind that this approach means that you have to keep state on the client for the next query, but that’s what you’d do if you were using previous/next buttons anyway.</p><p>I hope this helps you make integration with CosmosDB easier. Don’t forget to ask questions in the comments if something is unclear and feel free to suggest features or express ideas for <a href="https://github.com/Elfocrash/Cosmonaut">Cosmonaut</a>. Also, if you like the project then giving it a star on Github means a lot. Thank you.</p><p>Find more blogs like this on my personal site: <a href="http://chapsas.com/">http://chapsas.com</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4806f67c26cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing your CosmosDB C# code and automating it with AppVeyor]]></title>
            <link>https://medium.com/@nick.chapsas/testing-your-cosmosdb-c-code-and-automating-it-with-appveyor-7df2da50a4b4?source=rss-a5fdd7e51866------2</link>
            <guid isPermaLink="false">https://medium.com/p/7df2da50a4b4</guid>
            <category><![CDATA[nosql]]></category>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[cosmosdb]]></category>
            <dc:creator><![CDATA[Nick Chapsas]]></dc:creator>
            <pubDate>Tue, 18 Sep 2018 08:01:01 GMT</pubDate>
            <atom:updated>2018-09-18T08:01:01.634Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog I will talk about the challenges we face when we are writing unit tests for CosmosDB and how to automate them with AppVeyor.</p><h4>Introduction</h4><p>Don’t we all love well tested code? I know I do. <br>It’s just an awesome feeling knowing that if I change something over here, the thing over there didn’t break.</p><p>However, when your code depends on a third party library, then you have to hope that the code in that library is easy to work with and easy to test.</p><p>If this isn’t the first blog of mine that you read then you probably know that I’m working on an open-source ORM for CosmosDB called <a href="https://github.com/Elfocrash/Cosmonaut">Cosmonaut</a>. When I’m using a third party library and its code happens to be opensource then the first thing I do is to go to wherever it’s code is hosted and check if there are tests covering it. Nobody wants to use untested code, so I knew when I started that I should cover as many scenarios and cases as possible.</p><h4>Unit testing</h4><p>The CosmosDB SDK is not as friendly as it could be when it comes to unit testing (and the CosmosDB SDK team knows it, so it will get better). <br>Normally, the way you would unit test such code would be to mock the calls that would go over the wire and just return the dataset that you want this scenario to return.</p><p>However here is a list of things that make it really hard for you to test your code.</p><p><strong>Problem 1</strong></p><p>The ResourceResponse class has the constructor that contains the DocumentServiceResponse parameter marked as internal. This is bad because even though you can create a ResourceReponse object from your DTO class you cannot set things like RUs consumed, response code and pretty much anything else because they are all coming from the ResourceResponseBase which also has the DocumentServiceResponse marked as internal.</p><p><strong>Solution</strong></p><p>To solve this problem you have to somehow set the response headers for the ResourceResponse. As you might have guessed already, then only way you can do that is via reflection. Here is an extension method that can convert your generic type object to a ResourceResponse and allows you to set the responseHeaders.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f050df5ad2943c8fd36dec40f4a60df/href">https://medium.com/media/2f050df5ad2943c8fd36dec40f4a60df/href</a></iframe><p><em>This extension method is also part of Cosmonaut</em></p><p><strong>Problem 2</strong></p><p>The SDK is using IQueryable in order to build a fluent LINQ query and use it to query your CosmosDB. This LINQ query will be then converted into SQL via an internal LINQ2CosmosDBSQL provider that the SDK comes with. The problem with that is that if you want to mock your IDocumentClient&#39;s CreateDocumentQuery to return a specific data set based on a LINQ expression then you&#39;re in for a ride.</p><p><strong>Solution</strong></p><p>Lets take a look at the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/656c3f4da8224297147398c2da524f1c/href">https://medium.com/media/656c3f4da8224297147398c2da524f1c/href</a></iframe><p>As you can see, it needs quite the setup. That’s because the IQueryable needs to be setup from the ground up or else it won&#39;t be properly translated.</p><p><strong>Problem 3</strong></p><p>It’s basically the same as Problem 2 but for SQL, which makes it slightly easier because there is no IQueryable to setup.</p><p><strong>Solution</strong></p><p>Almost everything in the code from “Problem 2” is the same. The only differences are between the lines 24–47. You need to replace those with the code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa02c238048376eb525f1fc3866f6c99/href">https://medium.com/media/fa02c238048376eb525f1fc3866f6c99/href</a></iframe><p>These 3 are the most common unit testing problems you will face with the CosmosDB SDK. Thankfully the other operations are easier to test, especially if you make use of the ToResourceResponse extension method.</p><p><strong>UPDATE</strong>: As I was writing this Microsoft released the 2.0.0 version of the CosmosDB SDK packages and in them they changed the constructors of come classes.</p><p>If you are using a post 2.0.0 version of the SDK, here are the extension methods you need.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df3180e8235a30c169d92c6adc056b77/href">https://medium.com/media/df3180e8235a30c169d92c6adc056b77/href</a></iframe><h4>Integration testing</h4><p>Unit testing is awesome, but you also want to test your system against a real database. These tests should have a wider scope than the unit-sized context of your unit tests. I won’t go in depth on this one because everyone’s idea of integration testing seems to be different but what you need to remember is that we are trying to test again the real CosmosDB database.</p><p>As you probably know, CosmosDB comes with it’s own local emulator. That’s great because we can use it to run our integration/system tests against. What’s also great is that we can use the same emulator as part of our CI pipeline.</p><h4>Automating unit and integration testing</h4><p>I personally use <a href="https://www.appveyor.com/">AppVeyor</a> because it is incredibly easy to setup and get started, it supports loads of stuff such as Azure services and Nuget building out of the box and it is also free for open source projects.</p><p>AppVeyor is as simple to setup as linking your Github account and selecting the repository you want it to run against. Once you set that up it will trigger a build every time you commit code. There are tons of stuff to configure if you want something more specific but in this blog I will just explain how you can setup the CosmosDB emulator in AppVeyor.</p><p>AppVeyor will look for an appveyor.yml file in your repository. If it&#39;s not there it will use the settings that can be found in the &quot;Configuration&quot; section. Our appveyor.yml will be pretty simple. We just want to dotnet build and dotnet test our code. Before that however we need to install the CosmosDB Emulator on the AppVeyor VM. In order to do that we will need the following powershell script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e03cd3ceed3f63ddd270b3e5def727b1/href">https://medium.com/media/e03cd3ceed3f63ddd270b3e5def727b1/href</a></iframe><p>Now the CosmosDB Emulator is installed and you can enjoy testing against it using the default CosmosDB Emulator url https://localhost:8081 and the well known key C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==.</p><p>Here is the final appveyor.yml</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b9dbf52343df30fa9b61336eb8c715d/href">https://medium.com/media/0b9dbf52343df30fa9b61336eb8c715d/href</a></iframe><p>Once you set that up, a build will be kicked off every time you commit code in the repository and both unit and integration tests will be run against the local CosmosDB emulator instance.</p><p>Happy testing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7df2da50a4b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting started: Deploying resources in Azure with ARM Templates]]></title>
            <link>https://medium.com/@nick.chapsas/getting-started-deploying-resources-in-azure-with-arm-templates-fbe11e6bec7c?source=rss-a5fdd7e51866------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbe11e6bec7c</guid>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[azure]]></category>
            <dc:creator><![CDATA[Nick Chapsas]]></dc:creator>
            <pubDate>Thu, 12 Apr 2018 09:24:56 GMT</pubDate>
            <atom:updated>2018-04-17T13:02:32.033Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uDgJNNDqDQp8UDOx.png" /></figure><h4>Simple and easy resource provisioning</h4><p>One of the reasons I really like Azure is how easy it makes it to provision your infrastructure quickly and easily on the portal.</p><p>However, what’s even nicer is the support of Azure Resource Manager (ARM) templates for individual resources.</p><h4>What is an ARM template?</h4><p>An ARM template is a way to deploy one individual resource or a full resource group through Powershell, C#, Ruby or even the CLI.</p><p>ARM templates can be found in two places. Either as part of the individual resource (or the resource group) or by using the Resource Explorer service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/0*vAl3c9MdOqQBTDTg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/0*D6tqYd1qrnmbLmeF.png" /></figure><p>Something worth mentioning is that the usage of the <a href="http://resources.azure.com/">http://resources.azure.com</a> alternative isn’t recommended as some templates of preview features or in-depth info are missing from this section.</p><p>The ARM template consists of two files. The main template and the parameters. Both files are json files.</p><p>They usually consist of 4 parts.</p><ul><li>The parameters</li><li>The variables</li><li>The resources</li><li>The outputs</li></ul><p><strong>In the parameters section</strong>, you can define the values of the template that can be moved to parameters in order to provide a more generic template. That way, you can deploy multiple resources of that type using the same template but with different configuration each time. An example would be 4 different instances of CosmosDB in 4 different locations with just a single template. <br> You are limited to 255 parameters in a template. You can reduce the number of parameters by using objects that contain multiple properties.</p><p><strong>In the variables section</strong>, you construct values that can be used throughout your template. You do not need to define variables, but they often simplify your template by reducing complex expressions.</p><p><strong>In the resources section</strong>, you get to define which resources you want to create/update. Simple stuff. It can however get hectic because some of the templates, when exported, have several unnecessary values. A cleanup is recommended.</p><p><strong>Outputs</strong>, as the name implies, is the ability to have values outputted from the template to use somewhere else.</p><p>You can retrieve the output of a template by using the following powershell command.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f8f60c21ed180bcc45302885bc09734/href">https://medium.com/media/3f8f60c21ed180bcc45302885bc09734/href</a></iframe><p>Here is how the ARM template for CosmosDB looks like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98f68475eb844896a966b72196eb99e6/href">https://medium.com/media/98f68475eb844896a966b72196eb99e6/href</a></iframe><p>The parameters file for this template looks like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e6b6beb91a5ccc81fda0219019db8f7/href">https://medium.com/media/5e6b6beb91a5ccc81fda0219019db8f7/href</a></iframe><p>As we can see in the template itself, when run this template will create a CosmosDB resource in the UK West region.</p><p>The parameter has a defaultValue which in the template is cosmosdbnameValue. <br> If there was no parameter named cosmosdbname in the parameters file then when created the name of the CosmosDB instance would be the default one. However the parameters fill will override any defaulvalue from the main template.</p><h4>Running the template</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/0*lC3s16pPziNLUvnz.png" /></figure><p>There are several ways to run this ARM template. The easiest way if you are also creating/developing it through Visual Studio is to Right click at the project level and click “Deploy”. Keep in mind that this requires the Azure SDK to be installed for Visual Studio.</p><p>When a visual studio project of type “Azure Resource Group” is created it will also come out of the box with a powershell script called Deploy-AzureResourceGroup.ps1. This is the file that will do all the work for you, work-out your subscription based on the logged in account and use this data to create the resource on the right place.</p><p>Here is what the feedback of the powershell script looks like when run.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81eea58022ed4ba17b622018975d014e/href">https://medium.com/media/81eea58022ed4ba17b622018975d014e/href</a></iframe><h4>Deploying with Octopus</h4><p>Octupus Deploy makes the whole process way too easy. <br> All you need to do is deploy your provisioning projects as a package and then create a step using the built in “Deploy an Azure Resource Group” step option.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/261/0*2N5kxkddGCdkfn6l.png" /></figure><p>I will be showing this part of the process in depth on another blog.</p><p>Keep in mind that if you’re planning to deploy your resource templates using Octopus, it is a best practice to parameterize the templates using the octupus deploy variable way, example #{ParamaterName}. That way you can create multiple steps with the same template and parameters file and the only thing that changes is the scope of the options on each step.</p><p>More info on <a href="https://octopus.com/docs/deploying-applications/azure-deployments/resource-groups">https://octopus.com/docs/deploying-applications/azure-deployments/resource-groups</a></p><p><em>Originally published at </em><a href="http://chapsas.com/getting-started-deploying-resources-in-azure-with-arm-templates/"><em>chapsas.com</em></a><em> on April 12, 2018.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbe11e6bec7c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I made an entity store package for CosmosDB in C#: Cosmonaut]]></title>
            <link>https://medium.com/@nick.chapsas/i-made-an-entity-store-package-for-cosmosdb-in-c-cosmonaut-243434273d45?source=rss-a5fdd7e51866------2</link>
            <guid isPermaLink="false">https://medium.com/p/243434273d45</guid>
            <category><![CDATA[azure-cosmosdb]]></category>
            <category><![CDATA[package]]></category>
            <category><![CDATA[cosmosdb]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[azure]]></category>
            <dc:creator><![CDATA[Nick Chapsas]]></dc:creator>
            <pubDate>Fri, 30 Mar 2018 23:44:13 GMT</pubDate>
            <atom:updated>2018-04-19T13:44:07.553Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*yovZnkelJ4W3sSoRk-oQtw.png" /></figure><p>I like CosmosDB, I really do.<br>However I am not a fan of the Core CosmosDB SDK that Microsoft provides. It’s like the vanilla SQL connector. It’s cool and it can do a lot of things but I don’t necessarily want to use everything.</p><p>Here’s my experience developing an entity store for CosmosDB in C#.<br>The name of the project? <strong>Cosmonaut</strong></p><h3><strong>The problem</strong></h3><p>I should mention I’m also a big fan of Entity Framework (the core version).<br>It gives me everything I need to get my POCO objects, stick them in a database without having to write any SQL and get them back to manipulate them or use them.<br>The CosmosDB SDK isn’t like that. It’s more like a bad version of Dapper. It’s nice, fast and it does the job amazingly well, but you’ll still write a lot of code to get it up and running.</p><p>I want to use CosmosDB as my main persistence storage db to for prototype projects and some small applications I have up and running.</p><p>That’s the problem I wanted to solve. I will make a simple, tidy and easy to use entity store package for .NET Standard 2.0.</p><h3><strong>The idea</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*5u-53B7tjXhEOUX-575Qag.jpeg" /></figure><p>As I already mentioned I like how you Entity Framework goes straight to the point. You have a DbContext (or more) and you also have DbSets.</p><p>DbSets are essentially the tables in a SQL database. CosmosDB just like any other NoSQL database has Collections. DbContexts can be several things but they are associated with the database itself. The DbContext contains DbSets.</p><p>All I need to do to get this up and running is an interface and an implementation of that interface performing CRUD operations.</p><h3><strong>The limitations</strong></h3><p>Disclaimer: This is the first time that i sat down and said that I will create something that I can see the community using. It’s my first Nuget package.</p><blockquote>You cannot please everyone. It’s just impossible. So everything you read below is what I thought will be the “acceptable” by the wider audience.</blockquote><p>First and foremost to get this out of the way. Cosmonaut would have to make use of the package Microsoft.Azure.DocumentDB.Core. The plan is to fix all it’s flaws and make it’s usage straightforward.</p><h3><strong>The implementation</strong></h3><p><strong>“Can I see an ID please?”</strong></p><p>CosmosDB internally uses a property called id. What this tells me is that it will always need to have one. I can do nothing about it other than embrace it. Microsoft’s SDK allows you to insert an object in the database and retrieve it in an ORM fashion. It is essentially a dynamic which the library will try to cast to the object provided. What’s bizarre is that if your object has a property named Id (noticed the capital I?) then your document after creation will have one property named Id and another property named id which is the automatically generated internal one.</p><p>However if you try to get the document as the POCO object, it will (even more bizarrely) return the object with the internal id property mapped to the Id. This was the first (of many) limitations and issues i had to encounter. With some (not so elegant) code, the entities that the user is trying to add are being validated with the following way.</p><p>The consumer’s entities will need to have one of the following:<br>1. A string unique identifier named Id(case doesn’t matter)<br>2. A string unique identifier with any name the consumer wants but with the attribute [JsonProperty(&quot;id&quot;)] decorating it.<br>3. They need to implement the ICosmosEntity interface which will add the CosmosId automatically generated property.</p><p><strong>Operations</strong></p><p>All that the consumer has to do is use the ICosmosStore&lt;TEntity&gt;.<br>The implementation of the store needs just some cosmos options to be ready to use.</p><p>These options are:</p><ol><li>DatabaseName (Mandatory)</li><li>AuthKey (Mandatory)</li><li>EndpointUrl (Mandatory)</li><li>ConnectionPolicy (Optional, defaults to Microsoft’s default)</li><li>Collection Throughput (Optional, default to 400 which is the minimum because I don’t want to accidentally overcharge you)</li></ol><p>I really like it when the packages I use provide an extension method to setup whatever the package provides. So I did that aswell!<br>Setting up an entity store is as easy as adding this like to your ServiceCollection setup.<br>services.AddCosmosStore&lt;Book&gt;(cosmosSettings);</p><p>Now the consumers can just use .NET’s DI to get the service. Internally the service is registered as a Singleton which is Microsoft’s recommended way.</p><p>Once that is setup the user can use this store to do the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98fd1331a7e8be603b4ea9122860ee68/href">https://medium.com/media/98fd1331a7e8be603b4ea9122860ee68/href</a></iframe><p>This was my vision for what the initial release would have in terms of functionality. It looks very much like Entity Framework and surprisingly, it works as such.</p><p>Instead of going through the pain of finding self links and collections paths to update an object, now you can simply provide that object and Cosmonaut will do the rest. Same goes for any other operation. You can query the objects in the store with predicates and easily retrieve the ones matching the criteria.</p><p>Note here that I am still exposing the internal DocumentClient in case the consumer wants to do something that Cosmosnaut cannot do yet.</p><p><strong>The extra mile</strong></p><p>What I admire in great developers is that they never settle. They always ask “How can i make this better?”. I don’t think I’m one of them, but i really want to beone day. So i asked this very question.</p><p>The answer is simple. Up until now whenever you registered an object of some type Cosmonaut would use this type’s name to create a collection. If your entity class was called User then the collection created will be named users (which is the name of the class, pluralised and lowercase).</p><p>However this isn’t everyone’s cuppa. For that reason I create an attribute assignable to classes. It looks like this:</p><pre>[CosmosCollection(&quot;mycollection&quot;)]</pre><p>As you might have guessed already, the string mycollection will be the name of the collection that Cosmonaut will create for this entity. I really like this one.</p><p>Then I asked myself again.</p><p>You see, the way Entity Framework works is that it will not operate any actions when you use it’s DbSet’s methods. Instead it alter the objects and change their states so when the SaveChanges method is called then a transaction is created and entity tries to run the generated queries.</p><p>Unfortunately, even though there could be ways to go about something similar, I rejected the idea (after having a chat will some smarter people than me). The reason was that so many things can go wrong in both cosmos and the connection between that I would do more damage then good.</p><p><strong>Performance</strong></p><p>There would be no point in making something like this if I didn’t consider the possibility of someone using this in production (of some sort).</p><p>The way CosmosDB works is that you have the option to limit you RU/s (which is the throughput) to save money, because in Cosmos you pay based on many different factors, one of them being the usage itself. As you might have guessed, performance is depending on that in a huge degree.</p><p>What i didn’t know is that this is configurable on a collection level which i find pretty interesting. I took advantage of that feature.</p><p>Remember that CosmosCollection attribute? Well i also added a throughput option there as well. You can now configure the default throughput for all the collections at the settings level but you can also override this option with the [CosmosCollection(Throughput = 1500)] way. Cosmonaut will do all the setup for this on EntityStore registration.</p><p>Let’s talk numbers.<br>I performed several tests with the default connection configuration for CosmosDB and Cosmonaut.</p><p>With the collection set to 400 RU/s (which is the minimum)</p><pre>Added 1000 documents in 18875ms<br>Retrieved 1000 documents in 1086ms<br>Updated 1000 documents in 29969ms<br>Removed 1000 documents in 18977ms</pre><p>With the collection set to 5000 RU/s (which is a reasonable one)</p><pre>Added 1000 documents in 978ms<br>Retrieved 1000 documents in 58ms<br>Updated 1000 documents in 1432ms<br>Removed 1000 documents in 888ms</pre><p>With the collection set to 10000 RU/s (which is the max)</p><pre>Added 1000 documents in 885ms<br>Retrieved 1000 documents in 47ms<br>Updated 1000 documents in 1141ms<br>Removed 1000 documents in 722ms</pre><p>What do these results tell us?</p><p>Well it’s pretty obvious. The throughput makes an insanely huge difference on performance on 400 to 5k but not that much of a great one from 5k to 10k which is double.</p><p><strong>Let’s take a look closer and see why.</strong></p><p>First and foremost keep in mind that this is the worst case scenario. The tests are using the “Range” methods for these operations except for the retrieval one. There range operations are performing the tasks in parallel to provide the best performance possible. In your real life application you won’t be doing that of if you do you won’t be adding 1000 objects (probably).</p><p>I should take some of the blame for the lower RU/s. You see, when the max RU/s is met some of the documents used in the Range operations will fail to be added because of that. I have coded internal retry logic for those files. I think that making sure that everything that can be processed, is processed. This approach performed way better than having a foreach look and awaiting individual processes.</p><h4>Benchmarks</h4><p>These benchmarks took place 3 weeks after this post was made with a lot of performance improvements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*F13nvZEEA9zmlol4UPgMcg.png" /><figcaption>Benchmarks on 16/04/2018</figcaption></figure><h3>The code</h3><p>The project is open source under MIT license and published on Github.<br>You can find it here: <a href="https://github.com/Elfocrash/Cosmonaut">https://github.com/Elfocrash/Cosmonaut</a></p><p>It is also on Nuget. You can download it here: <a href="https://www.nuget.org/packages/Cosmonaut">https://www.nuget.org/packages/Cosmonaut</a></p><p>I would love some feedback, suggestions and issues reported.</p><p>Thanks,<br>Nick</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=243434273d45" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nuget Nuggets: Scrutor]]></title>
            <link>https://medium.com/@nick.chapsas/nuget-nuggets-scrutor-bdc9cace61d8?source=rss-a5fdd7e51866------2</link>
            <guid isPermaLink="false">https://medium.com/p/bdc9cace61d8</guid>
            <category><![CDATA[net-core]]></category>
            <category><![CDATA[nuget]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <dc:creator><![CDATA[Nick Chapsas]]></dc:creator>
            <pubDate>Fri, 23 Mar 2018 12:14:57 GMT</pubDate>
            <atom:updated>2018-03-23T12:14:57.029Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*a6mnxt5onfN21T1FI8W6oQ.jpeg" /></figure><p>It is undeniable that some of the best packages that are in any C# developer’s arsenal originated (or still are) in the community.</p><p>Just like any other developer that likes developing in his spare, I find myself testing around stuff and experimenting with ideas to see what might work. I recently starting sticking with a very specific project which I think has potential. This is my first project that went beyond the scale I expected it to code wise.</p><p>It is a project coded in .NET Core 2.1 with C#. What I really like about .NET Core is that there is an out of the box IoC system that anyone can jump right in and enjoy the benefits of dependency injection.</p><p>However, as I already mentioned, my project got out of hand and i ended up with a lot (A LOT) of interfaces and classes implementing those interfaces. This in return made my dependency registration really lengthy on my Startup.cs file. This is what the dependency registration looks like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/071418fbcac7271dabd0eb47c85a7c32/href">https://medium.com/media/071418fbcac7271dabd0eb47c85a7c32/href</a></iframe><p>You can see how, even though this isn’t technically a problem, can make my OCD go haywire.</p><p>Introducing <a href="https://github.com/khellang/Scrutor"><strong>Scrutor</strong></a></p><p>Scrutor (Scrutor — I search or examine thoroughly; I probe, investigate or scrutinise) is a Nuget package made by Kristian Hellang. It offers assembly scanning and decoration extensions for .NET Core’s Dependency Injection. I will focus on the scanning side of things even though the decoration part of it is also very interesting.</p><p>This is what the code above would look like with Scrutor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d6d11523786a56a2939701da7200b08/href">https://medium.com/media/3d6d11523786a56a2939701da7200b08/href</a></iframe><p>(IAssemblyMarker is an empty interface at the top level of the project. It will guide Scrutor into which assembly it has to scan)</p><p>That’s all there is. You can add more and more repositories, services, mappers and validator and as long as you have them registered with the base interfaces Scrutor will automatically scan the assemblies and register the dependencies with the appropriate lifetimes.</p><p>You can download Scrutor from Nuget using:</p><pre>Install-Package Scrutor</pre><pre>or</pre><pre>dotnet add package Scrutor</pre><p>Scrutor is Open Source under the MIT license.<br>Github link: <a href="https://github.com/khellang/Scrutor">https://github.com/khellang/Scrutor</a></p><p>Disclaimer: I am not affiliated with Scrutor in any way.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdc9cace61d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>