SOLID Design Principles Explained – C#

To Design a application  we need to follow some set of principle. SOLID principle is main in that.

SOLID is an acronym:

  1. Single Responsibility Principle (SRP)
  2. Open Closed Principle (OCP)
  3. Liskov Substitution Principle (LSP)
  4. Interface Segregation Principle (ISP)
  5. Dependency Inversion Principle (DIP)

 

Single Responsibility Principle (SRP)

“A class should have only one reason to change”

SRP states that a class should not be loaded with multiple responsibilities and  single responsibility should not be spread across multiple classes or mixed with other responsibilities.

The reason is that more changes requested in the future, the more changes the class need to apply.

Open Closed Principle (OCP)

“Classes should be open for extension, but closed for modification.”

OCP states that the class should be easily extended but there is no need to change its core implementations.The application or software should be flexible to change.New features should be implemented using the new code, but not by changing existing code

Lizkov Substitution Principle (OCP)

“LSP states that the child class should be perfectly substitutable for their parent class.”

If program or module is using base class then derived class should be able to extend their base class without changing their original implementation.

Interface Segregation Principle (ISP)

“ISP states that No client should be forced to implement methods which it does not use”

We need to break down classes, a class with lot  of implementation and used as a base class is not a good design. For that reason we can create separate interfaces for each operation or requirement rather than having a single class to do the same work.

Dependency Inversion Principle (DIP)

DIP states mainly two methodology they are

“High-level modules should not depend on low-level modules. Both should depend on abstractions”

“Abstractions should not depend on details. Details should depend on abstractions.”

It states that we must use abstract class or interfaces for reducing tight coupling among the software components.

 

What is a View in sql server and its advantages ?

 

A view is nothing more than a saved query.  A view can also be considered as a virtual table.

DeptId DeptName
1 IT
2 Payroll
3 HR
4 Admin

 

Id Name Salary Gender DepartmentId
1 Jhon 5000 Male 3
2 Mike 3400 Male 2
3 Pam 6000 Female 1
4 Todd 4800 Male 4
5 Sara 3200 Female 1
6 Ben 4800 Male 3

Select Query

Select Id, Name, Salary, Gender, DeptName

From tblEmployee

Join tblDepartment

On tblEmployee.DepartmentId=tblDepartment.DeptID

Result

Id Name Salary Gender DeptName
1 Jhon 5000 Male HR
2 Mike 3400 Male Payroll
3 Pam 6000 Female IT
4 Todd 4800 Male Admin
5 Sara 3200 Female IT
6 Ben 4800 Male HR

Creating View using select Query

Create View vWEmployeesByDepartment

As

Select Id, Name, Salary, Gender, DeptName

From tblEmployee

Join tblDepartment

On tblEmployee.DepartmentId=tblDepartment.DeptID

Select * from  vWEmployeesByDepartment

Result

Id Name Salary Gender DeptName
1 Jhon 5000 Male HR
2 Mike 3400 Male Payroll
3 Pam 6000 Female IT
4 Todd 4800 Male Admin
5 Sara 3200 Female IT
6 Ben 4800 Male HR

Advantages of views

  • Views can be used to reduce the complexity of the database schema
  • Views can be used as a mechanism to implement row and column level security

Example if IT department employees only to show

Create View vWITEmployeesByDepartment

As

Select Id, Name, Salary, Gender, DeptName

From tblEmployee

Join tblDepartment

On tblEmployee.DepartmentId=tblDepartment.DeptID

Where tblDepartment.DeptName=’IT’

Select * from  vWITEmployeesByDepartment

Result

Id Name Salary Gender DeptName
3 Pam 6000 Female IT
5 Sara 3200 Female IT

Example if salary to hide

Create View vWNonconfedentialdata

As

Select Id, Name,  Gender, DeptName

From tblEmployee

Join tblDepartment

On tblEmployee.DepartmentId=tblDepartment.DeptID

Select * from vWNonconfedentialdata

Result

Id Name Gender DeptName
1 Jhon Male HR
2 Mike Male Payroll
3 Pam Female IT
4 Todd Male Admin
5 Sara Female IT
6 Ben Male HR
  • Views can be used to present aggregated data and hide detailed data.

Example

Create view VwsummarizedData

As

select DeptName, Count(ID) as Total

From tblEmployee

Join tblDepartment

On tblEmployee.DepartmentId=tblDepartment.DeptID

Group by DeptName

 

Select * from VwsummarizedData

Result

DeptName Total
Admin 1
HR 2
IT 2
Payroll 1

 

To modify view – ALTER VIEW statement

To droop a view – DROP VIEW viewname

How to use date in javascript and the different date methods?

The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ). Date expression with arguments

  • new Date( )
  • new Date(datestring)
  • new Date(year,month,date[,hour,minute,second,millisecond ])

Description of the parameters:

  • No Argument:With no arguments, the Date( ) constructor creates a Date object set to the current date and time.
  • Datestring: When one string argument is passed, it is a string representation of a date, in the format accepted by the Date.parse( ) method.
  •  7 agruments: To use the last form of constructor given above, Here is the description of each argument:
    • year: Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998, rather than 98.
    • month: Integer value representing the month, beginning with 0 for January to 11 for December.
    • date: Integer value representing the day of the month.
    • hour: Integer value representing the hour of the day (24-hour scale).
    • minute: Integer value representing the minute segment of a time reading.
    • second: Integer value representing the second segment of a time reading.
    • millisecond: Integer value representing the millisecond segment of a time

Date Methods:

  •  Date()            – Returns today’s date and time
  • getDate()        -Returns the day of the month for the specified date according to local time.
  • getDay()         -Returns the day of the week for the specified date according to local time.
  • getFullYear() -Returns the year of the specified date according to local time.
  • getHours()      -Returns the hour in the specified date according to local time.
  • getMonth()     -Returns the month in the specified date according to local time.
  • getSeconds()  -Returns the seconds in the specified date according to local time.
  • getTime()        -Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • getTimezoneOffset()   -Returns the time-zone offset in minutes for the current locale.
  • getUTCDate()        -Returns the day (date) of the month in the specified date according to universal time.
  • getUTCDay()          -Returns the day of the week in the specified date according to universal time.
  • getUTCFullYear()   -Returns the year in the specified date according to universal time.
  • getUTCHours()      -Returns the hours in the specified date according to universal time.
  • getUTCMilliseconds()     -Returns the milliseconds in the specified date according to universal time.
  • getUTCMinutes()            -Returns the minutes in the specified date according to universal time.
  • getUTCMonth()  -Returns the month in the specified date according to universal time.
  • getUTCSeconds()    -Returns the seconds in the specified date according to universal time.

What are the different action method’s return type?

1) ViewResult : Used to return a view to render html in the browser. this is the most common used action result

2)PartialViewResult: Similar to view result, it returns a partial view.

3) ContentResult: Used to return any type of content. By default it is used to return plain text but the actual content type can be explicitly defined

4) EmptyResult: this is equivalent to void method,it is by definition an action result object,but it does nothing.

5)FileResult:Used to return binary content.

6)HttpUnauthroizedResult: You can return an HttpUnauthrized result when the request tries to access restricted content that, for example its not available to anonymous user.

7)JavaScriptResult:Used to  return JavaScript code.

8)JsonResult: Used  to return any object in JavaScript Object Notation(JSON) format.

9)RedirectRresult:Used to perform an HTTP redirect to another URL.you can define it as temporary (code 302) or permanent (code 301)  HTTP redirect.

10)RedirectToRouteResult: used to perform an HTTP redirect ,but to specific route rather than a URL

 

 

WHAT IS AJAX AND ITS FEATURES?

WHAT IS AJAX?

  • Asynchronous Javascript And Xml. It is not a programming language
  • Used to create better, faster and more interactive applications.
  • Ajax uses asynchronous data transfer between the browser and server.
  • Ajaxcontroltoolkit.dll is the dll used for Ajax control tool kit. 

What are the controls of ajax?

  • ScriptManager
  • ScriptManagerProxy
  • UpdatePanel
  • UpdateProgress
  • Timer

What are the advantages of AJAX?

  • It saves memory while fetching data from same page
  • More interactive
  • Speed 

What are the disadvantages of AJAX?

  • AJAX is dependent on javascript
  • Security issues
  • Debugging difficulty

What are the technologies used by AJAX?

  • Javascript
  • XMLHttpRequest
  • Document Object Model(DOM)
  • Extensible HTML(XHTML)
  • Cascading Style Sheets (CSS)

What are the features of AJAX?

  • Dataview control
  • Live data binding
  • Invoking Ado.net data services and data contexts
  • Client side template rendering

 

What is the difference between AJAX and JAVASCRIPT?

AJAX JAVASCRIPT
Ajax send request and performs other operations in the mean time they get the response Javascript send request and waits for the response
Full page downloading is not needed Full page downloading is needed
minimize the overload on server Posts request and updates the script every time
AJAX is a technique of communication Javascript is a scripting language

 

Explain asp.net page life cycle?

Asp.net page life cycle passes through some series of process during their life cycle. Following are the stages

Begin request

  • This event will show that it’s a new request
  • Object and variables are initialized in this stage
  • This event is raised on every request

Authenticate request

  • .net is ready to authenticate the user in this stage
  • Authentication code are inserted here
  • This request confirms the authentication

Authorize request

  • net is ready to authorize the user in this stage
  • Custom Authorization code can be done here
  • This request confirms the authorization of the user

Resolve request cache

  • Output cache directive is commonly used in asp.net for caching
  • After authorize request is finished, authorize will calls the cache request to serve the request.
  • During runtime asp.net can load data from cache if needed.
  • Any type of cache activity code can be written here

Map request handler

  • Checks the extension of the file
  • Checks the handler of the request

Acquire request state

  • Ready to acquire session state variables/information

Process request

  • Logic execution is done at this stage

Init

  • Control’s id is set at this stage
  • Dynamically add controls at this stage
  • Read control properties

Load

  • Controls are fully loaded at this stage
  • All the values are restored in this stage
  • “Ispostback” is used to avoid unnecessary state change

Render

  • Method of page and page objects
  • Html changes can be mad lastly in this section
  • This method generates client side html and scripts to display

Unload

  • This is to clean up the code like closing connection, closing opened files
  • Page objects are unloaded from the memory

Release request state

  • Save or update session variables are done at this stage

Update request cache

  • Update cache before end of the process

End request

  • Last stage end of the request

 

                               Image shows the page life cycle of an event in GET and POST condition

What is ADO.NET and its features?

ADO.NET 

  • It is one of the major component of .Net Framework.
  • It is designed to connect to databases like Oracle, MySQL, SQL Server, MS Access etc. and work with data stored in them

In ADO.net some namespaces are used to access database they are

  • Data – Contains all generic data access classes
  • Data.Common – Contains classes which are shared / overridden by data providers
  • Data.OleDb – OLE DB provider classes used to access database such as Oracle, MySQL, SQL Server and MS Access.
  • Data.SqlClient – Contains classes for SQL Server
  • Data.SqlTypes – Contains SQL Server data types

Different layers of ADO.Net

  • Database Access Layer
  • Business Logic Layer
  • Presentation Layer

Features of ADO.Net 

  • It provides some in built classes to connect databases like mysql,sql,oracle etc,
  • In built classes to do operations like insert,update,delete and select.
  • Integration is very tight with XML
  • Disconnected Data architecture for better performance

New features in ADO.NET Entity Framework

  • Presistence Ignorace
  • Lazy Loading
  • Self Tracking Entities
  • Model-First Development
  • Model-Defined Functions

Web form events available in ASP.NET?

The Web Form Events in the sequence of their execution –

  • Page_Init
    • First step in page life cycle.
    • This event will raise whenever page initialized.
    • All the controls in the page have been initialized.
    • This event can be used to read or initialize control properties.
  • Page_Load
    • This event occurs only after Page_Init event.
    • This event also will raise for every postback operation.
    • All control properties are loaded with information recovered from view state and control state.
  • Page_PreRender
    • Perform any updates before the output is rendered.
    • Any changes made to the state of the control in the prerender phase can be saved.
  • While changes made in the rendering phase are lost.
  • Page_PreRender is the last event you have a chance to handle prior to the page’s state being rendered into HTML.
  • Page_Unload
    • This event is used for cleanup code.
    • This event occurs for each control and then for the page.
    • Instances of classes, in other words objects.
    • Closing opened files.
    • Closing database connections.

Wcf

WCF                                                             

  • Microsoft has introduced WCF  for inter process communication. WCF let us to establish communication channels using
  • TCP
  • HTTP
  • MSMQ
  • IPC
  • Peer Network
  • Namespace “System.ServiceModel” is used for WCF.

Different ways to host WCF Service are

  • IIS
  • Self-Hosting
  • WAS

In WCF modes of communication between server and client are

  • One-Way
  • Request-Reply
  • Callback

Security features used in WCF 

  • Integrity
  • Confidentiality
  • Authentication
  • Authorization

The essential components of WCF

  • Service class
  • End point
  • Hosting Environment

How WCF works?

WCF will follow – “Software and Service Model”, in which all essential components are defined as services and this will be used by client program.

Difference between classic web services (ASMX) and WCF

Classic web service known as ASMX were using SOAP protocol for sending and receiving the messages over network and over HTTP protocol whereas WCF allows the communication to happen over any transport protocol.

What is boxing and unboxing in c#?

In c# type system are mainly of 3 types

  • Value type
  • Reference type
  • Pointer type

BOXING

The operation of converting a value type to  reference type is called BOXING

	int Val = 1;
 	Object Obj = Val; //Boxing
  • The first line we created a Value Type Val and assigned a value to Val
  • The second line , we created an instance of Object Obj and assign the value of Val to Obj.

From the above operation (Object Obj = i ) we saw converting a value of a Value Type into a value of a corresponding Reference Type . These types of operation is called Boxing.

UNBOXING

The operation of converting a   reference type to  value type  is called UNBOXING

 	int Val = 1;
 	Object Obj = Val; //Boxing
 	int i = (int)Obj; //Unboxing
  • The first two line shows how to Box a Value Type .
  • The third line (int i = (int) Obj)  means extracting the value type from object
  • It means  converting value of a reference  type into a value of a Value type

This  operation is called Unboxing.