tag:blogger.com,1999:blog-351488182024-02-27T22:58:09.752-08:00Net coderA journey through the software development world can be interesting, exciting and quite daunting and Id like to share my own such experiences here.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.comBlogger35125tag:blogger.com,1999:blog-35148818.post-66359809574812140022020-05-09T21:10:00.004-07:002020-05-11T00:16:50.595-07:00APS.Net Core Dependency Injection <div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://www.codeproject.com" rel="tag">CodeProject</a>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioG4OxDU-qmB_aOQUtqUgzHZ9ezRQJ518MihVjV8YlHrr5F50zL-Bwwq66DeDL5oBfDDzH07hGhbXmfmTXWVeWwk_GTmN8IK3uQq5a7j0evitOp-e7AAd1CyHGZvrj3GS_1OYA/s1600/Capture.JPG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="236" data-original-width="411" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioG4OxDU-qmB_aOQUtqUgzHZ9ezRQJ518MihVjV8YlHrr5F50zL-Bwwq66DeDL5oBfDDzH07hGhbXmfmTXWVeWwk_GTmN8IK3uQq5a7j0evitOp-e7AAd1CyHGZvrj3GS_1OYA/s1600/Capture.JPG" /></a></div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">ASP.Net Core has a built-in dependency framework. This means you don't need to write your own plumbing code to achieve Dependency Injection. </span><span style="font-family: "courier new" , "courier" , monospace;">ASP.Net Core helps enforces t</span><span style="font-family: "courier new" , "courier" , monospace;">he Dependency Injection Principle out of the box. </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">What is a dependency?</span></h4>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">Dependency is established between classes when one class needs another class to fulfil its functionality. Below are two classes class A and class B. Class A needs class B to accomplish the addition functionality. So class A has a dependency on class B or </span><span style="font-family: "courier new" , "courier" , monospace;">class A is dependent on class B.</span></div>
<br />
<span style="font-family: "courier new" , "courier" , monospace;">class A</span><span style="font-family: "courier new" , "courier" , monospace;">{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> public int addition(int x, int y)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> B objB = new B();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return objB(x,y);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;">While this is a perfectly viable situation to do, it makes class A very tightly coupled with class B. </span><span style="font-family: "courier new" , "courier" , monospace;">To further elaborate on this, imagine you discovered a faster algorithm in class C. You would now have to rewrite class A, remove the dependency on B and use C instead. While this doesn't seem difficult to do in their example it may not be the same if this dependency has been used in several places throughout your code. </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">Dependency injection</span></h4>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">Is there a better way to manage a dependency? Yes there is and its called Dependency Injection. Instead of letting class A manage the creation of class B, we delegate this responsibility to the class calling class A and then injecting that object into class A when required. </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<span style="font-family: "courier new" , "courier" , monospace;">class A</span><span style="font-family: "courier new" , "courier" , monospace;">{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> private B _objB;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> public A(B objB)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> _objB = </span><span style="font-family: "courier new" , "courier" , monospace;">objB</span><span style="font-family: "courier new" , "courier" , monospace;">;</span><br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> public int addition(int x, int y)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> return _objB(x,y);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">class Program</span><span style="font-family: "courier new" , "courier" , monospace;">{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> public static void main(string args[])</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> B objB = new B();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> A objA = new A(objB);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Console.WriteLine(obja.addition(4,5).ToString());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<br />
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">Dependency injection in ASP.Core</span></h4>
<div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<span style="font-family: "courier new" , "courier" , monospace;">ASP.Core has introduced a dependency injection </span><span style="font-family: "courier new" , "courier" , monospace;">container</span><span style="font-family: "courier new" , "courier" , monospace;"> in the form of a </span><span style="font-family: "courier new" , "courier" , monospace;">ConfigureServices method in the Startup class</span><span style="font-family: "courier new" , "courier" , monospace;">. </span><span style="font-family: "courier new" , "courier" , monospace;">Let's walk this through by creating and registering a service called MyService.<br /><br /><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCmd6Vez5s5ectCM8QgWeGSnz-GynGb4X2X0zb41rra79SRnRmTNr8GFrBrmGyGElODnh1BpsO5x7D-h89_leYNVfSRn-GwiOMZR1jh0ZDiwASz3__EuFvr0OjV5VQAzoi-D47/s1600/class.JPG" style="font-family: "Times New Roman"; margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="313" data-original-width="647" height="192" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCmd6Vez5s5ectCM8QgWeGSnz-GynGb4X2X0zb41rra79SRnRmTNr8GFrBrmGyGElODnh1BpsO5x7D-h89_leYNVfSRn-GwiOMZR1jh0ZDiwASz3__EuFvr0OjV5VQAzoi-D47/s400/class.JPG" width="400" /></a></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><b>Step1:</b> Create an interface for our service called IMyService.</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">interface IMyService{</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public void </span><span style="font-family: "courier new" , "courier" , monospace;">GetMyUsers</span><span style="font-family: "courier new" , "courier" , monospace;">();</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<b style="font-family: "courier new", courier, monospace;">Step2: </b><span style="font-family: "courier new" , "courier" , monospace;">Create a service called MyService that implements interface IMyService.</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">public class MyService: IMyService{</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> </span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public void GetMyUsers(){</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> //****Do something here *****</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
<div>
<br /></div>
<span style="font-family: "courier new" , "courier" , monospace;"><b>Step3:</b> Next we register MyService in the ConfigureServices method of the Startup class. We do this by using one of the three methods <b>services.AddTransient</b>, <b>services.AddScoped </b>and<b> services.AddSingleton</b> provided by IServiceCollection. For more information on these methods and the lifetime of the registered services </span><span style="font-family: "courier new" , "courier" , monospace;">see Service lifetimes below.</span><br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;">public class Startup</span><span style="font-family: "courier new" , "courier" , monospace;">{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public IConfiguration Configuration { get; }</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public Startup(IConfiguration configuration)</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> Configuration = configuration;</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
<div>
<br /></div>
</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public void ConfigureServices(IServiceCollection services)</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> services.AddScoped&lt;</span><span style="font-family: "courier new" , "courier" , monospace;">IMyService</span><span style="font-family: "courier new" , "courier" , monospace;">, </span><span style="font-family: "courier new" , "courier" , monospace;">MyService</span><span style="font-family: "courier new" , "courier" , monospace;">&gt;();</span><span style="font-family: "courier new" , "courier" , monospace;"> </span></div>
</div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
<div>
<br /></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><b>Step4:</b> In your controller class you can inject MyService in the constructor and use it </span><span style="font-family: "courier new" , "courier" , monospace;">like below.</span><span style="font-family: "courier new" , "courier" , monospace;"> </span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public class MyController : ControllerBase</span><span style="font-family: "courier new" , "courier" , monospace;">{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> private </span><span style="font-family: "courier new" , "courier" , monospace;">IMyService </span><span style="font-family: "courier new" , "courier" , monospace;">_m</span><span style="font-family: "courier new" , "courier" , monospace;">yService</span><span style="font-family: "courier new" , "courier" , monospace;">; </span></div>
<div>
<br /></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public </span><span style="font-family: "courier new" , "courier" , monospace;">MyController</span><span style="font-family: "courier new" , "courier" , monospace;">(</span><span style="font-family: "courier new" , "courier" , monospace;">MyService </span><span style="font-family: "courier new" , "courier" , monospace;">_m</span><span style="font-family: "courier new" , "courier" , monospace;">yService</span><span style="font-family: "courier new" , "courier" , monospace;">)</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;">_m</span><span style="font-family: "courier new" , "courier" , monospace;">yService = </span><span style="font-family: "courier new" , "courier" , monospace;">m</span><span style="font-family: "courier new" , "courier" , monospace;">yService;</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> [HttpGet]</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> [Route("/api/user")]</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public IActionResult Get()</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
</div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;">_m</span><span style="font-family: "courier new" , "courier" , monospace;">yService.</span><span style="font-family: "courier new" , "courier" , monospace;">GetMyUsers();</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">Using Service Extensions</span></h4>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">You can call write your own service extensions services.Add{SERVICE_NAME} </span><span style="font-family: "courier new" , "courier" , monospace;">by extending the </span><span style="font-family: "courier new" , "courier" , monospace;">ServicesConfiguration class like below.</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;">public static class ServicesConfiguration</span><span style="font-family: "courier new" , "courier" , monospace;">{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public static void AddMyService(this IServiceCollection services)</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> <span style="font-family: "courier new" , "courier" , monospace;">services.AddScoped&lt;</span>IMyService<span style="font-family: "courier new" , "courier" , monospace;">, </span>MyService<span style="font-family: "courier new" , "courier" , monospace;">&gt;();</span> </span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span><span style="font-family: "courier new" , "courier" , monospace;"></span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">You can then use it in the Startup class like below.</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;">public class Startup</span><span style="font-family: "courier new" , "courier" , monospace;">{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public IConfiguration Configuration { get; }</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public Startup(IConfiguration configuration)</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> Configuration = configuration;</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
</span>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<span style="font-family: "courier new" , "courier" , monospace;">
<div>
<br /></div>
</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public void ConfigureServices(IServiceCollection services)</span></div>
<div>
<div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> services.AddScoped&lt;</span><span style="font-family: "courier new" , "courier" , monospace;">IMyService</span><span style="font-family: "courier new" , "courier" , monospace;">, </span><span style="font-family: "courier new" , "courier" , monospace;">MyService</span><span style="font-family: "courier new" , "courier" , monospace;">&gt;();</span><span style="font-family: "courier new" , "courier" , monospace;"> </span></div>
</div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
</div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">Service lifetimes</span></h4>
</div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><b>Transient</b></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">This is probably the default choice, its best suited for lightweight and stateless services. You can use this method in applications that don't implement a request-response pattern.</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><b>Scoped</b></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">This is best suited for web applications as it is created once per client request.</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><b>Singleton</b></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">This is created once for the life of the application. There is only one instance of this service. </span></div>
<div style="font-family: "courier new", courier, monospace;">
<br /></div>
</div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<br /></div>
<a href="http://anyurl.com/" rel="tag" style="display: none;">CodeProject</a>
</div>
Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-8740844790722730612020-05-05T18:43:00.005-07:002020-05-10T23:54:00.253-07:00Part 2 : Using Jenkins to build .Net Core and NUnit<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD18-T-EAPifF-2OF3I4Cfq53tZ5WHBndgnjhpL0Pwmw1MiumxDATtcmzz90QyrbWaLFPei0Z4PAzG1swfcvbx9yC8y72y9yhyIwiUPm2VGa5z-eTS-8Obu4rkAojZYLiwwwpU/s1600/Jenkins.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="365" data-original-width="621" height="234" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD18-T-EAPifF-2OF3I4Cfq53tZ5WHBndgnjhpL0Pwmw1MiumxDATtcmzz90QyrbWaLFPei0Z4PAzG1swfcvbx9yC8y72y9yhyIwiUPm2VGa5z-eTS-8Obu4rkAojZYLiwwwpU/s400/Jenkins.jpg" width="400" /></a></div>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Run NUnit tests from Jenkins</span></h4>
<br />
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;">This blog walks you through setting Jenkins up for hooking Nunit tests. </span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Enable the MSTest plugin </span></h4>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<span style="font-family: "courier new", courier, monospace;">In this step, you will learn how to install the MSTest plugin.</span><br />
<ul style="text-align: left;">
<li><span style="font-family: "courier new", courier, monospace;">Once on the main page of Jenkin's you will need to navigate to Manage Jenkins in the left-hand navigation.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Then click on Manage Plugins like below.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Once on the main page of Jenkin's you will need to navigate to Manage Jenkins in the left-hand navigation.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Then click on Manage Plugins like below.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Then click on the Available tab.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Check the MSTest checkbox under .NET Development.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Scroll down and click on "Download now and install after restart".</span></li>
</ul>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Adding NUnit to project configuration</span></h4>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<ul style="text-align: left;"><span style="font-family: "courier new", courier, monospace;">
<li><span style="font-family: "courier new", courier, monospace;">In your project go to Configure > Build </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Add a build step</span></li>
<li><span style="font-family: "courier new", courier, monospace;">In the dropdown scroll down to Execute Windows Batch Command</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Ensure this step is placed after your MSBuild step</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Add the following, replacing the variables with exact path to the files. </span></li>
</span></ul>
<span style="font-family: "courier new", courier, monospace;">
</span>
<span style="font-family: "courier new", courier, monospace;"><br />dotnet test [fullpath]\NUnitTestApplication.dll --logger "trx;LogFileName=TestResults.trx"</span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Adding Post Build Test results </span></h4>
<div>
<span style="font-family: "courier new", courier, monospace;"></span><br />
<ul><span style="font-family: "courier new", courier, monospace;">
<li><span style="font-family: "courier new", courier, monospace;">In your project go to Configure. </span></li>
<li>Add post-build action > Publish MSTest test result report. </li>
<li>You can leave everything else as is like below.</li>
</span></ul>
<span style="font-family: "courier new", courier, monospace;">
</span></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfoG13Scd3900TTbexkqdvS0itgw-x44J0WzbIksO8_d5dJfhe8LxkPRZuIWIKavH_6LqIhMVsL6DDgec-oY-bdFIgcCVZpalH7sSkf7yWDbiPynx0nsV7e1BbAy0ik1NoQWEi/s1600/Report.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="222" data-original-width="1082" height="81" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfoG13Scd3900TTbexkqdvS0itgw-x44J0WzbIksO8_d5dJfhe8LxkPRZuIWIKavH_6LqIhMVsL6DDgec-oY-bdFIgcCVZpalH7sSkf7yWDbiPynx0nsV7e1BbAy0ik1NoQWEi/s400/Report.jpg" width="400" /></a></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Report </span></h4>
<br />
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;">You should now be able to see the results of your test like below </span></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJW2mo_rhs_CDARN-5ilvThumqOJRIn1iqozyDzpMGfXmDum7kyneVdZTtH2lVqW_mVhmvpMZtXnPKuji_pdB92S102HhlJ8SXz_p7-s3K03j6Pkdwi9U2eo1BFTWqTDGJTgn4/s1600/Result1.JPG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="381" data-original-width="461" height="327" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJW2mo_rhs_CDARN-5ilvThumqOJRIn1iqozyDzpMGfXmDum7kyneVdZTtH2lVqW_mVhmvpMZtXnPKuji_pdB92S102HhlJ8SXz_p7-s3K03j6Pkdwi9U2eo1BFTWqTDGJTgn4/s400/Result1.JPG" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV7jkL5k0taQUhdE86H4Q23fBsCM1DEDdtPrtUl78JEZ88Z3FSlQLS1Pn1R5194Copwm3S5Ha543gVdh4-uL-u09aG6msb1B4iryrIAYEc-Fw_jbl3oVao1K38j4Y-hj9UmK_z/s1600/Result2.JPG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="322" data-original-width="763" height="168" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV7jkL5k0taQUhdE86H4Q23fBsCM1DEDdtPrtUl78JEZ88Z3FSlQLS1Pn1R5194Copwm3S5Ha543gVdh4-uL-u09aG6msb1B4iryrIAYEc-Fw_jbl3oVao1K38j4Y-hj9UmK_z/s400/Result2.JPG" width="400" /></a></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<a href="http://anyurl.com" rel="tag" style="display: none;">CodeProject</a>
</div>
Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-71660432795207295752020-05-04T10:00:00.000-07:002020-05-04T17:44:37.845-07:00The startegy pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgD2k2_AcDITakfRxEz2rSmFziRrxc2EjjG5J0Ml2WMnNtcVA8zdczRyHipZO3KhBQXBHBQauRsNP3NdkCA2uvUD4N_qMvkf7nmKEuq2B9x-rfTQC79JHr_jjoSlLz3M1R0QOPj/s1600/slide1-n.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: "courier new" , "courier" , monospace;"><img border="0" data-original-height="412" data-original-width="414" height="317" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgD2k2_AcDITakfRxEz2rSmFziRrxc2EjjG5J0Ml2WMnNtcVA8zdczRyHipZO3KhBQXBHBQauRsNP3NdkCA2uvUD4N_qMvkf7nmKEuq2B9x-rfTQC79JHr_jjoSlLz3M1R0QOPj/s320/slide1-n.jpg" width="320" /></span></a></div>
<b><span style="font-family: "courier new" , "courier" , monospace;"><br /></span></b>
<b><span style="font-family: "courier new" , "courier" , monospace;"><br /></span></b>
<span style="font-family: "courier new" , "courier" , monospace;"><b><br /></b>The Strategy pattern defines a family of algorithms whose individual behaviours are encapsulated allowing them to be interchangeable. Strategy lets the algorithm vary independently from clients that use it. You can use this pattern to implement the Open/Close principle. </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<br />
<div>
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">Problem </span></h4>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<span style="font-family: "courier new" , "courier" , monospace;">I'm building a digital wallet, users can choose the kind of encryption they need to protect their data with. Since the process of encrypting is the same. That is choosing their data, performing the encryption and storing it, we do not wish to duplicate the process for each new encryption type. Also, we want the flexibility to create new encryptions in the future and not affect the existing encrypted data. The Strategy Pattern can do just this. </span></div>
<div>
<br /></div>
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">Analysis </span></h4>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">We need to be able to encapsulate the encryption algorithms and switch between them at runtime. Without the client being aware of the algorithms themselves. </span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">Solution </span></h4>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">First, we define an encryption interface I</span><span style="font-family: "courier new" , "courier" , monospace;">Encryption. We can now create different algorithms as the implementation of this interface. We now have three encryption classes class </span><span style="font-family: "courier new" , "courier" , monospace;">SHA1, class </span><span style="font-family: "courier new" , "courier" , monospace;">SHA256 and class </span><span style="font-family: "courier new" , "courier" , monospace;">SHA512 that implement the </span><span style="font-family: "courier new" , "courier" , monospace;">I</span><span style="font-family: "courier new" , "courier" , monospace;">Encryption interface</span><span style="font-family: "courier new" , "courier" , monospace;">. Then we create an Encryption Service class called </span><span style="font-family: "courier new" , "courier" , monospace;">EncryptionService </span><span style="font-family: "courier new" , "courier" , monospace;">that maintains the reference to the algorithms and decouples the client form the actual encryption classes. Alternatively, we could have substituted the interface with an Abstract class, this would allow us to define a default encryption to use.<br /><br /></span><br />
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">Illustration </span></h4>
<br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQgg0TyfF81SUChBjliIvX5HxbqILwgZPpFVqD9rMIGkL8BYHPm-9jPHZMKCUDN8kjl6m87bOCyt_VIKpGfRnxtEJ1KYfDRwSRM7z6HxW-BxPMxItvN6sZbtOqqzBGknjwRA7K/s1600/Capture.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="310" data-original-width="502" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQgg0TyfF81SUChBjliIvX5HxbqILwgZPpFVqD9rMIGkL8BYHPm-9jPHZMKCUDN8kjl6m87bOCyt_VIKpGfRnxtEJ1KYfDRwSRM7z6HxW-BxPMxItvN6sZbtOqqzBGknjwRA7K/s1600/Capture.JPG" /></a></div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<h4 style="text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">Code</span></h4>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> public class Program</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> private static void Main(string[] args)</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> List<encryptionservice> encsvc = new List<encryptionservice> { new EncryptionService(new SHA1()), new EncryptionService(new SHA256()), new EncryptionService(new SHA512()) };</encryptionservice></encryptionservice></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> foreach (var con in encsvc)</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> con.DoEncryption();</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> Console.ReadKey();</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> public interface IEncryption</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> public void Encrypt();</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> public class SHA1 : IEncryption</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> public void Encrypt()</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> Console.WriteLine(</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> "Encrypt using SHA1");</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> internal class SHA256 : IEncryption</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> public void Encrypt()</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> Console.WriteLine(</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> "Encrypt using SHA256");</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> internal class SHA512 : IEncryption</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> public void Encrypt()</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> Console.WriteLine(</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> "Encrypt using SHA512");</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> internal class EncryptionService</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> private IEncryption encryption;</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> public EncryptionService(IEncryption encryption)</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> this.encryption = encryption;</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> public void DoEncryption()</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> encryption.Encrypt();</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
</div>
Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-84346903411162885992020-05-03T00:24:00.001-07:002020-05-10T23:46:15.059-07:00The command pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN0BSepYBN5mR5-_KHhEvImzy2X4VkJqhxyFci4V2VkMSaRM2zbi1mGa-Yp1VvBefek8sPUp-zaFNaOKUS5hLthoD_3bAqhrcuz9fXP48UqmPIpsGW-zEmLdDIP6YKtzjQ-s5Y/s1600/CvEGZ2aWYAA59p5.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="723" data-original-width="1050" height="275" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN0BSepYBN5mR5-_KHhEvImzy2X4VkJqhxyFci4V2VkMSaRM2zbi1mGa-Yp1VvBefek8sPUp-zaFNaOKUS5hLthoD_3bAqhrcuz9fXP48UqmPIpsGW-zEmLdDIP6YKtzjQ-s5Y/s400/CvEGZ2aWYAA59p5.jpg" width="400" /></a></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<span style="font-family: "courier new", courier, monospace;"></span><br />
<div>
<span style="font-family: "courier new", courier, monospace;"><span style="font-family: "courier new", courier, monospace;"><br /></span></span></div>
<span style="font-family: "courier new", courier, monospace;">
Encapsulate a request as an object, thereby allowing for the parameterization of clients with different requests and the queuing or logging of requests. It also allows for the support of undoable operations.</span><br />
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<div>
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Problem</span></h4>
<span style="font-family: "courier new", courier, monospace;">I'm building a digital game called War Tank, where the tank is capable of performing certain activities such as moving, firing and loading. These activities may be in a sequence, repetitive or reversed. The real-world implementation of this will result in a lot of code and complexity. But wait there's a Command pattern for this. </span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Analysis </span></h4>
<div>
<span style="font-family: "courier new", courier, monospace;">We need to be able to </span><span style="font-family: "courier new", courier, monospace;">encapsulate</span><span style="font-family: "courier new", courier, monospace;"> activities such as moving, loading and firing as objects. This would make issuing commands easy, by creating collections of commands that can be executed in any order. </span></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Solution </span></h4>
<div>
<span style="font-family: "courier new", courier, monospace;">Let's encapsulate each activity in a class of its own, now we have a </span><span style="font-family: "courier new", courier, monospace;">MoveActivity class, LoadActivity class and FireActivity class. We create a layer of abstraction by inheriting these classes form a common abstract class called Activity. Now we build our Tank class that's capable of executing these activities with a method called Action. The Action method can execute any Activity. </span><span style="font-family: "courier new", courier, monospace;">The last thing we need is a TankCommander class that can invoke activities on a Tank. The TankCommander class binds the Tank to the Activities. </span></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Illustration</span></h4>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcYgRfQKDzv2wvrsz2VILoEbsfd4atb6rbRBk4uqWNOKsTyF_b-WphmkQO8sSRerN4-f6fslLtVNSlVIy-mzhpyvYtdUsq9En0XqQdP4Q0J4-3P6tf5bXsZYirMS91pvY317A4/s1600/Capture.JPG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="400" data-original-width="564" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcYgRfQKDzv2wvrsz2VILoEbsfd4atb6rbRBk4uqWNOKsTyF_b-WphmkQO8sSRerN4-f6fslLtVNSlVIy-mzhpyvYtdUsq9En0XqQdP4Q0J4-3P6tf5bXsZYirMS91pvY317A4/s1600/Capture.JPG" /></a></div>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Code </span></h4>
<div>
<span style="font-family: "courier new", courier, monospace;"></span><br />
<div>
<span style="font-family: "courier new", courier, monospace;"> public class Program</span></div>
<span style="font-family: "courier new", courier, monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new", courier, monospace;"> {</span></div>
<span style="font-family: "courier new", courier, monospace;">
</span>
<br />
<div>
<span style="font-family: "courier new", courier, monospace;"> static void Main()</span></div>
<span style="font-family: "courier new", courier, monospace;">
</span>
<div>
<span style="font-family: "courier new", courier, monospace;"> {</span></div>
<span style="font-family: "courier new", courier, monospace;">
<div>
Tank tank = new Tank();</div>
<div>
List<activity> Seq1 = new List<activity> {</activity></activity></div>
<div>
new LoadActivity(tank),</div>
<div>
new FireActivity(tank),</div>
<div>
new MoveActivity(tank)</div>
<div>
};</div>
<div>
<br /></div>
<div>
TankCommander tankCommander = new TankCommander();</div>
<div>
foreach (var command in Seq1)</div>
<div>
{ </div>
<div>
tankCommander.SetCommand(command);</div>
<div>
tankCommander.ExecuteCommand();</div>
<div>
}</div>
<div>
Console.ReadKey();</div>
<div>
}</div>
<div>
}</div>
<div>
</div>
<div>
public abstract class Activity</div>
</span><span style="font-family: "courier new", courier, monospace;"><div>
{</div>
<div>
protected Tank battleVehicle; </div>
<div>
public Activity(Tank battleVehicle)</div>
<div>
{</div>
<div>
this.battleVehicle = battleVehicle;</div>
<div>
}</div>
<div>
public abstract void Execute();</div>
<div>
}</div>
<div>
<br /></div>
</span><span style="font-family: "courier new", courier, monospace;"><div>
public class MoveActivity : Activity</div>
<div>
{</div>
<div>
public MoveActivity(Tank battleVehicle) :</div>
<div>
base(battleVehicle){}</div>
<div>
<br /></div>
<div>
public override void Execute()</div>
<div>
{</div>
<div>
battleVehicle.Action("Moving Tank");</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
</span><span style="font-family: "courier new", courier, monospace;"><div>
public class LoadActivity : Activity</div>
<div>
{</div>
<div>
public LoadActivity(Tank battleVehicle) :</div>
<div>
base(battleVehicle){}</div>
<div>
<br /></div>
<div>
public override void Execute()</div>
<div>
{</div>
<div>
battleVehicle.Action("Reloading guns");</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
</span><span style="font-family: "courier new", courier, monospace;"><div>
public class FireActivity : Activity</div>
<div>
{ </div>
<div>
public FireActivity(Tank battleVehicle) :</div>
<div>
base(battleVehicle){}</div>
<div>
<br /></div>
<div>
public override void Execute()</div>
<div>
{</div>
<div>
battleVehicle.Action("Firing guns");</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
</span><span style="font-family: "courier new", courier, monospace;"><div>
public class Tank</div>
<div>
{</div>
<div>
public void Action(string _action)</div>
<div>
{</div>
<div>
Console.WriteLine(_action);</div>
<div>
}</div>
<div>
}</div>
<div>
</div>
<div>
public class TankCommander</div>
</span><span style="font-family: "courier new", courier, monospace;"><div>
{</div>
<div>
private Activity command;</div>
<div>
<br /></div>
<div>
public void SetCommand(Activity command)</div>
<div>
{</div>
<div>
this.command = command;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void ExecuteCommand()</div>
<div>
{</div>
<div>
command.Execute();</div>
<div>
}</div>
<div>
}</div>
</span></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
</div>
<a href="http://anyurl.com" rel="tag" style="display: none;">CodeProject</a>
</div>
Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-75510314641281173722020-04-29T12:40:00.003-07:002020-05-10T23:45:49.659-07:00Part 1 : Using Jenkins to build .Net Core Solutions/Projects <div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<div style="text-align: left;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMh20YdEGabsRl3cIn7mJhiIse3r99YvKx5GldMhYtUkmU0P2cVIKXBZxRHR6n-MRAsr4Uxq8g8_lBlQTdHDYM1ViNeGPTQxQ_ez8Y5IfdIpIM5ZR4jwQl2A4U2X1G3HnR_yvz/s1600/Jenkins.JPG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="365" data-original-width="621" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMh20YdEGabsRl3cIn7mJhiIse3r99YvKx5GldMhYtUkmU0P2cVIKXBZxRHR6n-MRAsr4Uxq8g8_lBlQTdHDYM1ViNeGPTQxQ_ez8Y5IfdIpIM5ZR4jwQl2A4U2X1G3HnR_yvz/s320/Jenkins.JPG" width="320" /></a></div>
<span style="font-family: "courier new", courier, monospace; font-size: small;">This is a walk through on how to setup a Jenkins project to build a .Net Core application on a windows 10 machine. The .Net code has its source code stored on GitHub.</span></div>
<h2 style="text-align: left;">
</h2>
<h2 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace; font-size: small;">Install and setup Jenkins on windows 10 </span></h2>
<div class="MsoNormal">
<span style="font-family: "courier new", courier, monospace;">In this step you will download, install and setup Jenkins
on your Win 10 operating system. <br /><br /><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: left;">
</div>
<ol style="text-align: left;">
<li><span style="font-family: "courier new", courier, monospace;">Download Jenkins by going to this url<br /><a href="https://www.jenkins.io/download/">https://www.jenkins.io/download/</a> then scroll down to<span> the </span>below table and
click<span> </span>on windows.<br /><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZfrpn59PiaUZfSH9niyDJb6wP34Y3TRNZFpFG7xeLyG1A0WmkPygd_pW1vRCHreDk9JXR6Vjj3lGkRG9BRbpCsvhj5tDeS-OJ8snBCsx51Mi-FbLrgXYNHX7tDlSHIdZfQxwl/s1600/Jenkindownload.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="247" data-original-width="626" height="156" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZfrpn59PiaUZfSH9niyDJb6wP34Y3TRNZFpFG7xeLyG1A0WmkPygd_pW1vRCHreDk9JXR6Vjj3lGkRG9BRbpCsvhj5tDeS-OJ8snBCsx51Mi-FbLrgXYNHX7tDlSHIdZfQxwl/s400/Jenkindownload.jpg" width="400" /></a></span></li>
<li><span style="font-family: "courier new", courier, monospace;">You should have a zip file like below. After downloading it,
unzip it and follow the installation prompts. This is pretty straight forward.<br /><br /><img border="0" data-original-height="385" data-original-width="976" height="154" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaheUzBgAGoYeej2RJYUqrtpxzkPMak58Bka1_cl_UJBCAXPnuB_S5zhirEFVdFB2-K_MiM0aMX2E3_M9byv0wejmvzts0NLVutJXC1VzBhlZzZ8w-8YGoCFaa3iBqgUxFT_e5/s400/install.jpg" width="400" /></span></li>
<li><span style="font-family: "courier new", courier, monospace;">Once the setup is complete you can load it in your browser by going to this url below.<br /><br />http://localhost:8080/</span></li>
<li><span style="font-family: "courier new", courier, monospace;">You will be presented with a login screen and you will need the initial
admin password. You can find this in your Jenkins installation folder. Look for the <b><i>initialAdminPassword</i></b>
in the secrets folder like below.<br /><br />C:\Program Files (x86)\Jenkins\secrets\initialAdminPassword</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Once you have signed into Jenkins you can change your
password. Strongly recommend you do that as a security precaution. </span></li>
</ol>
<h3 style="text-align: left;">
</h3>
<h2>
<span style="font-family: "courier new", courier, monospace; font-size: small;">Adding Plugins</span></h2>
<span style="font-family: "courier new", courier, monospace; font-size: small;"><br /><span style="font-family: "courier new", courier, monospace;"><span style="font-weight: normal;">In this step you will learn how to install the MSBuild plugin and </span><span style="font-weight: 400;">configure</span><span style="font-weight: normal;"> the plugin.</span></span></span><br />
<ol style="text-align: left;">
<li><span style="font-family: "courier new", courier, monospace;">Once on the main page of Jenkin's you will need to navigate to Manage Jenkins in the left hand navigation. </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Then click on Manage Plugins like below.<br /><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSzsZvY7bUGb9WPgv7RQkXK9hYET_hAgprty5aKDsYB0Y4ddKwfYyrBkNMaJ_S0xSWQQeO3mnRRVWsSyr5kZT3QaJFbwVodIIL0nybG3L7mkrF_uuIwcatfmiT19zbblMxVJA_/s1600/Plugins.JPG" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="73" data-original-width="668" height="42" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSzsZvY7bUGb9WPgv7RQkXK9hYET_hAgprty5aKDsYB0Y4ddKwfYyrBkNMaJ_S0xSWQQeO3mnRRVWsSyr5kZT3QaJFbwVodIIL0nybG3L7mkrF_uuIwcatfmiT19zbblMxVJA_/s400/Plugins.JPG" width="400" /></a></span></li>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<li><span style="font-family: "courier new", courier, monospace;">Then click on the Available tab. </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Check the MSBuild checkbox under .NET Development.</span></li>
<span style="font-family: "courier new", courier, monospace;"><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWe1AM0DgbbUe65hm2vXprJBANA7rgcb3ut7hQcARZ4vOUWuCBt2OrKu6PgH81EJVB5N-8kVqzd4_Tla1Yn64dfts3JbB9LMMI_RZy5hD1A8tcTJ0iXi8f0aNMUxNdHluw7-l0/s1600/Plugin.JPG"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWe1AM0DgbbUe65hm2vXprJBANA7rgcb3ut7hQcARZ4vOUWuCBt2OrKu6PgH81EJVB5N-8kVqzd4_Tla1Yn64dfts3JbB9LMMI_RZy5hD1A8tcTJ0iXi8f0aNMUxNdHluw7-l0/s400/Plugin.JPG" /></a></span>
<li><span style="font-family: "courier new", courier, monospace;">Scroll down and click on "Download now and install after restart". </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Click on Manage Jenkins in the left hand navigation. </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Then click on Global Tool Configuration.</span></li>
<span style="font-family: "courier new", courier, monospace;"><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheNNX6GpTLYwIEnljg0Smxp-iKkrKfgAoQpEv1KHhiV1jRRhoPV_Z2kYi_k9dYnYqCsKjl4JkzHv9VEHa0PSG3k4BG8NbsXtJoactDYgfdTc_X37YtXcygLNlEvTs1Qa3bHsfI/s1600/globakl_config.JPG"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheNNX6GpTLYwIEnljg0Smxp-iKkrKfgAoQpEv1KHhiV1jRRhoPV_Z2kYi_k9dYnYqCsKjl4JkzHv9VEHa0PSG3k4BG8NbsXtJoactDYgfdTc_X37YtXcygLNlEvTs1Qa3bHsfI/s400/globakl_config.JPG" /></a> </span>
<li><span style="font-family: "courier new", courier, monospace;">Scroll down to MSBuild and click on Add MSBuild </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Give it a name E.g. VSBuild. </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Set the path to your MSBuild executable in the Path to MSBuild field. This will be located in the folder below if you have Visual studio installed. Alternatively you could download the .Net framework and locate the MSBuild executable path in the .Net installation folder. It's important to note that the MSBuild.exe is required even though there is a warning that its only expecting a folder path.C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Msbuild.exe</span></li>
<span style="font-family: "courier new", courier, monospace;"><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8MFpSIemPNbnvXr32OcW7uEH4ASy1zd8gG_4L9irP5n-F_7WM0-uZ4Kwexf7ZflN9VomeS1WaricsidhPYBtv1pmXR_EPXBKkrYM847YAcbNpjYdtu1pgK4dgCXx77wRuow-Y/s1600/msBuild_config.JPG"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8MFpSIemPNbnvXr32OcW7uEH4ASy1zd8gG_4L9irP5n-F_7WM0-uZ4Kwexf7ZflN9VomeS1WaricsidhPYBtv1pmXR_EPXBKkrYM847YAcbNpjYdtu1pgK4dgCXx77wRuow-Y/s400/msBuild_config.JPG" /></a> </span>
<li><span style="font-family: "courier new", courier, monospace;">Scroll down and click Apply and Save</span></li>
</ol>
<h3 style="text-align: left;">
</h3>
<h2 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace; font-size: small;">Creating your first Jenkins project</span></h2>
<div style="text-align: left;">
<span style="font-family: "courier new", courier, monospace; font-size: small; font-weight: normal;">In this step you will learn how to setup a Jenkins project to build a .Net Core application.</span></div>
<ol style="text-align: left;">
<li><span style="font-family: "courier new", courier, monospace;">Click on the New Item in the left hand navigation. </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Enter an item name and select Freestyle project. </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Scroll down and click OK. </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Under Source Code Management choose GIT.<br /><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJDXMOHzufmRjfk8jiQAbrwCBj9BXo5jkQarESJuxuafHjDgxxwHsu0b6WhEEIkPAshK5EygFxoIzKiNLMr4ZqV82rJTRX1gd9mjK1i7YGPxiEUC0rWOguIC8OxQwEdqoUf1d4/s1600/projectSourceCodeConfig.JPG" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="428" data-original-width="704" height="242" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJDXMOHzufmRjfk8jiQAbrwCBj9BXo5jkQarESJuxuafHjDgxxwHsu0b6WhEEIkPAshK5EygFxoIzKiNLMr4ZqV82rJTRX1gd9mjK1i7YGPxiEUC0rWOguIC8OxQwEdqoUf1d4/s400/projectSourceCodeConfig.JPG" width="400" /></a></span></li>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<li><span style="font-family: "courier new", courier, monospace;">Enter the GIT repo url in Repository URL. Based on the type of repository you may need to provide appropriate credentials. </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Scroll down to Build and click on Add build step.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Choose the MSBuild Version you setup in Jenkin setup above Choose the MSBuild Build File as a full path to the Solution/Project file.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">For Command Line Arguments add /p:Configuration=DEBUG or /p:Configuration=RELEASE depending on what you intend to build. </span></li>
</ol>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjajMbW2EqyMk8MlQzrQNx4u8uCXjG3F8ua31S2u60hgjxcZ4bn25_lg290dvJ1J5c6JatDdSjPp2gmcuak9XohsxKn5cs-Z9-Y07UMlOgW5bDZwxUwYvYtjcd9wSCNxtOyjhT-/s1600/Build.JPG" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: "courier new", courier, monospace;"><img border="0" data-original-height="301" data-original-width="710" height="168" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjajMbW2EqyMk8MlQzrQNx4u8uCXjG3F8ua31S2u60hgjxcZ4bn25_lg290dvJ1J5c6JatDdSjPp2gmcuak9XohsxKn5cs-Z9-Y07UMlOgW5bDZwxUwYvYtjcd9wSCNxtOyjhT-/s400/Build.JPG" width="400" /></span></a></div>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<div class="MsoNormal">
<span style="font-family: "courier new", courier, monospace;">** If you are not checking in the project assets like me. This is part of the .gitignore by default,</span><span style="font-family: "courier new", courier, monospace;"> you will need an additional <br />build step to restore them. </span></div>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<br />
<ol>
<li><span style="font-family: "courier new", courier, monospace;">Click on Add build step.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Choose the MSBuild Version you setup in Jenkin setup above.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">Choose the MSBuild Build File as a full path to the Solution/Project file.</span></li>
<li><span style="font-family: "courier new", courier, monospace;">For Command Line Arguments add /t:restore.</span></li>
</ol>
<span style="font-family: "courier new", courier, monospace;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0p1TOBCS7c-1XifaY7IOggWu08fwj7HY9G3wXupIw1ijQpwFyFlFlstFT9QrsjEKfPJmMkca9Dk5KwNtTVOXGYbciLWeBOvubrt2aj3-7EhUb6DMPHNxcSpO6aVnpHgXMUUSr/s1600/projectBuildConfig.JPG" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="608" data-original-width="714" height="340" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0p1TOBCS7c-1XifaY7IOggWu08fwj7HY9G3wXupIw1ijQpwFyFlFlstFT9QrsjEKfPJmMkca9Dk5KwNtTVOXGYbciLWeBOvubrt2aj3-7EhUb6DMPHNxcSpO6aVnpHgXMUUSr/s400/projectBuildConfig.JPG" width="400" /></a></span><br />
<h3 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace; font-size: small;">Build your project<br /><br /><span style="font-weight: normal;">In this step you obtain Nirvana when you get to build your project.</span></span></h3>
<div>
<ol>
<li><span style="font-family: "courier new", courier, monospace;">Click on the name of your project. </span></li>
<li><span style="font-family: "courier new", courier, monospace;">Then click on<span style="color: #333333; white-space: nowrap;"> </span>Build Now in the left hand side.<br /><br /><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1dssUBcxHhQrJtW1KD56-NCu_vw2Qb-NsZ3ScvH1RpabaFOGeTfKwJqYAQmAg8LAUc_ALKYpLR5_G1rhsgT9UnRtx-LuC-HSkPAlKyek6HPOjTm0qp2vi2a-Y1n_zmyQm_Ydx/s1600/build_now.png" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="227" data-original-width="949" height="95" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1dssUBcxHhQrJtW1KD56-NCu_vw2Qb-NsZ3ScvH1RpabaFOGeTfKwJqYAQmAg8LAUc_ALKYpLR5_G1rhsgT9UnRtx-LuC-HSkPAlKyek6HPOjTm0qp2vi2a-Y1n_zmyQm_Ydx/s400/build_now.png" width="400" /></a></span></li>
<li><span style="font-family: "courier new", courier, monospace;"><span style="font-family: "courier new", courier, monospace;">You should see your project build status now.</span><br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl7ddS1eV6QGqiJpxBaAw-h6GPzEoMpeY84fTtJGZP5dZH9ti_vwcHTpLixtZjkzhnuTKsnQSYE67ulx6-BROIyK3JIFbyU6kD2LZmiYmc_98huTHSY2xV6Hn61lBHAVFsrcmk/s1600/buildhistory.JPG" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="247" data-original-width="939" height="105" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhl7ddS1eV6QGqiJpxBaAw-h6GPzEoMpeY84fTtJGZP5dZH9ti_vwcHTpLixtZjkzhnuTKsnQSYE67ulx6-BROIyK3JIFbyU6kD2LZmiYmc_98huTHSY2xV6Hn61lBHAVFsrcmk/s400/buildhistory.JPG" width="400" /></a></span></li>
</ol>
</div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new", courier, monospace;"><br /></span></div>
<a href="http://anyurl.com" rel="tag" style="display: none;">CodeProject</a>
</div>
Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com1tag:blogger.com,1999:blog-35148818.post-58119377057619300142013-06-18T19:24:00.002-07:002013-06-18T19:24:13.070-07:00MVC Custom Model Binder <div dir="ltr" style="text-align: left;" trbidi="on">
Recently I was looking at an unusual problem where we had to add custom
fields to a view on an ad hoc basis. Not really a problem if it was an
MVC app talking directly to a data source. But mine was over several
REST based services that were talking to a CRM dynamics backend, spread
across multiple domains. Which meant we had to change the data models
through out the system. That's when it occurred to me that we can have
one dedicated field that can store all the ad hoc field data using
JASON or xml and we append it to one of the existing fields. Probably
not the ideal solution, but that's when I came across custom binding for
MVC. I would like to share with you how simple and easy it is to use
custom binding to solve similar issues.<br />
<br />
See here for more <a href="http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder?msg=4591496#xx4591496xx">http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder?msg=4591496#xx4591496xx</a> </div>
Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-89458382914362667232012-02-10T16:33:00.001-08:002020-05-10T23:45:26.743-07:00The Adapter Pattern<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDzd7eNtvml2HSSyQpaO84GNyXVzuHRlRPorBCduAdJfiCEfO8CmPzukzycd83pzw67BWiEpVtoOyleyJeml55AtOzndBp0aFjw4FXY1rFIXHrULQ6R6Sj1u7O9HnSxPz7oHmh/s1600/universal_converter_box.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="559" data-original-width="737" height="242" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDzd7eNtvml2HSSyQpaO84GNyXVzuHRlRPorBCduAdJfiCEfO8CmPzukzycd83pzw67BWiEpVtoOyleyJeml55AtOzndBp0aFjw4FXY1rFIXHrULQ6R6Sj1u7O9HnSxPz7oHmh/s320/universal_converter_box.png" width="320" /></a></div>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;">The adapter pattern is used to match one interface with another. This is a useful pattern to use when implementing</span><span style="font-family: "courier new", courier, monospace;"> Interface Segregation Principles.</span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><b>Problem </b> </span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span><span style="font-family: "courier new", courier, monospace;">I'm building a digital simulator called </span><span style="font-family: "courier new", courier, monospace;">FarmAnimals </span><span style="font-family: "courier new", courier, monospace;">that can build farm animals with many capabilities. Some of these capabilities I wish to implement on my own and for others Id like to use an external source like a library. I have found a library called ZooLib that offers animal capabilities that I'd like to use. But the ZooLib library classes implement an interface that is very different to the one implemented by my classes in FarmAnimals. Thankfully there's an Adapter pattern to the rescue!</span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><b>Analysis </b></span><br />
<span style="font-family: "courier new", courier, monospace;"><b><br /></b>I've decided to create a Goat using my FarmAnimals application. FarmAnimals contains an Interface </span><span style="font-family: "courier new", courier, monospace;">IFarmAnimal which defines a method called </span><span style="font-family: "courier new", courier, monospace;">getDietaryType(). All animals need to eat is a given and so </span><span style="font-family: "courier new", courier, monospace;">I expect all my animal classes to implement </span><span style="font-family: "courier new", courier, monospace;">IFarmAnimal</span><span style="font-family: "courier new", courier, monospace;">.</span><br />
<br />
<span style="font-family: "courier new", courier, monospace;">The ZooLib library has a useful function </span><span style="font-family: "courier new", courier, monospace;">getFamily() </span><span style="font-family: "courier new", courier, monospace;">that can help identify the family my animals belong to. </span><span style="font-family: "courier new", courier, monospace;">I want to use this feature in FarmAnimals. But my Goat class implements an IFarmAnimal interface and the getFamily which is part of the MountainGoat class in ZooAnimals implements an interface called IZooAnimals which is a completely different interface.</span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;"><b>Solution </b></span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<span style="font-family: "courier new", courier, monospace;">Create an abstract base class called ZooAnimalAdapter that implements both interfaces. It delegates all my IFarmAnimal operations to the concrete subclasses in this case Goat and builds wrappers around the IFarmAnimal which are also available as part of my Goat class. </span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span>
<b><span style="font-family: "courier new", courier, monospace;">Code</span></b><br />
<b><span style="font-family: "courier new", courier, monospace;"><br /></span></b>
<br />
<h4>
<span style="font-family: "courier new", courier, monospace;">Contents of the ZooLib library</span></h4>
<span style="font-family: "courier new", courier, monospace;">public interface IZooAnimal</span><br />
<span style="font-family: "courier new", courier, monospace;">{</span><br />
<span style="font-family: "courier new", courier, monospace;"> void getFamily();</span><br />
<span style="font-family: "courier new", courier, monospace;">}</span><br />
<span style="font-family: "courier new", courier, monospace;"><br /></span><span style="font-family: "courier new", courier, monospace;">public class MountainGoat : IZooAnimal</span><br />
<span style="font-family: "courier new", courier, monospace;">{</span><br />
<span style="font-family: "courier new", courier, monospace;"> public void getFamily()</span><br />
<span style="font-family: "courier new", courier, monospace;"> {</span><br />
<span style="font-family: "courier new", courier, monospace;"> Console.WriteLine("Family : Bovidae");</span><br />
<span style="font-family: "courier new", courier, monospace;"> }</span><br />
<span style="font-family: "courier new", courier, monospace;">}</span><br />
<h4 style="text-align: left;">
<span style="font-family: "courier new", courier, monospace;">Contents of my FarmAnimal application</span></h4>
<span style="font-family: "courier new", courier, monospace;">public interface IFarmAnimal<br />{<br /> void getDietaryType();<br />}</span><br />
<div>
<span style="font-family: "courier new", courier, monospace;"><br />public abstract class ZooAnimalAdapter:IFarmAnimal,IZooAnimal<br />{<br /> public abstract void getDietaryType();<br /> public void getFamily()<br /> {<br /> IZooAnimal za = new MountainGoat();<br /> za.getFamily();<br /> }<br />}<br /><br /> public class Goat: ZooAnimalAdapter <br />{<br /> public void getDietaryType()<br /> {<br /> Console.WriteLine("Diet: Herbivor");<br /> }<br />}</span></div>
<div>
<span style="font-family: "courier new", courier, monospace;">public class Program</span></div>
<div>
<span style="font-family: "courier new", courier, monospace;">{<br /> static void Main(string[] args) <br /> { <br /> Goat g = new Goat(); <br /> g.getFamily(); <br /> g.getDietaryType(); <br /> Console.ReadKey(); <br /> } <br />} </span></div>
<a href="http://anyurl.com" rel="tag" style="display: none;">CodeProject</a>
</div>
Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-88919266596831434532012-01-19T17:15:00.000-08:002012-01-22T14:51:04.651-08:00Design PatternsIve been saying to myself that I will set aside time, to write a tutorial on design patterns. I think that time has come and here is what I have come up with.<br />
<br />
<b>What are design patterns?</b><br />
<br />
In the object oriented world the solutions/designs we create to overcome problems or requirements have probably been done before. Over time these solutions/designs have evolved into repeatable set of patterns. Some of these patterns have been identified and classified into one of the three types of design patterns.<br />
<br />
However there is no hard and fast rules for these patterns and you might have already been using this patterns unconsciously. However there are a few patterns that are well documented and widely used. Even these patterns might have several variants and be customiz3d to solve specific problems. <br />
<br />
<br />
<b>What are the types of design patterns? How man are there in total? </b><br />
<br />
There are three main types of design patterns. There are several design patterns and their variants in use, here are a few that have been well documented and frequently used. <br />
<br />
<b> Creational Patterns</b><br />
Abstract Factory <br />
Builder <br />
Factory Method <br />
Prototype <br />
Singleton <br />
<br />
<b> Structural Patterns</b><br />
Adapter <br />
Bridge <br />
Composite <br />
Decorator <br />
Facade <br />
Flyweight <br />
Proxy <br />
<br />
<b> Behavioral Patterns</b><br />
Chain of Resp. <br />
Command <br />
Interpreter <br />
Iterator <br />
Mediator <br />
Memento <br />
Observer <br />
State <br />
Strategy <br />
Template Method <br />
Visitor <br />
<br />
<b>Why do we need design patterns? Are they really useful?</b><br />
<br />
Design patterns help implement the object oriented concepts. They also provide us with a standard set of available and well researched solutions to familiar software design problems.<br />
<br />
"Are they really useful" is a hard question to answer and also quite opinionated, but My understanding is Yes they are useful. My justification for this is, by implementing design patterns in the long run you will create code that is standardized and understandable by all. Further there is no point in re inventing the wheel, especially with less efficient code and designs. <br />
<br />
<b>How do I know when to apply which pattern?</b><br />
<br />
Unfortunately like all things abstract, this is the hardest to master. We can only learn when to apply a certain design pattern from experience and prior learning. As mentioned before there is no limitations to the number of patterns. There are a number of variants of any given pattern and every solution would require some modifications to even a well defined pattern.<br />
<br />
However one method I have used to learn "When to use a pattern" is by looking at code written by others. Try and identify what patterns they have used to solve a particular design problem, this is your best bet in learning when to use a particular design pattern.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-55398561901825070562011-04-05T17:51:00.001-07:002011-04-05T17:53:12.519-07:00Implementing WCF .Net 4 on the windows 2008 R2 serverI recently came across one of those annoying deployment issues when I deployed my WCF project to a windows 2008 R2 server. <br />
<br />
After much googling and scratching my head till I could feel my scalp going raw, I figured out that WCF activation needs to be added as a feature through the Server Manager.<br />
<br />
When I actually figured that part out I was caught on a another mind-boggling experience. .Net framework 4 was not available via the Server Manager. But I look at the WINDOWS\Microsoft.NET\Framework folder and there was .Net 4. <br />
Anyhow I figured this out too, you need to have the Microsoft .NET Framework 4 (Standalone Installer) for Server Core <br />
<br />
So things to watch out for <br />
<br />
1) .Net 4 (Server Core) edition is installed on the windows 2008 server<br />
2) Make sure that you add the feature WCF Activation is added to the server.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-12777084005069805972011-03-21T16:10:00.000-07:002011-03-21T16:10:50.954-07:00Snippet editorVS2010 doesn't come with inbuilt snippet editor but here is an excellent alternative <br />
<br />
http://snippeteditor.codeplex.com/Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-78161823738311770202011-03-03T15:34:00.000-08:002011-03-03T15:34:54.897-08:00Here is a quick reference on using XDocument<div dir="ltr" style="text-align: left;" trbidi="on">Here is a quick reference on using XDocument<br />
<br />
<jobdetails><br />
<jobnumber>5<jobnumber><br />
</jobnumber></jobnumber></jobdetails><br />
<br />
1) Loading an XML file into a Xdocument object <br />
<br />
XDocument xdocSource = XDocument.Parse(Xmltmp);<br />
<br />
2) Loading a string directly into a Xdocument object<br />
<br />
XDocument xdocSource = XDocument.Load(XMLReader xr);<br />
<br />
3) To get to a particular element you need to follow the path<br />
<br />
XmlElement xe = xdocSource.Descendants(soap + "JobDetails").FirstOrDefault().Element(soap + "JobNumber");<br />
<br />
4) To get the value of the above use the following <br />
string tmp = xdocSource.Descendants(soap + "JobDetails").FirstOrDefault().Element(soap + "JobNumber").Value;<br />
<br />
<br />
<a href="http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=1495569" rel="tag" style="display: none;">CodeProject</a></div>Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-47817562558975953602010-08-28T17:27:00.000-07:002010-08-28T18:40:36.768-07:00Code Review : whats all the fuss about?"Code review" for those of you who are not familiar with the terminology it is the practice of reviewing code written by another developer/programmer in a development team. Here are my brief thoughts on how and what a code review process is and should be about.<br />
<br />
Is there an emotional side to it?<br />
<br />
Much too often code reviews are sought with distress. This can easily be attributed to a lack of understanding as to the purpose of a code review. A code review policy in a team should be aimed at creating and maintaining a consistent, structured and standardized code base. Nothing more nothing less.<br />
<br />
So we got the essence of it now what?<br />
<br />
To achieve all of the above we need to come up with a list of process's and tools that can help achieve our goals. To get you started here are a few <br />
<br />
1. Determine when you will conduct your code review, is it after each development iteration, is it after testing? <br />
<br />
2. To achieve standardization, create a guide and update it regularly with necessary checks you want to cover with your code review. Here are a few to start with. <br />
<br />
a. Check for hard coded data.<br />
b. Check for performance bottlenecks IO operations, db connections etc. <br />
c. Check for adherence to naming convention. <br />
<br />
3. Train your team in your code review process <br />
<br />
4. Regularly discuss your code review policy with all involved. Remember code review is not a solitary exercise and your code review policy will have a wider acceptance when everyone's opinions/ideas are heard.<br />
<br />
5. The code review exercise is not one just for your Team Leads. Anyone and everyone in the team should be have the ability to conduct a code review. This will help inculcate your coding standards across the team. <br />
<br />
Tools <br />
<br />
Here are some tools that can help with your code review objectives <br />
<br />
1. A good code repository that can help you identify the code required to be reviewed<br />
<br />
2. A good diff tool that can highlight the differences between the latest and previous versions of your code.<br />
<br />
3. A good refactoring tool that will automate most of your code review process<br />
<br />
Conclusion <br />
<br />
Id like to end my thoughts on the code review process with the following <br />
<br />
For the reviewer : Focus on the code not the developer! This is an opportunity to learn for you too, you can learn how to do it or how not to do it so keep an open mind. <br />
<br />
For the developer : This is not a critic of your your coding skills but an exercise to improve the code in the code base.<br />
<br />
<br />
<a href="http://anyurl.com" style="display:none" rel="tag">CodeProject</a>Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-78172267521936890632010-08-25T17:50:00.000-07:002010-08-25T22:09:33.242-07:00Centralized asynchronous logging<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsCeM0sQ10MUl6Cj1xV6hDsRntLVwueA-shZWRp6XkHYj_O9wKmAb-9i2FvHZf87yORteS9gkgoooGXB3iXtAWvp6WJ2Gfxh_Lgcp7Le1H-y5c4vgasLQy0vV5NeRU2NyS37J5/s1600/Logging.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br />
</a></div>If you’ve been working long enough in application development like me I’m sure your aware of the benefits of incorporating a good logging mechanism within your application. <br />
<br />
Recently I was designing an SOA based enterprise platform. There were a number of services and applications that required some serious logging and honestly I wouldn't have given it much thought and just included some third party component such as log4net or Microsoft Enterprise Application Logging Block that would log data to a text file within each application or service.<br />
<br />
But I had a second thought when re-factoring my architecture and being a firm believer of consolidating and centralizing application development and anything related that perfects the reuse of code, process or data. I thought there must be a better way to implement logging?<br />
<br />
So I made a list of woes I had experienced from previous logging implementations. The most significant thing that hit me was the difficulty involved in accessing logging information when things went wrong. You had to locate the application, the server then scan through a huge massive log file and you had to be the one who has access to this file. That meant a significant down time even if the solution was a tiny change in the config. <br />
<br />
<br />
So the most possible solution was centralized logging, but wait what about performance? logging to a flat file surely meant a hit on performance. So lets have centralized asynchronous logging is what I came up with<br />
<br />
Centralized Asynchronous Logging. <br />
<br />
I used Enterprise Application Logging Distributed Services to implement Centralized Asynchronous Logging.<br />
Briefly you have various applications on different servers log to a Message queue and run a windows service that will read from the queue and populate a database. You then create a log viewer application which you can use as to analysis the logs . With this set up you can implement a number of useful process to deal with logging. <br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsCeM0sQ10MUl6Cj1xV6hDsRntLVwueA-shZWRp6XkHYj_O9wKmAb-9i2FvHZf87yORteS9gkgoooGXB3iXtAWvp6WJ2Gfxh_Lgcp7Le1H-y5c4vgasLQy0vV5NeRU2NyS37J5/s1600/Logging.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsCeM0sQ10MUl6Cj1xV6hDsRntLVwueA-shZWRp6XkHYj_O9wKmAb-9i2FvHZf87yORteS9gkgoooGXB3iXtAWvp6WJ2Gfxh_Lgcp7Le1H-y5c4vgasLQy0vV5NeRU2NyS37J5/s320/Logging.jpg" /></a></div>Benefits <br />
<br />
* No more dealing with large log files.<br />
* Template for the type of logs you collect will make your logging a lot more uniform in content and easy to read <br />
* Clean and maintain your logs efficiently and effectively.<br />
* Search and query your logs. <br />
* Having a central logging repository enables you to report and analysis the health of your applications.<br />
* Developers need not be the only ones required to analyze a problem, other levels can access this information via the web viewer application.<br />
<br />
For more information on how to implement the above have a look at the following url <br />
<br />
See the following link http://msdn.microsoft.com/en-us/library/ff647847.aspx<br />
<br />
<a href="http://anyurl.com" rel="tag" style="display:none" >CodeProject</a>Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-37507316553653177052010-08-19T16:23:00.000-07:002010-08-21T08:54:34.377-07:00<span style="font-weight:bold;">Writing an extensible polling service</span><br /><br />In an enterprise system environment you often come across the need to execute asynchronous processes/tasks. The most common approach is to create a windows service that will poll for tasks and execute them. However this leads to the creation of a number of services over time.<br /><br />Managing multiple services and ensuring that they are all up and running can be huge task by itself. Not to forget the development time involved in rewriting the same polling service code every time. This led me to build a generic polling service that could run any task in an orderly manner and be flexible enough to allow for the addition of new tasks with relative ease in the future. <br /><br />If your interested click <a href="http://www.codeproject.com/KB/dotnet/ExtensiblePollingService.aspx">here </a>is the article with the source codeRitesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-29037473598017915322010-07-28T17:03:00.001-07:002010-08-18T17:13:53.670-07:00WCF Interop Services<br /><br /><br />I was trying to architect and implement an Enterprise Service bus using WCF at work. One of the key components of this Service bus was to consume vendor services that were on non (.Net) platforms. This had been previously implemented using web services, but trying to implement the same using WCF was a whole new ball game. The two main challenges I came across were establishing a secure binding and getting the data types to match.<br /><br />Challenge one<br /><br />Get the binding to work correctly. The vendor documentation was quiet limiting with regards to establishing a connection. So after a lot of mucking around and guesses I went to basics and accessed service directly via code. This provided me a much better insight into the expected configuration.<br /><br /><br />Challenge two<br /><br />Get the data types to match. This meant generating the data objects from their data schema and then converting it back into XML that can be passed back to the service. Fortunately got the schema (xsd) files for the objects used in the Java web service from the vendor.<br /><br />After several attempts of trying to generate data-contracts from the XSD and trying to serialize them back to valid XML that I could pass back to the java web service using different combinations of svcutil, xsd.exe, XmlSerializer Class and DataContractSerializer Class. The one that worked was to use xsd.exe to create my data objects and XML Serializer to convert it back to XML to pass back<br />to the Java web service.<br /><br />Several attempts to use SVCUtil were in vain, I think it has a long way to go before becoming a mature tool like xsd.exe especially when it comes down to generating data objects from datacontracts. On the other hand the xsd.exe seemed to be more robust and well documented. Following is the line I used in a batch file that I hook to VS pre-build step. <br /><br />xsd -c -l:c# -n:[Namespace] [schemafile.xsd]<br /><br />* Each schema requires to be run on its own I One thing that I did notice was that<br />* One thing I noticed when using Svcutil is that it requires you specify the included/linked schema in the same executing line. Else you will end up with fields(appends a field to the property) being serialized instead of properties. The best option was to use a wild card.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-49819730220651081252010-06-24T16:43:00.000-07:002010-06-24T16:44:23.505-07:00<span style="font-weight: bold;">View the structure of the GAC folder do the following</span><br /><br />start > run > cmd<br />type : "cd\windows\assembly"<br />type: "attrib -r -h -s desktop.ini"<br />type: "ren desktop.ini desktop.bak"<br /><br />Open the GAC folder normally in your explorer, and you can see the actual structure.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-55178874606475029862009-12-18T15:14:00.000-08:002010-08-23T17:54:40.631-07:00<span style="font-weight: bold;">Configuring ASP session state on SQL server </span><br />
<br />
This is a brief to the point note on how to setup ASP session state to use SQL server to store session information.<br />
<br />
There are 2 steps setting up the ASPState database and the configuration setting in your web.config.<br />
<br />
For some reason you cannot run the sql file directly cant remember what the issue is, but you need to use ASP_RegSQl.exe from a commmand prompt. The script file and the aspregsql exe are located here C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727<br />
<br />
<br />
<span style="font-weight: bold;">Steps</span><br />
<br />
1) Open a command propmt and locate the following path C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 based on your Os version and .Net version<br />
<br />
2) Use the following statement<br />
<br />
a. Using default ASPState database and Sql security<br />
<br />
<span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;">aspnet_regsql -S serverName -U UserName -P Password </span></span></span></span></span><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;">-ssadd</span></span></span></span></span> <span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"> -sstype p</span></span></span></span></span><br />
<span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><br />
</span></span></span></span></span>b. Using default ASPState database and windows security<br />
<br />
<span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;">aspnet_regsql -S serverName -E </span></span></span></span></span><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;">-ssadd</span></span></span></span></span> <span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"> -sstype p</span></span></span></span></span><br />
<br />
c. Using custom database and Sql security<br />
<br />
<span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;">aspnet_regsql -d TableName -S serverName </span></span></span></span></span><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"> -U UserName -P Password </span></span></span></span></span><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;">-ssadd</span></span></span></span></span> <span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"><span style="color: rgb(255, 0, 0);"><span style="font-family:Verdana,sans-serif;"><span style="font-size:85%;"> -sstype c</span></span></span></span></span><br />
<br />
<span class="input">t -</span> Stores session data in the SQL Server tempdb database. This is the default. If you store session data in the tempdb database, the session data is lost if SQL Server is restarted.<p><span><span class="input">p - </span></span>Stores session data in the ASPState database instead of in the tempdb database.</p><p><span><span class="input">c </span></span>- Stores session data in a custom database. If you specify the <span><span class="input">c</span></span> option, you must also include the name of the custom database using the <span><span class="input">-d</span></span> option.</p><br />
<br />
3) In your configuration file<br />
<br />
a) Using default Sql security<br />
<br />
<sessionstate mode="SQLServer" timeout="20" allowcustomsqldatabase="true" sqlconnectionstring="Data Source=Server;User ID=UserID;Password=Password;" cookieless="false"><br />
<br />
<br />
b) Using default windows security<br />
<br />
<sessionstate mode="SQLServer" timeout="20" allowcustomsqldatabase="true" sqlconnectionstring="Data Source=Server;Integrated-Security=SSPI;" cookieless="false"><br />
<br />
c) Custom database name<br />
<br />
<sessionstate mode="SQLServer" timeout="20" allowcustomsqldatabase="true" sqlconnectionstring="Data Source=Server;Initial Catalog=tablename;User ID=UserID;Password=Password;" cookieless="false"><br />
<br />
<span style="font-weight: bold;">Using SQL server to store ASP Session info in a Web Farm<br />
<br />
</span><span>Things to watch for when using SQL to store the session info in a web farm scenario</span><span style="font-weight: bold;"><br />
<br />
</span><span>1) The machine key between the servers needs to be the same as AspState Session info is encrypted using the machine key.</span><span style="font-weight: bold;"><br />
<br />
</span><span>2) </span><span>The application path to your websites on all machines needs to be consistent as well.</span><span style="font-weight: bold;"><br />
<br />
Other issues<br />
<br />
</span><span> Since the session information needs to be converted from memory to a more persist-able form such as text. Out of memory session stores serialize and deserialize the session, you need to ensure that all complex/custom objects that </span><span>you wish to store in a session are serializable</span><span style="font-weight: bold;">. </span><span>If you come across any such object that you cannot serialize by using the serialization attribute you can always serialize it programatically</span><span style="font-weight: bold;"><br />
</span><a href="http://anyurl.com" style="display:none" rel="tag">CodeProject</a>Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-29249237073641343462009-12-16T14:06:00.001-08:002010-08-24T18:31:07.369-07:00Code Management on a budget<span style="font-weight: bold;">Code management on a budget in a team development environment</span><br />
<br />
<a href="http://anyurl.com/" rel="tag" style="display: none;">CodeProject</a><br />
<br />
If you've done any development in a team environment, I'm sure your aware of the pains associated with such development.<br />
<br />
You spend a whole day debugging and fixing conflicts only to come in the next day and find your code broken again. You decide enough is enough and walk over to your teammate and ask him for an explanation. He shows you that his version compiles and runs just fine. You walk back to your terminal scratching your head thoroughly frustrated and spend the next six hours locating an environment configuration variable that has been breaking your code.<br />
<br />
A code base can grow exponentially in a team environment, getting bloated with buggy code, conflicts, configuration issues, environment issues etc. A huge amount of time is wasted in debugging and resolving environment and configuration issues, increasing your projects time and reducing productive coding.<br />
<br />
So what's the solution? While it is a complex problem with a number of parameters that could go wrong and no perfect solution. Implementing an efficient code management process that suits your team's development needs and environment may not be perfect but could still make things a little less frustrating and improve your overall efficiency.<br />
<br />
Based on how your team operates you'll need to find the right balance between a technical or managerial solution. So below is a rough guide on what is available out there and by no means exhaustive. <br />
<br />
<span style="font-weight: bold;">Tools</span><br />
<br />
Like they say the right tools get the job done. Here are a few tools that can aid in managing your code.<br />
<br />
<ol><li>Firstly you need some kind of a code repository(I prefer SVN). There are a host of other commercial and open source products to choose from. Visual studio, Telelogic Synergy, CVS etc</li>
<li>You need a good diff and merge tool that can be used with the code repository (DiffMerge seems like a nice tool to do this, again a whole lot of them available.<br />
</li>
<li>If your projects use a database then you should also get some database diff tools. I use SQLDiff.<br />
</li>
<li>Re-factoring tools will help keep your code in good shape a nice add on to Visual studio is ReSharper </li>
<li>Logging is a really helpful feature to have in any project. Trying to debug a projects for every error that occurs in a team environment can make it extremely frustrating and wasteful activity. Logging can gives you the ease of locating the errors at a glance. Log4Net is an open source library that you can use. It allows you to set and change the severity level in your project and allows you to choose a logging medium like a flat file, event logs or a database. </li>
<li>A continuous build machine can greatly help in team development, users have access to different builds. Each build can be configured to show the diff between a baseline(stable project) making it easy to compare broken versions of the build.</li>
</ol><span style="font-weight: bold;">Process</span><br />
<br />
All the tools in the world cannot help without some degree of good process management. Like any other management practice it requires planning, organizing, training and controlling to improve productivity.<br />
<br />
<ol><li>Come up with a plan on how to structure/organize, share and store your code base. Review this plan periodically and update it to suite your changing needs. </li>
<li>Things to consider in your plan naming conventions, integration schedules/policies, consideration for environment differences, deployment practices, release management.</li>
<li>Documentation is key, convert your plan into policies,rules, guides for the team. This will also ensure that information on your process is readily available to the team. </li>
<li>Train the team, everyone needs to be on the same wave length in order to be effective, so train everyone on the team.</li>
</ol><br />
Ive only outlined a few possibilities to better code management in a .Net team environment. Get creative, use your imagination and get everyone involved to come up with the right tools and techniques for your team.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-91530364837185159162009-12-11T10:50:00.000-08:002009-12-17T10:15:10.518-08:00<span style="font-weight: bold;">BCP for bulk inserts into SQL server (SQL 2005) </span><br /><br />BCP is a powerful utility to bulk insert data into SQL server. You can use this from the command line thus can be incorporated into a batch file.<br /><br />I used the format file approach to using BCP. You can generate the format file using the following command line<br />bcp [CatalogName].[Owner].[TableName] nul -m 10 -f [FormatFileName].fmt -S [ServerName] -T<br /><br /><br />Finally figured out how to remove the double quotes in a CSV data import file. You will need to change the BCP format file to achieve this. Below is the BCP format file required to do this.<br /><br />9.0<br />4<br />1 SQLCHAR 0 0 "\"" 0 x Latin1_General_CI_AS<br />2 SQLCHAR 0 50 "\",\"" 1 column1 Latin1_General_CS_AS<br />3 SQLCHAR 0 4 "\",\"" 2 column2 Latin1_General_CS_AS<br />4 SQLCHAR 0 300 "\"\r\n" 3 column3 Latin1_General_CS_AS<br /><br />So the trick is to add an extra fake first row in the BCP format file to strip the very first double quote in the data row of your CSV file. The fake column x has a max data size of 0.<br /><br />Also you must not have spaces between the rows of the format file.<br /><br /><br />To execute the BCP use the following command line<br /><br />bcp [CatalogName].[Owner].[TableName] in [ImportDataFileName].CSV -f [FormatFileName].fmt -S [ServerName] -U [UserName -P [Password]<br /><br />Make sure paths to all your files are relative to where the BCP is located. BCP is usually found in the following location if you have SQL 2005 C:\Program Files\Microsoft SQL Server\90\Tools\BinnRitesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-32723031267915700562009-12-10T23:39:00.000-08:002009-12-11T00:06:27.179-08:00<span style="font-weight:bold;">Batch files for automating data feeds </span><br /><br />Today I had to come up with a system/process that could retrieve data from a web site after logging in, retrieve the data and then upload the data into a SQL database on a daily basis.<br /><br />Instead of building a complex application that could do it all. I decided on writing a batch file that combined a custom command line download utility, an unzip utility and SQL BCP utility to download and update the data file. <br /><br />This approach is quick and simple. Plus most routine data processing tasks require some amount of maintenance and updating from time to time. Using a batch file makes it relatively easy to access the process and manage any changes in the future.<br /><br />However the interesting portion of the whole exercise was developing the custom download tool that could login to a website and download the required file/s. Its pretty simple actually what you need to do is simulate a form post using the Http Request class and stream the response to a file on your local server/machine.<br /><br />While this is pretty simple it got me thinking about how you can start interfacing different web applications using this method. I'm sure most applications expose some kind of web service to do this but for those that don't and require some kind of authentication this approach opens up a host of possibilities.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-20808786510715983522009-12-09T15:02:00.000-08:002009-12-17T10:30:17.585-08:00<span style="font-weight: bold;">Using a Mac Book Pro for .Net development </span><br /><br />Most of my work involves .Net development so I needed a windows based laptop. I had narrowed down my choice to a Lenovo Thinkpad X series and Mac Book Pro simply because of their build quality, having a one year old running wild teaches you to appreciate some of the finer elements of a laptop I guess. However the Mac Book Pro offered so much more for about 500$ less.<br /><br />I really like the magnetic power cord (forgetting that the power cord is still plugged to the wall problem has disappeared now) and illuminated keyboard, even the aluminum casing and illuminated apple logo makes it a real sexy beast. There still are some draw backs, you will need a copy of windows to start with and a Mouse! Even though the touch pad is click-able its rather awkward for windows use.<br /><br />So come Thanks Giving sales and I just had to have one for 200$ less than the normal price.<br /><br />Soon I got down to the fun part of installing windows vista on it. I must say that the install went pretty smoothly. But once I booted into Vista I found that most drivers were missing, I couldn't even get the networking to work :(<br /><br />So whats the solution, just use the Leopard DVD that ships with the Mac book pro it has all the drivers in it. I had to do a bit of searching around before I figured it out. Here's a <a href="http://illshare.wordpress.com/2008/10/05/windows-vista-on-macbook-pro-apple/">link</a> on how to install Vista on your Mac Book Pro.<br /><br />So far Ive installed VS2008 and SQL Express on it and I haven't come across anything weird yet but I haven't actually gotten any work done on it yet, I'm sure Ill get down to it sooner or later.<br /><br />Between I have been having fun with Mac Os too. So its been fun trying to do somethings a little different. More on my dev experiences on the Mac Book Pro soon...<br /><br /><br /><span style="font-weight: bold;">Update - 17 Dec 2009</span><br /><br />So I finally got down to setting up an existing project on the Mac Book Pro. Ran the build and voila it works like a charm! I can see my web app in Firefox. I'm so excited now its just as fast as my windows based desktop with 4 gigs of DDR and Quad core Phenom processor. Id be interested to hear from anyone who has found any issues with this kind of dev setup.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-45140126903692007062009-12-08T06:58:00.000-08:002009-12-08T07:01:28.619-08:00I came across another OR mapping tool having used LLBGen and Nhibernate this was somehow quite refreshing Subsonic by Rob Conery. Its really simple to use at least version 3.0 and you can do all the development within the VS IDE which is awesome. Really like the .tt code generation templates that you can build from within Visual Studio<br />-Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-76893259784627700192009-05-11T06:22:00.000-07:002009-05-11T06:42:13.615-07:00Spring is here and I have been trying to get as much fresh air and sun as possible. <br /><br />The code camp was very interesting got a peek at f# and the asynchronous programming model that improves performance greatly, but couldn't the f# constructs be incorporated into the C# language. As a developer I could do with not having to learn a whole lot of new syntax don't get me wrong, its fun to learn new stuff but why relearn something that you have already learnt? <br /><br />To illustrate my point we use the "using" keyword in C# to specify libraries we wish to include in our class files. With F# this keyword is now 'open' while this is a trivial example, I do begin to wonder if I am venting too early and before long someone will come up with a standard F# library or framework that is wrapped in C# code?<br /><br />That apart I have been working towards my MCPD certification, its been worth it really and recommend the self paced training kit even if you arent taking up the the Exam.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-43792831364329736452009-04-16T20:08:00.000-07:002009-04-16T20:19:10.306-07:00Ok I must say I am extremely excited and looking forward to the .Net <a href="http://www.torontocodecamp.net/">code camp in Toronto</a> this Saturday. <br /><br />Between I came across this amazing video of <a href="http://www.youtube.com/watch?v=9lp0IWv8QZY">Susan Boyle</a> its a serious wake up call for anyone judging others, never write someone off before their time!<br /><br />That apart I am dissecting Isolated Storage in the .Net Framework here's an article I wrote <a href="http://www.codeproject.com/KB/dotnet/Isolated_Storage.aspx"><span style="font-style:italic;">"Using Isolated Storage to store application user data in the .Net framework "</span></a> and getting a grip on DLinq can be a real B@#$%%#@ until you figure it out! :) <br /><br />So long everyone....Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0tag:blogger.com,1999:blog-35148818.post-8513999409851499702009-04-11T09:30:00.000-07:002009-04-11T12:23:50.216-07:00Attended the talk on Authentication Alternatives for ASP.NET by Colin Bowern in Toronto this week. Interestingly enough I was plagued by the hysteria of having to manage my numerous sets of credentials for a while now. So this is an interesting space to watch out for in the near future. <br /><br /><br />For quite a while I have been interested in understanding the magic behind partial post backs and I finally tried it. Here is the <a href="http://www.codeproject.com/KB/aspnet/PartialPostback.aspx">article </a> I wrote on how to make your own partial post back.Ritesh Rameshhttp://www.blogger.com/profile/02885789308872727660noreply@blogger.com0