Posts

Useful MVA Courses for you on laas

  Please find some useful MVA course below.   Microsoft Iaas Series I: IaaS Fundamentals Microsoft Iaas Series II:  Dive Deep into Networking Storage and Disaster Recovery Scenarios Microsoft Iaas Series III:  Embrace Open Source Technologies to Accelerate and Scale Solutions

Clean Code Practices : Conditions

  It is said “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” so that we have to make sure the code we write should be easily read and understood by the other developers. This post I will be discussing practices we can follow to make our conditional statements clean. In this post I will mention code dirty and clean to you to understand the difference Principles:- Clear Intent Use the right tool Bite-size logic Sometimes code isn’t the answer   1.Use Positive Conditionals When you write conditions always try to use positive conditionals rather using negative because humans are more comfortable in grabbing positive things. Dirty if (!isNotlogggedIn)              {              } see above example it is bit confusing to understand the meaning in first time. Clean if (logggedIn)              {              }   2.Use Ternary operator see the following example first. Dirty ...

Event Initializer in C# 6.0

  Take the following sample class which has a property and an event listener public class Lecturer      {          public string Name { get ; set ; }          public EventHandler < EventArgs > Speaking;        } . In C# 6.0 when you initialize a new object wire up delegate to the event inside the object initialization syntax. Before C# 6.0 it was illegal. static void Main( string [] args)        {            EventHandler < EventArgs > log = (e, o) => Console .WriteLine( "Speaking" );              Lecturer mathsLecturer = new Lecturer            {                Name = "Perera" ,                Speaking += log            };        } Also you can directly assign the lambda to the event as follows. static void Main( string [] args)          {              EventHandler < EventArgs > log = (e, o) => Console .WriteLine( "Speaking" );                Lecturer math...

Dictionary Initializer in C# 6.0

  After C# 3.0 is released we had the ability of initializing lists and dictionaries using following syntax. Dictionary < string , User > userList = new Dictionary < string , User >              {                  { "admin" , new User ( "Sammani" ) },                    { "Guest" , new User ( "Hemal" ) },              }; Here we add the key value pairs inside the “{ }”. In c# 6.0 it has a new syntax as follows which gives more look like working with key value pairs. Dictionary < string , User > userList = new Dictionary < string , User >            {                [ "admin" ]= new User ( "Sammani" ) ,                 [ "Guest" ]= new User ( "Hemal" )            }; you can the number of characters are almost same in both the syntax but in c# 6.0 syntax looks more a like key value pair. This is another syntax sugar added in c# 6.0. Happy Codi...

Primary Constructor in C# 6.0

  In my previous post I explain about Auto Property Initializer in C# 6.0. Now lets see another new feature in C# 6.0 which is Primary constructor. First we will see the following code. public class Country {      public string Name { get ; private set ; }        public Country( string name)          {          Name = name;          }   } here we have a class which has a property with a private setter and assigning the value for the name property inside the constructor. Now in C# 6.0 you can write the above code in a more simplified manner as follows. public class Country ( string name)      {          public string Name { get ;}=name;          } This is what is called primary constructor in C# 6.0 . Notice the parameters added just right of the class definition. This tells the compiler to add a constructor to the Country class with one parameters: name. So we don’t need to create separate constructor with par...

Auto Property Initializer

  Anyone who have worked with properties in a class must have experienced that it was little annoying that we have to have private fields and assigned if we want to have default values for properties. See the following example which we used to do when we have collection property. public class Country      {          private List < string > _regions = new List < string >();          public List < string > Regions { get { return _regions} set { value = _regions; } }        } but this is no more needed. We can simply write it as below. public class Country      {          public List < string > Regions { get ; set ; }= new List < string >();        } Also this is useful when you want to assign default values for read only fields. Before C# 6.0 public class Country    {        public string Name { get ; }        public Country()        {            this .Name = "Sri Lanka" ; ...

Injecting a service into a view in MVC 6

Image
  Asp.net MVC 6 supports injecting service to view using a class. Example :- When country combo box value get changed , region com Only restriction is that class should be Public Non-nested Non-Abstract 1. Create a Asp.Net Project in Visual Studio 2015     2. Create a folder name services and add a service class. using System; using System.Threading.Tasks;   namespace ViewInjection.Services {      public class EmployeeService      {          public async Task < int > GetEmployeesOnLeaveCount()          {              return await Task .FromResult(5);          }            public async Task < int > GetEmployeesOnSiteCount()          {              return await Task .FromResult(                  10);          }            public async Task < int > GetEmployeesOnWorkCount()          {              return await Task .FromResult(97);          }      } }   2. Create a View to ...