Showing posts with label (Information technology) IT interview questions. Show all posts
Showing posts with label (Information technology) IT interview questions. Show all posts

Thursday, June 12, 2014

Explain MVC model binders ? ( ASP.NET MVC interview questions)

Model binder maps HTML form elements to the model. It acts like a bridge between HTML UI and MVC model.



Take the below simple HTML form example :-








Now this form needs to fill the below “Customer” class model. If you see the HTML control name they are different from the class property name. For example HTML textbox control name is “CCode” and the class property name is “CustomerCode”.  This mapping code is written in HTML binder classes.

publicclassCustomer
{
publicstring CustomerCode { get; set; }
publicstring CustomerName { get; set; }
}

To create a model binder we need to implement “IModelBinder” interface and mapping code needs to be written in the “BindModel” method as shown in the below code.

publicclassCustomerBinder : IModelBinder
{

publicobject BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
HttpRequestBase request = controllerContext.HttpContext.Request;

string strCustomerCode = request.Form.Get("CCode");
string strCustomerName = request.Form.Get("CName");

returnnewCustomer
            {
                CustomerCode = strCustomerCode,
                CustomerName = strCustomerName
            };
}
}

Now in the action result method we need to use the “ModelBinder” attribute which will attach the binder with the class model.

publicActionResult SubmitCustomer([ModelBinder(typeof(CustomerBinder))]Customer obj)        
{

return View(“DisplayCustomer”);
}

This MVC interview question was provided by questpond which conducts ASP.NET MVC training . In case you are new to MVC you can start from questpond video which is shown below.

Friday, September 6, 2013

What is cyclomatic complexity (C# testing interview questions with answers)?

Cyclomatic complexity helps you measure code complexity.  Higher the code complexity, more it is prone to errors and more important it becomes to unit test that specific code.

Cyclomatic complexity number depends on how many different execution paths your code can execute depending on varying inputs. More the code paths, more the complexity.

For example take the below code. There is nothing complex in the below code, we do not have many execution paths. We just have lots of code lines which does variable initialization.  So the cyclomatic complexity of the below code is “1”.

public void Cancel()
{
            Num1 = 0;
            Num2 = 0;
            Operation = "";
            Pie = 3.14;
            Num5 = 0;
            Operation1 = "";
            Num20 = 0;
            Numx = 0;
            OperationStart = "";
            PieMore = 3.145;
            Num6 = 0;
            Operationnew = "";
}

But now take the example of below code. There are 4 branch conditions in the below code as shown in the below table. So the cyclomatic complexity is “4”.


Input
Output
Path 1
Operation.Length ==0
Exception thrown
Path 2
Operation == “+”
Num1 + Num2
Path 3
Operation == “-“
Num1 – Num2
Path 4
Any other inputs
Num1 * Num2


public int Calculate()
        {
            if (Operation.Length == 0)
            {
                throw new Exception(" Operation not sepecified");
            }
                if (Operation == "+")
                {
                    return Num1 + Num2;
                }
                else if (Operation == "-")
                {
                    return Num1 - Num2;
                }
                else
                {
                    return Num2 * Num1;
                }
                return 0;

        }

To measure code complexity using visual studio , click on Analyze à Calculate code metric for the solution. This feature is available only for VS ultimate editions. Once done you should get the cyclomatic complexity as shown in the below figure. 




Wednesday, August 21, 2013

Fluent interfaces and Method Chaining in C#



Humans are lazy and programmers are humans J . We like things which are simplified and clean. So if you see the evolution of programming, we initially started with functional programming, and evolved towards object oriented programming.

So as functional programmers where we used to remember methods and function names like “Add”, “Subtract”, “Multiply” and “Divide”, in OOP we just need to remember class “Maths” and all functions just pop out when we create the object of the class.

But as object oriented programmers we need to remember there are two worlds in OOP, one who creates the class and second who consumes it by creating the object.

So by following good OOP principles like abstraction, encapsulation etc developer writes the below “Customer” class so that other developers who consume this class should not feel it complicated.

public class Customer
    {
        private string _FullName;

        public string FullName
        {
            get { return _FullName; }
            set { _FullName = value; }
        }

        private DateTime _Dob;

        public DateTime Dob
        {
            get { return _Dob; }
            set { _Dob = value; }
        }

        private string _Address;

        public string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
  
    }

Now the consumer who want to consume our “Customer” class will create the customer object , set properties and invoke methods.  Below is the sample code for the same.


Customer customer = new Customer();
customer.FullName = "Shiv";
customer.Dob = Convert.ToDateTime("1/1/2008");
customer.Address = "Mumbai";

Now let’s zoom on the above consumer code. Is it complicated?, probably not if you are C# developer or a object oriented programmer.

But what if your consumer is a tester who really does understand c# and would like to have more simplified interfaces to invoke your customer class for UNIT testing purpose.

What if you are component seller and you would like to surprise your component consumers with simplified interfaces. You would like to stand different from your competitors.

Welcome to the concept of “Fluent interfaces”.

“Fluent interfaces simplify your object consumption  code by making your code more simple, readable and discoverable.”

So if our component consumers can write object invocation code in simple English sentence like below , that would “ROCK” right.


customer.NameOfCustomer("Shiv")
                .Bornon("12/3/1075")
                .StaysAt("Mumbai");

So now the next thing how to we implement “Fluent” interfaces. That’s done by using “Method chaining”.

“Method chaining” is a common technique where each method returns an object and all these methods can be chained together to form a single statement.

So the above customer class we can wrap in another class (“CustomerFluent”) which will implement method chaining and expose chained methods in a simplified format.

So you can see in the below code methods “NameofCustomer” , “BornOn” accept input s and return backs “CustomerFluent” class.

public  class CustomerFluent
    {
        private Customer obj = new Customer();
        public CustomerFluent NameOfCustomer(string Name)
        {
            obj.FullName = Name;
            return this;
        }
        public CustomerFluent Bornon(string Dob)
        {

            obj.Dob = Convert.ToDateTime(Dob);
            return this;
        }

        public void StaysAt(string Address)
        {
            obj.Address = Address;
           
        }
    }

Now your client code is simple, nice and FLUENT as in the below code.

customer.NameOfCustomer("Shiv")
                .Bornon("12/3/1075")
                .StaysAt("Mumbai");

I have seen four visible uses of fluent interfaces:-

LINQ Queries

var x = context.Users.Select(u => new { u.Id, u.Name });

Unit testing

Assert.AreEqual(obj.Expected,obj1.Actual);

Mock testing

Container.GetMock()
                  .Setup(s => s.Save(new Person()))
                  .Returns(true)
                  .Verifiable();

DSL (Domain specific language)

DSL is a language for simple users who do not understand programming. So they can type something in simple English as shown below. This will be parsed and checked for syntaxes. Later this sentence can be mapped to a internal fluent interface statement.

task "warn if website is not alive":
      every 1.Hour
      starting now
      when WebSite("http://www.questpond.com ").IsAlive is false
      then:notify "admin@example.org", "site down!"

Below is the code of the Fluent interface statement to which the above DSL can map.

new FluentTask("alert if site is down")
      .Every( TimeSpan.FromMinutes(3) )
      .StartingFrom( DateTime.Now )
      .When(delegate
      {
            return WebSite("http://www.questpond.com").IsAlive == false;
      })
      .Execute(delegate
      {
            Notify(“admin@example.org”, “site down!”);
      });

Are Fluent interfaces always good?

If you are creating fluent interfaces for developers then probably you are wasting time. Developers are consistent with creating objects with the new keyword and setting properties.  Even if you create something simple for them they are consistent with certain consuming methodology and you will find you are adding more complexity than simplifying it. 

Second thing we need to understand that we need to write more amount of code to create fluent interfaces. As you saw in the previous code I created a separate class for making interfaces simplified.

There are very few instances when you need fluent interfaces, below are few of them:-
  • During UNIT testing when the developers are not full fledged programmers.
  • You want your code to be readable by non-programmers so that they can understand if the code is satisfies their domain logic.
  • You are component seller and you want to stand out in the market as compared to the others by making your interfaces simpler.
  • You are creating a DSL language and this language is mapped with fluent interface statements.