Sunday, September 8, 2019

Test-driven development (TDD)

TDD is development methodology.  by TDD Your code will make sure there are no bugs.

Requirements are turned into very specific test cases, then the software is improved so that the tests pass.

TDD is a software development process that relies on the repetition of a very short development cycle.

Image result for tdd development methodology

Benefits of TDD:

Writing the tests first requires you to really consider what do you want from the code.

You receive fast feedback.

TDD creates a detailed specification.

TDD reduces time spent on rework.

Spend less time in the debugger.

Able to identify the errors and problems quickly.

TDD tells you whether your last change (or refactoring) broke previously working code.

TDD allows the design to evolve and adapt to your changing understanding of the problem.

TDD forces the radical simplification of the code. You will only write code in response to the requirements of the tests.

You're forced to write small classes focused on one thing.

The resulting unit tests are simple and act as documentation for the code.

The development time to market is shorter.

The programmer’s productivity is increased.

Quality is improved.

Bugs are reduced.






Thursday, August 22, 2019

Map network drive with sql

exec xp_cmdshell 'net use Z: \\192.01.01.10\FileStorage [Password] /user:192.01.01.10\Administrator'(Username)

--exec xp_cmdshell 'net use Z: /delete'

--exec xp_cmdshell 'dir Z:'

For more ref : https://www.mytechmantra.com/LearnSQLServer/Configure-Network-Drive-Visible-for-SQL-Server-During-Backup-and-Restore-Using-SSMS/

Tuesday, December 5, 2017

Visual Studio 2017 download

Visual Studio Community 2017

A free, fully featured, and extensible solution for individual developers to create applications for Android, iOS, Windows, and the web.
Download

Visual Studio Enterprise 2017

An integrated, end-to-end solution for developers looking for high productivity and seamless coordination across teams of any size.
Download

Visual Studio Professional 2017

Improve productivity with professional developer tools and services to build applications for any platform.
Download

Visual Studio Test Professional 2017

Drive quality and collaboration throughout the development process. 
Download

Visual Studio Team Explorer 2017

A free solution for non-developers to interact with Team Foundation Server and Visual Studio Team Services.
Download 

Sunday, November 12, 2017

.net 2.0 features

1 Partial class
2 Generics
3 Static classes
4 Generator functionality
5 Anonymous delegates
6 Delegate covariance and contravariance
7 The accessibility of property accessors can be set                  independently
8 Nullable types
9 Null-coalescing operator



Partial class
Partial classes allow implementation of a class to be spread between several files, with each file containing one or more class members. It is useful primarily when parts of a class are generated automatically. For example, the feature is heavily used by code-generating user interface designers in Visual Studio.

file1.cs:

public partial class MyClass
{
    public void MyMethod1()
    {
        // Manually written code
    }
}
file2.cs:

public partial class MyClass
{
    public void MyMethod2()
    {
        // Automatically generated code
    }
}


Generics
Generics, or parameterized types, or parametric polymorphism is a .NET 2.0 feature supported by C# and Visual Basic. Unlike C++ templates, .NET parameterized types are instantiated at runtime rather than by the compiler; hence they can be cross-language whereas C++ templates cannot. They support some features not supported directly by C++ templates such as type constraints on generic parameters by use of interfaces. On the other hand, C# does not support non-type generic parameters. Unlike generics in Java, .NET generics use reification to make parameterized types first-class objects in the CLI Virtual Machine, which allows for optimizations and preservation of the type information.[1]

Static classes
Static classes are classes that cannot be instantiated or inherited from, and that only allow static members. Their purpose is similar to that of modules in many procedural languages.

Generator functionality
The .NET 2.0 Framework allowed C# to introduce an iterator that provides generator functionality, using a yield return construct similar to yield in Python.[2] With a yield return, the function automatically keeps its state during the iteration.

// Method that takes an iterable input (possibly an array)
// and returns all even numbers.
public static IEnumerable<int> GetEven(IEnumerable<int> numbers)
{
    foreach (int i in numbers)
    {
        if (i % 2 == 0) 
            yield return i;
    }
}
There is also a yield break statement, in which control is unconditionally returned to the caller of the iterator. There is an implicit yield break at the end of each generator method.

Anonymous delegates
As a precursor to the lambda functions introduced in C# 3.0, C#2.0 added anonymous delegates. These provide closure-like functionality to C#.[3] Code inside the body of an anonymous delegate has full read/write access to local variables, method parameters, and class members in scope of the delegate, excepting out and ref parameters. For example:-

int SumOfArrayElements(int[] array)
{
    int sum = 0;
    Array.ForEach(array,
        delegate(int x)
        {
            sum += x;
        }
    );
    return sum;
}
Unlike some closure implementations, each anonymous delegate instance has access to the same relative memory location for each bound variable, rather than to the actual values at each creation. See a fuller discussion of this distinction.

Delegate covariance and contravariance
Conversions from method groups to delegate types are covariant and contravariant in return and parameter types, respectively.[4]

The accessibility of property accessors can be set independently
Example:

string status = string.Empty;

public string Status
{
    get { return status; }             // anyone can get value of this property,
    protected set { status = value; }  // but only derived classes can change it
}

Nullable types
Nullable value types (denoted by a question mark, e.g. int? i = null;) which add null to the set of allowed values for any value type. This provides improved interaction with SQL databases, which can have nullable columns of types corresponding to C# primitive types: an SQL INTEGER NULL column type directly translates to the C# int?.

Nullable types received an improvement at the end of August 2005, shortly before the official launch, to improve their boxing characteristics: a nullable variable which is assigned null is not actually a null reference, but rather an instance of struct Nullable<T> with property HasValue equal to false. When boxed, the Nullable instance itself is boxed, and not the value stored in it, so the resulting reference would always be non-null, even for null values. The following code illustrates the corrected flaw:

int? i = null;
object o = i;
if (o == null)
    System.Console.WriteLine("Correct behaviour - runtime version from September 2010 or later");
else
    System.Console.WriteLine("Incorrect behaviour - pre-release runtime (from before September 2010)");

When copied into objects, the official release boxes values from Nullable instances, so null values and null references are considered equal. The late nature of this fix caused some controversy[5] , since it required core-CLR changes affecting not only .NET2, but all dependent technologies (including C#, VB, SQL Server 2005 and Visual Studio 2005).

Null-coalescing operator
The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.[6]

object nullObj = null; 
object obj = new Object(); 
return nullObj ?? obj; // returns obj
The primary use of this operator is to assign a nullable type to a non-nullable type with an easy syntax:

int? i = null;
int j = i ?? 0; // If i is not null, initialize j to i. Else (if i is null), initialize j to 0.

Sunday, September 17, 2017

Customized Authentication Filters in ASP.MVC5

As we studied in last article, Filters are used to inject logic at the different levels of request processing. Below is the filters execution sequence:
Authentication Filters ==>  Authorization filter ==> Action filter ==> Result filter ==> Exceptionfilter
  • The authentication filter executes before any other filter
  • The authorization filter executes after Authentication filter and action method, or before any other filter
  • The action filter executes before and after any action method
  • The result filter executes before and after the execution of any action result
  • The exception filter executes only if any action methods, filters, or results throws an exception
  • Create a new class
  • Implement IAuthenticationFilter Interface
  • Derive it from ActionFilterAttribute
  • Override the OnAuthentication method to run logic before the action methodg

  • OnAuthentication Method
  • The program invokes Authentication Filters by calling this method. This method creates the AuthenticationContext. AuthenticationContext has information about performing authentication. We can use this information to make authentication decisions based on the current context.

    OnAuthenticationChallenge Method

    This method executes after the OnAuthentication method. We can use the OnAuthenticationChallenge method to perform additional tasks on request. This method creates an AuthenticationChallengeContext the same way as OnAuthentication.
  • Tuesday, November 29, 2016

    Generics in C-sharp(C#)

    Generics allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.

    Some of the most useful features of Generics are:
    • type safety.
    • Eliminates type mismatch at run time.
    • Better performance with value type.
    • Does not incur inter conversion.
    Generic | Creation of Generic List <T>

    The procedure for using a generic List collection is similar to the concept of an array. We do declare the List, extend its sub members and then access the extended sub members of that List.

    For example:

    List<int> myInts = new List<int>(); 
    myInts.Add(1);
    myInts.Add(2);
    myInts.Add(3);
    for (int i = 0; i < myInts.Count; i++) 
       Console.WriteLine("MyInts: {0}", myInts[i]); 
    }

    Generic | Handling Dictionary <TKey , TValue> 

    A Dictionary is nothing but a generic collection  that works with key/value pairs. A Dictionary works similarly to the way of non-generic collections such as a HashTable. The only difference between a Dictionary and a HashTable is that it operates on the object type.

    The complete scenario of a Dictionary collection is explained through the example of customers.

    For example:

    public class Customer
    public    Customer(int id, string name)
       { 
          ID = id;
          Name = name; 
       } 
       private int m_id;

    public    int ID
       { 
          get { return m_id; }
          set { m_id = value; } 
       } 
       private string m_name; 
       public string Name
       { 
          get { return m_name; }
          set { m_name = value; }
       }
    }

    Generic | Dictionary Collection

    This code part represents dictionary handling with customer extended objects. It also explains how entries are extracted from a Dictionary.

    For example:

    Dictionary<int, Customer> customers = new Dictionary<int, Customer>(); 

    Customer cust1 = new Customer(1, "Cust 1");
    Customer cust2 = new Customer(2, "Cust 2");
    Customer cust3 = new Customer(3, "Cust 3"); 
    customers.Add(cust1.ID, cust1);
    customers.Add(cust2.ID, cust2);
    customers.Add(cust3.ID, cust3);

    foreach (KeyValuePair<int, Customer> custKeyVal in customers) 
       Console.WriteLine(
       "Customer ID: {0}, Name: {1}",
       custKeyVal.Key,
       custKeyVal.Value.Name); 
    }


    Monday, November 28, 2016

    ASP.NET MVC interview question

    1) What is MVC?
    MVC is a pattern which is used to split the application's implementation logic into three components: models, views, and controllers.

    2) What is MVC (Model View Controller)?
    MVC is an architectural pattern which separates the representation and user interaction. It’s divided into three broader sections, Model, View, and Controller. Below is how each one of them handles the task.

    The View is responsible for the look and feel.
    Model represents the real world object and provides data to the View.
    The Controller is responsible for taking the end user request and loading the appropriate Model and View.

    3) What are the benefits of using MVC?
    There are two big benefits of MVC:

    Separation of concerns is achieved as we are moving the code-behind to a separate class file. By moving the binding code to a separate class file we can reuse the code to a great extent.
    Automated UI testing is possible because now the behind code (UI interaction code) has moved to a simple .NET class. This gives us opportunity to write unit tests and automate manual testing.

    4) Can you explain the page life cycle of MVC?
    Below are the processed followed in the sequence -
    App initialization
    Routing
    Instantiate and execute controller
    Locate and invoke controller action
    Instantiate and render view.

    5) What are the advantages of MVC over ASP.NET?
    Provides a clean separation of concerns among UI (Presentation layer), model (Transfer objects/Domain Objects/Entities) and Business Logic (Controller).
    Easy to UNIT Test.
    Improved reusability of model and views. We can have multiple views which can point to the same model and vice versa.
    Improved structuring of the code.

    6) What is the use of ViewModel in MVC?

    ViewModel is a plain class with properties, which is used to bind it to strongly typed view. ViewModel can have the validation rules defined for its properties using data annotations.


    7) What is Razor View Engine?
    Razor is the first major update to render HTML in ASP.Net MVC 3. Razor was designed specifically for view engine syntax. Main focus of this would be to simplify and code-focused templating for HTML generation.


    8) What are Actions in ASP.Net MVC?
    Actions are the methods in Controller class which is responsible for returning the view or json data. Action will mainly have return type : "ActionResult" and it will be invoked from method : "InvokeAction()" called by controller.

    9) How to enable Attribute Routing?
    Just add @Model.CustomerName the method : "MapASP.Net MVCAttributeRoutes()" to enable attribute routing

    10) Explain JSON Binding?
    JavaScript Object Notation (JSON) binding support started from ASP.Net MVC3 onwards via the new JsonValueProviderFactory, which allows the action methods to accept and model-bind data in JSON format. This is useful in Ajax scenarios like client templates and data binding that need to post data back to the server.

    11) which is a better fit, Razor or ASPX?
    As per Microsoft, Razor is more preferred because it’s light weight and has simple syntaxes.

    12) How to implement AJAX in MVC?
    You can implement AJAX in two ways in MVC:

    13) AJAX libraries
    jQuery
    Below is a simple sample of how to implement AJAX by using the “AJAX” helper library. In the below code you can see we have a simple form which is created by using the Ajax.BeginForm syntax. This form calls a controller action called getCustomer. So now the submit action click will be an asynchronous AJAX call.

    Hide   Copy Code
    <script language="javascript">
    function OnSuccess(data1)
    {
    // Do something here
    }
    </script>


    14) What are Code Blocks in Views?
    Unlike code expressions that are evaluated and sent to the response, it is the blocks of code that are executed. This is useful for declaring variables which we may be required to be used later.

    @{
     int x = 123;
     string y = "aa";
     }


    15) What is RouteConfig.cs in ASP.Net MVC 4?
    "RouteConfig.cs" holds the routing configuration for ASP.Net MVC. RouteConfig will be initialized on Application_Start event registered in Global.asax.

    16) What is PartialView in ASP.Net MVC?
    PartialView is similar to UserControls in traditional web forms. For re-usability purpose partial views are used. Since it's been shared with multiple views these are kept in shared folder. Partial Views can be rendered in following ways :

    Html.Partial()
    Html.RenderPartial()

    17) How can we determine action invoked from HTTP GET or HTTP POST?
    This can be done in following way : Use class : "HttpRequestBase" and use the method : "HttpMethod" to determine the action request type.

    18) What is ViewData?

    Viewdata contains the key, value pairs as dictionary and this is derived from class – “ViewDataDictionary“. In action method we are setting the value for viewdata and in view the value will be fetched by typecasting.


     19) How we can add the CSS in MVC?

    Below is the sample code snippet to add css to razor views –

    <link rel="StyleSheet" href="/@Href(~Content/Site.css")" type="text/css"/>

     20) Mention some action filters which are used regularly in MVC?

    Below are some action filters used –

    Authentication
    Authorization
    HandleError
    OutputCache


    21) Explain Keep method in Tempdata in MVC?

    As explained above in case data in Tempdata has been read in current request only then “Keep” method has been used to make it available for the subsequent request.

    @TempData[“TestData”];
    TempData.Keep(“TestData”);

    22) What is the use of the dispose method in C# ?

    dispose method are used for forcefully garbage collection..!!!

    23) Major and importance difference between for and foreach loop ?

    a for loop is a construct that says "perform this operation n. times".a foreach loop is a construct that says "perform this operation against each value/object in this IEnumerable"

    24) What is data dictionary?
    Dictionaries provide fast lookups, based on keys, to get values. With them, we use keys and values of any type, including int and string.

    Opps Part 1 : Abstraction

      Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...