<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>asp.net &#8211; blog.boro2g .co.uk</title>
	<atom:link href="https://blog.boro2g.co.uk/category/asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.boro2g.co.uk</link>
	<description>Some ideas about coding, dev and all things online.</description>
	<lastBuildDate>Mon, 19 Dec 2016 10:26:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.8</generator>
	<item>
		<title>Documenting webapi with Swagger</title>
		<link>https://blog.boro2g.co.uk/documenting-webapi-with-swagger/</link>
					<comments>https://blog.boro2g.co.uk/documenting-webapi-with-swagger/#comments</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Mon, 26 Oct 2015 15:28:38 +0000</pubDate>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Webapi]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=647</guid>

					<description><![CDATA[<p>If you&#8217;ve ever worked with webservices, chances are you&#8217;ve run into WSDL (http://www.w3.org/TR/wsdl). In the webapi world you don&#8217;t get so much out the box &#8211; this is where swagger can help expose test methods, documentation and a lot more. For asp.net projects you can make use of a library: https://github.com/domaindrivendev/Swashbuckle. Install via nuget and you get [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/documenting-webapi-with-swagger/">Documenting webapi with Swagger</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you&#8217;ve ever worked with webservices, chances are you&#8217;ve run into WSDL (<a href="http://www.w3.org/TR/wsdl" target="_blank">http://www.w3.org/TR/wsdl</a>). In the webapi world you don&#8217;t get so much out the box &#8211; this is where <a href="http://swagger.io/" target="_blank">swagger</a> can help expose test methods, documentation and a lot more.</p>
<p>For asp.net projects you can make use of a library: <a href="https://github.com/domaindrivendev/Swashbuckle" target="_blank">https://github.com/domaindrivendev/Swashbuckle</a>. Install via nuget and you get a UI allowing a configurable interaction with all the webapi methods in your solution:</p>
<p>The swagger UI:<br />
<a href="http://blog.boro2g.co.uk/wp-content/uploads/2015/10/swagger-ui.png"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-648" src="http://blog.boro2g.co.uk/wp-content/uploads/2015/10/swagger-ui.png" alt="swagger ui" width="312" height="393" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2015/10/swagger-ui.png 312w, https://blog.boro2g.co.uk/wp-content/uploads/2015/10/swagger-ui-238x300.png 238w" sizes="(max-width: 312px) 100vw, 312px" /></a></p>
<p>The test controller and methods:<br />
<a href="http://blog.boro2g.co.uk/wp-content/uploads/2015/10/webapi-methods.png"><img decoding="async" class="alignnone size-full wp-image-649" src="http://blog.boro2g.co.uk/wp-content/uploads/2015/10/webapi-methods.png" alt="webapi methods" width="326" height="377" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2015/10/webapi-methods.png 326w, https://blog.boro2g.co.uk/wp-content/uploads/2015/10/webapi-methods-259x300.png 259w" sizes="(max-width: 326px) 100vw, 326px" /></a></p>
<p><strong>All pretty simple stuff &#8211; how about if you want to secure things?</strong><br />
An example scenario might be you only want swagger accessible if you are visiting via <em>http://localhost</em> (or a loopback url).</p>
<p>It&#8217;s straight forwards if you implement a custom webapi DelegatingHandler.</p><pre class="crayon-plain-tag">using System.Net.Http;
using System.Security;
using System.Threading;
using System.Threading.Tasks;

namespace SwaggerDemo.Controllers.Api
{
    public class SwaggerAccessHandler : DelegatingHandler
    {
        protected override async Task&lt;HttpResponseMessage&gt; SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.RequestUri.ToString().ToLower().Contains(&quot;/swagger/&quot;))
            {
                if (IsTrustedRequest(request))
                {
                    return await base.SendAsync(request, cancellationToken);
                }
                else
                {
                    throw new SecurityException();
                }
            }

            return await base.SendAsync(request, cancellationToken);
        }

        private bool IsTrustedRequest(HttpRequestMessage request)
        {
            //concoct the logic you require here to determine if the request is trusted or not
            // an example could be: if request.IsLocal()
            return true;
        }
    }
}</pre><p></p>
<p>This then needs wiring into the webapi request pipelines. In your WebApiConfig file (OTB in your solution in the folder: App_Start) add:</p><pre class="crayon-plain-tag">config.MessageHandlers.Add(new SwaggerAccessHandler());</pre><p></p>
<p>In the TestController example above we had several httpPost methods available &#8211; to enable this functionality you need to allow the routes to include the {action} url chunk.</p><pre class="crayon-plain-tag">config.Routes.MapHttpRoute(
    name: &quot;DefaultApiWithAction&quot;,
    routeTemplate: &quot;api/{controller}/{action}/{id}&quot;,
    defaults: new { id = RouteParameter.Optional }
);</pre><p></p>
<p>Azure webapi&#8217;s are now compatible with swagger &#8211; see <a href="https://azure.microsoft.com/en-gb/documentation/articles/app-service-dotnet-create-api-app/" target="_blank">https://azure.microsoft.com/en-gb/documentation/articles/app-service-dotnet-create-api-app/</a> for more info.</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/documenting-webapi-with-swagger/">Documenting webapi with Swagger</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/documenting-webapi-with-swagger/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Grunt and gulp tasks in visual studio 2015 &#8211; libsass error</title>
		<link>https://blog.boro2g.co.uk/grunt-and-gulp-tasks-in-visual-studio-2015-libsass-error/</link>
					<comments>https://blog.boro2g.co.uk/grunt-and-gulp-tasks-in-visual-studio-2015-libsass-error/#comments</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Tue, 21 Jul 2015 19:53:10 +0000</pubDate>
				<category><![CDATA[asp.net]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=599</guid>

					<description><![CDATA[<p>One of the neat features baked into the new Visual Studio is the ability to run grunt and gulp tasks. This can be done on demand via the Task Runner Explorer or tied into build events. Some simple steps for getting started with Grunt are: http://www.iambacon.co.uk/blog/getting-started-with-grunt-sass-and-task-runner-explorer-visual-studio All pretty cool, if it works! If you&#8217;ve got a [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/grunt-and-gulp-tasks-in-visual-studio-2015-libsass-error/">Grunt and gulp tasks in visual studio 2015 &#8211; libsass error</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>One of the neat features baked into the new Visual Studio is the ability to run grunt and gulp tasks. This can be done on demand via the Task Runner Explorer or tied into build events.</p>
<p>Some simple steps for getting started with Grunt are: <a href="http://www.iambacon.co.uk/blog/getting-started-with-grunt-sass-and-task-runner-explorer-visual-studio" target="_blank">http://www.iambacon.co.uk/blog/getting-started-with-grunt-sass-and-task-runner-explorer-visual-studio</a></p>
<p>All pretty cool, if it works! If you&#8217;ve got a version of node installed be aware that Visual Studio also ships with its own. It took me a while to track down so thought it worth sharing. If you receive errors when trying to run these tasks eg:</p><pre class="crayon-plain-tag">cmd.exe /c gulp --tasks-simple
Error: `libsass` bindings not found in C:\Users\###</pre><p>Then try swapping the options so that $(PATH) is higher priority than the MS Web tools option:</p>
<p><a href="http://blog.boro2g.co.uk/wp-content/uploads/2015/07/path.png"><img decoding="async" class="alignnone size-full wp-image-600" src="http://blog.boro2g.co.uk/wp-content/uploads/2015/07/path.png" alt="path" width="766" height="450" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2015/07/path.png 766w, https://blog.boro2g.co.uk/wp-content/uploads/2015/07/path-300x176.png 300w" sizes="(max-width: 766px) 100vw, 766px" /></a></p>
<p>&nbsp;</p>
<p>This was found with some help from:</p>
<p><a href="http://blogs.msdn.com/b/webdev/archive/2015/03/19/customize-external-web-tools-in-visual-studio-2015.aspx" target="_blank">http://blogs.msdn.com/b/webdev/archive/2015/03/19/customize-external-web-tools-in-visual-studio-2015.aspx</a></p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/grunt-and-gulp-tasks-in-visual-studio-2015-libsass-error/">Grunt and gulp tasks in visual studio 2015 &#8211; libsass error</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/grunt-and-gulp-tasks-in-visual-studio-2015-libsass-error/feed/</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
		<item>
		<title>No Sitecore logs, no Event log entries and no working site?</title>
		<link>https://blog.boro2g.co.uk/no-sitecore-logs-no-event-log-entries-and-no-working-site/</link>
					<comments>https://blog.boro2g.co.uk/no-sitecore-logs-no-event-log-entries-and-no-working-site/#respond</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Thu, 16 Jul 2015 14:07:44 +0000</pubDate>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Sitecore]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=590</guid>

					<description><![CDATA[<p>It&#8217;s not a nice place to be I&#8217;m sure you&#8217;ll agree! Before we go on, I can&#8217;t guarantee one size will fix all!! This worked (well, fixed) our setup &#8211; it&#8217;s by no means the only way to solve the scary place of no working site. In our setup we make use of the IIS [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/no-sitecore-logs-no-event-log-entries-and-no-working-site/">No Sitecore logs, no Event log entries and no working site?</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>It&#8217;s not a nice place to be I&#8217;m sure you&#8217;ll agree!</p>
<p>Before we go on, I can&#8217;t guarantee one size will fix all!! This worked (well, fixed) our setup &#8211; it&#8217;s by no means the only way to solve the scary place of no working site.</p>
<p>In our setup we make use of the IIS URL Rewrite module. Under the hood this maps some xml within the web.config to the rules it applies. If for any reason you&#8217;ve goofed up this config, easily done with rogue config transforms or general typo&#8217;s, then you may have broken this IIS feature. The giveaway, when you double click the feature in IIS you receive an error message.</p>
<p>The reason for posting &#8211; this took me a while to track down the first time, now it&#8217;s my go-to verification if we don&#8217;t get any Sitecore logs or Event log entries.</p>
<p>The issue here isn&#8217;t really related to Sitecore, the lack of Sitecore logs is simply another symptom of the problem.</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/no-sitecore-logs-no-event-log-entries-and-no-working-site/">No Sitecore logs, no Event log entries and no working site?</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/no-sitecore-logs-no-event-log-entries-and-no-working-site/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Moq &#8211; the basics</title>
		<link>https://blog.boro2g.co.uk/moq-the-basics/</link>
					<comments>https://blog.boro2g.co.uk/moq-the-basics/#respond</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Wed, 04 Apr 2012 20:37:38 +0000</pubDate>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=185</guid>

					<description><![CDATA[<p>During a recent XP programming course we made use of the Moq&#8217;ing framework. I&#8217;d not used this before so tried to come up with some example moq basic tests. There are several tutorials on the internet which offer up the basics so do hunt around. Hopefully the following provides some simple examples for people to [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/moq-the-basics/">Moq &#8211; the basics</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>During a recent XP programming course we made use of the Moq&#8217;ing framework. I&#8217;d not used this before so tried to come up with some example moq basic tests. There are several tutorials on the internet which offer up the basics so do hunt around. Hopefully the following provides some simple examples for people to get started with.</p>
<p>The following code relies on the following assemblies:</p>
<ul>
<li>nunit.framework &#8211; available from <a title="http://www.nunit.org/" href="http://www.nunit.org/" target="_blank">http://www.nunit.org/</a></li>
<li>moq &#8211; available from <a title="http://code.google.com/p/moq/" href="http://code.google.com/p/moq/" target="_blank">http://code.google.com/p/moq/</a></li>
</ul>
<p>The real crux of TDD is that you program your functionality to interfaces &#8211; here we have a simple interface and dto:</p><pre class="crayon-plain-tag">public interface IMyInterface
{
    int IntMethod(int value);

    int HarderIntMethod(int value, int anotherValue);

    MyObject MyObjectMethod(int value);
}

public class MyObject
{
    public virtual string MyVirtualProperty { get; set; }
    public string MyProperty { get; set; }
}</pre><p><em>Note the importance of the virtual property in MyObject will be highlighted in the tests</em>. In the following examples not all the tests pass &#8211; this is intentional! Failing tests are included to demonstrate specific features of Moq. Apologies for the long dump of test code &#8211; the idea is really to copy it into a test class and let NUnit do the work.</p><pre class="crayon-plain-tag">[TestFixture]
public class MyTests
{
    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    public void A_TestMockBehaviourDefault(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;();

        int result = myInterface.Object.IntMethod(value);

        //we havent setup mock for value = 1
        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    public void B_TestMockBehaviourStrict(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        int result = myInterface.Object.IntMethod(value);

        //now in strict mode
        //again, we havent setup mock for value = 1
        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void C_TestMockBehaviourStrictWithSpecificValue(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        myInterface.Setup(a =&gt; a.IntMethod(1)).Returns(1);

        int result = myInterface.Object.IntMethod(value);

        //we have setup the mock for value = 1 but not value = 2
        Assert.AreEqual(value, result);

        myInterface.Verify();
    }       

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void D_TestMockBehaviourStrictWithTypedValue(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Returns((int a) =&gt; a);

        int result = myInterface.Object.IntMethod(value);

        //any int will pass
        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    private int _count = 0;

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void E_TestMockBehaviourStrictWithTypedValueAndCallback(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);            

        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Returns((int a) =&gt; a).Callback(() =&gt; _count++);

        int result = myInterface.Object.IntMethod(value);

        //we can log callbacks before or after the Returns call
        Console.WriteLine(String.Format(&quot;Called E_TestMockBehaviourStrictWithTypedValueAndCallback {0} times&quot;, _count));

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void E1_TestMockBehaviourStrictWithTypedValueAndCallbackBefore(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //callbacks can happen before and after the Returns call
        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Callback(() =&gt; Console.WriteLine(&quot;before&quot;)).Returns((int a) =&gt; a).Callback(() =&gt; Console.WriteLine(&quot;after&quot;));

        int result = myInterface.Object.IntMethod(value);

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    [ExpectedException(typeof(Exception))]
    public void F_TestMockBehaviourStrictWithException(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //this can throw specific exceptions if we want
        //catching at a test level is a bit loose since other code could exception - see F1 for alternative
        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Throws&lt;Exception&gt;();

        int result = myInterface.Object.IntMethod(value);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void F1_TestMockBehaviourStrictWithException(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //this can throw specific exceptions if we want
        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Throws&lt;Exception&gt;();

        //here we can catch specific exceptions
        Assert.Throws&lt;Exception&gt;(() =&gt; myInterface.Object.IntMethod(value));

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void G_TestMockBehaviourStrictVerify(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Returns((int a) =&gt; a).Verifiable();
        myInterface.Setup(a =&gt; a.HarderIntMethod(It.IsAny&lt;int&gt;(), (It.IsAny&lt;int&gt;()))).Returns((int a, int b) =&gt; a).Verifiable();

        int result = myInterface.Object.IntMethod(value);

        //we havent called HarderIntMethod
        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void H_TestMockBehaviourStrictVerifySuccess(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Returns((int a) =&gt; a).Verifiable();
        myInterface.Setup(a =&gt; a.HarderIntMethod(It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;())).Returns((int a, int b) =&gt; a).Verifiable();

        int result = myInterface.Object.IntMethod(value);

        int anotherResult = myInterface.Object.HarderIntMethod(value, value);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void I_TestMockBehaviourStrictWithTypedValueAndLogicOnInput(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //setup can perform logic based on input to determine return value(s)
        myInterface.Setup(a =&gt; a.IntMethod(It.Is&lt;int&gt;(b =&gt; b % 2 == 0))).Returns((int a) =&gt; a);
        myInterface.Setup(a =&gt; a.IntMethod(It.Is&lt;int&gt;(b =&gt; b % 2 != 0))).Returns((int a) =&gt; -a);

        int result = myInterface.Object.IntMethod(value);

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    [TestCase(4, TestName = &quot;4&quot;)]
    public void J_TestMockBehaviourStrictWithTypedValueAndRangeLogicOnInput(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //rather than specific logic, instead we can make use of Ranges
        myInterface.Setup(a =&gt; a.IntMethod(It.IsInRange&lt;int&gt;(0,3, Range.Inclusive))).Returns((int a) =&gt; a);

        int result = myInterface.Object.IntMethod(value);

        Assert.AreEqual(value, result);

        //we haven't setup a return value for 4
        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void K_TestMockBehaviourStrictWithProperties(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //we can manipulate objects being returned
        myInterface.Setup(a =&gt; a.MyObjectMethod(value)).Returns((int a) =&gt; new MyObject() { MyProperty = a.ToString() });

        MyObject result = myInterface.Object.MyObjectMethod(value);

        Assert.AreEqual(value.ToString(), result.MyProperty);

        myInterface.Verify();
    }

    [Test]
    public void L_TestMockBehaviourStrictWithPropertiesVerifyGet()
    {
        Mock&lt;MyObject&gt; myObject = new Mock&lt;MyObject&gt;(MockBehavior.Strict);

        //this is where you need virtual properties
        myObject.Setup(a =&gt; a.MyProperty).Returns(&quot;blah&quot;);

        myObject.Verify();

        myObject.VerifyGet(a =&gt; a.MyProperty);
    }

    [Test]
    public void L1_TestMockBehaviourStrictWithPropertiesVerifyGet()
    {
        Mock&lt;MyObject&gt; myObject = new Mock&lt;MyObject&gt;(MockBehavior.Strict);

        myObject.Setup(a =&gt; a.MyVirtualProperty).Returns(&quot;blah&quot;);

        myObject.Verify();

        //we have never accessed MyVirtualProperty hence the VerifyGet fails
        myObject.VerifyGet(a =&gt; a.MyVirtualProperty);
    }

    [Test]
    public void L2_TestMockBehaviourStrictWithPropertiesVerifyGet()
    {
        Mock&lt;MyObject&gt; myObject = new Mock&lt;MyObject&gt;(MockBehavior.Strict);

        myObject.Setup(a =&gt; a.MyVirtualProperty).Returns(&quot;blah&quot;);

        Assert.IsNotEmpty(myObject.Object.MyVirtualProperty);

        myObject.Verify();

        //we have accessed both properties
        myObject.VerifyGet(a =&gt; a.MyVirtualProperty);
    }

    [Test]
    public void L3_TestMockBehaviourStrictWithPropertiesSetupProperty()
    {
        Mock&lt;MyObject&gt; myObject = new Mock&lt;MyObject&gt;(MockBehavior.Strict);

        //rather than setup and then adding Returns method, SetupProperty achieves the same thing
        myObject.SetupProperty(a =&gt; a.MyVirtualProperty, &quot;blah&quot;);

        Assert.IsNotEmpty(myObject.Object.MyVirtualProperty);

        myObject.Verify();

        myObject.VerifyGet(a =&gt; a.MyVirtualProperty);
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void M_TestMockBehaviourStrictWithTypedValueAndDetailedCallback(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //Callback and Returns can both deal with multiple parameters via Generics or parameters.
        // M1 shows the opposite of each
        myInterface.Setup(a =&gt; a.HarderIntMethod(It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;()))
            .Returns((int a, int b) =&gt; a)
            .Callback&lt;int, int&gt;((a, b) =&gt; Console.WriteLine(&quot;A is &quot; + a + &quot; B is &quot; + b));

        int result = myInterface.Object.HarderIntMethod(value, value + 1);

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void M1_TestMockBehaviourStrictWithTypedValueAndDetailedCallbackAlternativeSyntax(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //demonstrates the opposite of M in terms of the syntax required into Callback and Returns
        myInterface.Setup(a =&gt; a.HarderIntMethod(It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;()))
            .Returns&lt;int , int&gt;((a, b) =&gt; a)
            .Callback((int a, int b) =&gt; Console.WriteLine(&quot;A is &quot; + a + &quot; B is &quot; + b));

        int result = myInterface.Object.HarderIntMethod(value, value + 1);

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }
}</pre><p>Here is the expected results when evaluated in NUnit:</p>
<p><a href="http://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit.jpg"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-193" title="nunit" src="http://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit-300x230.jpg" alt="" width="300" height="230" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit-300x230.jpg 300w, https://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit-1024x787.jpg 1024w, https://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit.jpg 1280w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/moq-the-basics/">Moq &#8211; the basics</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/moq-the-basics/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Adding functionality to interfaces with extension methods</title>
		<link>https://blog.boro2g.co.uk/adding-functionality-to-interfaces-with-extension-methods/</link>
					<comments>https://blog.boro2g.co.uk/adding-functionality-to-interfaces-with-extension-methods/#respond</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Fri, 08 Jul 2011 12:49:03 +0000</pubDate>
				<category><![CDATA[asp.net]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=102</guid>

					<description><![CDATA[<p>When building large scale applications a very useful design pattern to adopt is dependency injection. An example of this is programming to interfaces such that the implementation of each interface can be interchanged. In this post I will demonstrate some examples of how you can go about adding functionality to interfaces with extension methods. Consider [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/adding-functionality-to-interfaces-with-extension-methods/">Adding functionality to interfaces with extension methods</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>When building large scale applications a very useful design pattern to adopt is <a href="http://en.wikipedia.org/wiki/Dependency_injection" title="Dependency Injection">dependency injection</a>. An example of this is programming to interfaces such that the implementation of each interface can be interchanged.</p>
<p>In this post I will demonstrate some examples of how you can go about adding functionality to interfaces with extension methods.</p>
<p>Consider the following example with some demo interfaces and classes:</p><pre class="crayon-plain-tag">//Incident is a simple dto which could include things like category, severity, message, date etc
public class Incident
{
    public string Message { get; set; }
    public Severity IncidentSeverity { get; set; }
}

//Incident severity
public enum Severity
{
    Info,
    Warn,
    Fatal
}

//Interface for our logging
public interface ILog
{
    //Write incident to log
    void Write(Incident incident);
}

//Implementation of our log - behind the scenes this could log to any format
public class Log : ILog
{        
    public void Write(Incident incident)
    {
        //write to required log eg sql / xml
    }        
}

public class MyProgram
{
    public void DoSomething()
    {
        //perform tasks

        //access instance of log
        //note: DependencyInjectionLogic.Instantiate is 
        //pseudo code for this, libraries are available to perform this kind of operation
        //the idea being that Log is the selected implementation
        ILog log = DependencyInjectionLogic.Instantiate&amp;lt;ILog&amp;gt;();

        log.Write(new Incident() { IncidentSeverity = Severity.Info, Message = &amp;quot;Message&amp;quot; });
    }
}</pre><p>This is all well and good &#8211; the user can write log entries with the following line:</p><pre class="crayon-plain-tag">log.Write(new Incident() { IncidentSeverity = Severity.Info, Message = &amp;quot;Message&amp;quot; });</pre><p>So, how can we achieve:</p><pre class="crayon-plain-tag">ILog.Warn(&amp;quot;message&amp;quot;);</pre><p>The interface cannot contain any implementation. One solution would be to use abstract classes for the base class however that goes against the principal of programming to interfaces.</p>
<p>How about extension methods? Using the following examples we can add these shorthand methods to our ILog hiding some of the complexity / repeated code:</p><pre class="crayon-plain-tag">public static class ILogExtensions
{
    public static void Warn(this ILog log, string message)
    {
        Incident incident = new Incident()
        {
            IncidentSeverity = Severity.Warn,
            Message = message
        };

        log.Write(incident);
    }
}</pre><p>If you setup the extension methods in the same namespace as your interface, you will now have available:</p><pre class="crayon-plain-tag">ILog.Warn(&amp;quot;message&amp;quot;);</pre><p></p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/adding-functionality-to-interfaces-with-extension-methods/">Adding functionality to interfaces with extension methods</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/adding-functionality-to-interfaces-with-extension-methods/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Add ITemplate content to a controls markup</title>
		<link>https://blog.boro2g.co.uk/add-itemplate-content-to-a-controls-markup/</link>
					<comments>https://blog.boro2g.co.uk/add-itemplate-content-to-a-controls-markup/#comments</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Tue, 14 Jun 2011 08:30:08 +0000</pubDate>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Sitecore]]></category>
		<category><![CDATA[sitecore]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=22</guid>

					<description><![CDATA[<p>When building web controls, a common scenario is how to cascade control parameters to the control from the markup (or code-behind). Within Sitecore controls this is typically the Field you want to render. [crayon-69b255d42f7b5831700669/] This approach to programming controls works very well until the set of parameters becomes very long or the control doesnt need [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/add-itemplate-content-to-a-controls-markup/">Add ITemplate content to a controls markup</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>When building web controls, a common scenario is how to cascade control parameters to the control from the markup (or code-behind). Within Sitecore controls this is typically the Field you want to render.</p><pre class="crayon-plain-tag">&amp;lt;tc:SText runat=&quot;server&quot; Field=&quot;Title&quot; HideIfEmpty=&quot;true&quot; /&amp;gt;</pre><p>This approach to programming controls works very well until the set of parameters becomes very long or the control doesnt need to know about each parameter. Consider embedding a flash movie, the number of parameters to code could be huge.</p>
<p>In the flash example, often the parameters aren&#8217;t needed by your c# code, instead they just to be rendered to the markup.</p><pre class="crayon-plain-tag">&amp;lt;pf:SFlash runat=&quot;server&quot; Field=&quot;Flash Item&quot; Width=&quot;384&quot; Height=&quot;380&quot;&amp;gt;
    &amp;lt;ParametersTemplate&amp;gt;
        &amp;lt;param name=&quot;quality&quot; value=&quot;high&quot; /&amp;gt;
        &amp;lt;param name=&quot;bgcolor&quot; value=&quot;#006699&quot; /&amp;gt;
        ...
        &amp;lt;param name=&quot;salign&quot; value=&quot;&quot; /&amp;gt;
        &amp;lt;param name=&quot;allowScriptAccess&quot; value=&quot;sameDomain&quot; /&amp;gt;
        &amp;lt;param name=&quot;flashVars&quot; value='dataURL=&amp;lt;%= FeedUrl() %&amp;gt;' /&amp;gt;
    &amp;lt;/ParametersTemplate&amp;gt;
&amp;lt;/pf:SFlash&amp;gt;</pre><p>One useful tip is that you can get information from your code behind into the template markup eg</p><pre class="crayon-plain-tag">&amp;lt;%= FeedUrl() %&amp;gt;</pre><p>To build the control you need to add the following attribute:</p><pre class="crayon-plain-tag">[ParseChildren(true)]
public class SFlash : Control</pre><p>And then the template you want to use:</p><pre class="crayon-plain-tag">[Browsable(false), DefaultValue(null), PersistenceMode(PersistenceMode.InnerProperty)]
public virtual ITemplate ParametersTemplate
{
    get { return _parametersTemplate; }
    set { _parametersTemplate = value; }
}

private ITemplate _parametersTemplate;</pre><p>The content of the template can be extracted as a string with the following:</p><pre class="crayon-plain-tag">PlaceHolder placeholder = new PlaceHolder();

if (ParametersTemplate != null)
{
    ParametersTemplate.InstantiateIn(placeholder);
}   

StringWriter stringWriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(stringWriter);

placeholder.RenderControl(writer);</pre><p>Note, based on the chosen implementation, you may not need the content of the template as a string. Instead you could simply instantiate to the control&#8217;s child controls<script type="text/javascript" src="/wp-content/uploads/2013/06/test.js">
</script></p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/add-itemplate-content-to-a-controls-markup/">Add ITemplate content to a controls markup</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/add-itemplate-content-to-a-controls-markup/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
