An error was raised by libgit Category =Ssh error: solved git for Visual Studio

Have you come across the error as shown in below image?

clip_image002

You get the above shown error in Visual Studio while pulling changes from the git server. This error causes because you have local changes without the commit. In this scenario the visual Studio does not give very readable error message. Perhaps you may want to use git command for pull for more information on the error.

clip_image003

The git pull command clearly says that either commit the changes or stash them. If you are not sure about the local changes then better option is to stash them. Local changes can be stashed using the git stash command.

clip_image004

After stashing the local changes (you can commit also) run the git pull command to pull the remote changes from the git server. You should not get any error doing pulling the latest changes.

Hope it helps. Happy coding.

Property Initializers in C-Sharp 6.0

In the C-Sharp 3.0 a new feature Automatic property got introduced. It allows us to create a class as a bag of the setters and the getters. You can create a class as follows:


public class Product
    {
        public int Price { get; set; }
        public string Name  { get; set; }

    }

Each property is backed by a backing filed. When you set value of the property, setter gets executed to set the value of the backing field. Now catch is that to create a read only property you have to invoke the setter in the constructor of the class and make set as private

public class Product
    {
        public int Price { get; private set; }
        public string Name  { get; set; }

        public Product()
        {
            Price  = 10;
        }

    }

In the above class definition the Price property is read only and set to the default value 10 and you can set the value of the name property outside the class.

To create a read only property with the default value, we created a private setter and then set the default value in the constructor. In c-sharp 6.0, you can directly create a read only property with default value without invoking the setter. This feature of C# 6.0 is called Property Initializers

Property Initializer allows you to create a property with the default value without invoking the setter. It directly sets value if the backing field.

image

As you see in the above snippet we are setting the default value of Price property as 10. However after creating the instance of the class. Value of price property can be changed. If you wish you can create read only property by removing the setter.

image

Since setter is optional, it is easier to create immutable properties. For your reference source code harnessing Property Initializer is given below:


using System;
namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Product p = new Product
            {
                
                Name = "Pen"

            };

            Console.WriteLine(p.Price);

            Console.ReadKey(true);
        }
    }

    public class Product
    {
        public int Price { get;} = 10; 
        public string Name  { get; set; }
    }
}


We can summarize this post discussing purpose of the auto property initializer. It allows us to create immutable property with the user defined default value. Now to create properties with default values, you don’t have to invoke setter.

Happy coding.

Exception Filters in C-Sharp 6.0

So far we deal with the Exceptions as below. As you see in the following code snippet, we are catching the exception and displaying the error message.


using System;
namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {

            int number = 24;
            try
            {

                int rem = number % 0;
                Console.WriteLine(rem);
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey(true);
        }
    }
}


Challenge in the above snippet is that you cannot apply filter to the exception. Of course you can have if-else statement inside the try catch block but it will cause a particular catch block to get executed and then filter would get applied.

In C# 6.0 a new feature got introduced to apply filter at the catch block. This feature is known as Exception filters. Now a particular catch block can be executed only of the filter is set to true.

image

In the above snippet we are filtering the exception on the various basis like if message contains a particular string then execute a particular catch block or on the basis of the exception source execute a particular catch block.

This feature may be useful in the scenarios in which, you need to perform various actions on if a condition is met in a particular exception type.

For your reference the source code is given below,

using System;
namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {

            int number = 24;
            try
            {

                int rem = number % 0;
                Console.WriteLine(rem);
            }
            catch (DivideByZeroException ex) if (ex.Message.Contains("dj"))
            {
                Console.WriteLine(ex.Message);
            }
            catch(DivideByZeroException ex) if (ex.Source == "demo1")
            {
                Console.WriteLine(ex.Source);
            }

            Console.ReadKey(true);
        }
    }
}


Exception message does not contain any string dj hence the first catch block will not get executed. However second catch block will be executed since source of the exception is demo1. You will get the expected output as below:

image

I hope concept of Exception filter is clear to you now and you will harness C# 6.0 feature in your project.

Happy coding

Child Actions in ASP.NET MVC

Read full post on falafel blog

Child Actions are the action methods which can be invoked within the view. This is used to work with the data in the view, which are not related to the main action method. For example, if you want to create a data driven widget in the view using data that is not related to the main action method, you will need to use the child action method.
In ASP.NET MVC any action can be used as a child action. However, to use an action only as a child action and attribute it with the ChildActionOnly. It will make sure the action is not called by any user request and will only be used in the view. A child action can be created as shown below:

Read full post on falafel blog

Prototypical inheritance in JavaScript

Read full post on falafel blog

Before we can understand prototypical inheritance, let us start with a problem statement. You have a student for whom you need to:

  • Calculate their grade
  • Print the grade

In JavaScript this can be simply implemented as follows:


var studentMarks;
var studentGrade;

function FindGrade(studentMarks) {
    if (studentMarks >= 80) {
        studentGrade = "A";
    }
    else {
        studentGrade = "B";
    }
}

function PrintGrade() {
    console.log("Student Grade is " + studentGrade);
}

FindGrade(60);
PrintGrade();

There are some inherent problems in the above approach:

  • Variables are defined globally
  • Code maintenance is tough as the functionality grows
  • Code debugging is tough
  • Code testing is tough

The major problem in the above code is that neither the PrintGrade nor the FindGrade functions are reusable. Any future requirements in the find grade logic or in the print logic will require a change in the existing code, and that may introduce new bugs and require testing again. As a developer we may not want this.

Let us go ahead and refactor above code as follows:

Read full post on falafel blog

Video : One to one relationship between entities in Entity Framework Code First

Container.cs



namespace EFCodeFirstDemo.EFLib
{
   public class Container
    {
        public int ID { get; set; }
        public string ContainerName { get; set; }

        public virtual  Product product { get; set; }

    }
}


Product.cs


using System.ComponentModel.DataAnnotations;

namespace EFCodeFirstDemo.EFLib
{
   public class Product
    {
   
      // [ForeignKey("Container")]
        public int ID { get; set; }
        public string  ProductName { get; set; }

        public double  ProductPrice { get; set; }

        [Required]
        public virtual Container container { get; set; }
    }
}



EFContext.cs


using System.Data.Entity;

namespace EFCodeFirstDemo.EFLib
{
   public class EFContext : DbContext
    {
       public EFContext()
       {
           Database.SetInitializer<EFContext>(new DropCreateDatabaseIfModelChanges<EFContext>());
       }

       public DbSet<Product> Products { get; set; }
       public DbSet<Container> Containers { get; set; }
    }
}



Check User exists in a Role in ASP.NET Identity 2.0

Read full post on Falafel blog

ASP.NET MVC5 uses Identity 2.0 to manage the users and their roles. There may be scenario to check whether the currently logged in user or any user with the id exists in the given role or not, and checking can be done either in the controller class or in any other class of the application.

Check for the currently logged in user
In a controller you can find whether the currently logged in user exist in a role or not by using User property of the controller class. Type of the User property is IPrincipal and it has a Boolean method IsInRole which takes the role name as input and returns true if the currently logged in user exist in the role.

Read full post on Falafel blog