Thursday, 8 May 2014

3-Tire, MVC , MVP and MVVM architectures

It’s a bit confusion to each one and they just skip the importance of it.
First let me talk about the 3-tier (n-tier) architecture and what are the problems it has and why the new patterns came as it was the most popular architecture in their time. Also even now most of the companies are preferring 3 tier architecture for their development of the projects.

3-Tier Architecture: 
One of the most important and useful architecture to create the web applications. It has mainly 3 layers-
- UI or 
Presentation layer (.aspx, .ascx, aspx.cs, .ascx.cs files)
- 
Business Logic Layer (.cs classes)
- Data Access Layer (.cs classes)
These are the 3 main layers which are used while developing the applications in 3-tier architecture.

The 
UI Layer or Presentation Layer contains the main User Interface which will be viewable to the users. So all the screens, User defined controls, custom controls etc comes under UI or presentation layer.

Business Logic Layer is the middle layer which is used for the development of business logics for the application so that the validation should be performed or business requirements should be fulfilled before sending data to the Data Access layer. Most of the validations are performed at this level. This layer contains the business objects and the methods used for the communication between the UI and Data Access Layer.
Here few architects think of separating this layer with Business Object layer to make the design mode readable and easy. So they create one more layer called Business Objects layer which contains the public properties which can be used throughout the application.

Data Access Layer is used for all the database operations- Insert, Update, Delete, Select etc. So all the logic which is used for these operations are performed at this layer. Calling stored procedures, passing parameters etc.

So 3-Tier architecture uses these layers for the communications and gets the result to the UI layer. The communication is done based on the request from the user by UI layer. This request goes through the business logic layer and then if any validations need to perform. It will validate and if the validation passed, it sends these requests to the Data Access Layer. The data access layer performs the database operations and then the results is send back to
business layer and then to the UI.
Hope till now it
’s all going good...

Now the problem comes- 
1. If we add one more method in the business logic layer or in data access layer or in UI, we need to update all the layers. It means we need to implement that method in all the layer. Similarly if we remove one method from any of these layers, it will give you the error.
2. If we don
’t want to perform the validations, then also it will go to business logic layer and then to the database layer. Because here all the layers are tightly bounded to each other and are dependent to each other.
3. Always the floe will be UI <---> Business Logic Layer <---> Data Access Layer
There is no skip at any place.

Why MVC? 
MVC is also the components of triad means similar to the 3-tier architecture. It also has 3 layers-
- View (UI in 3-tier architecture) (.aspx, .ascx files)
- Controller (Business Logic Layer in 3-tier architecture) (.cs files)
- Model (Data Access layer in 3-tier architecture) (.cs files)
Here you can see the main difference between the UI and View is that the View does not contain .cs file attached with it. It contains only .aspx, .ascx files.
View- It is similar to the .aspx pages but with the .cs files. These are again used for the user interface.
Controller- It is slight difference than the Business Logic layer classes. It contains all the events, methods and properties related to the corresponding view.
Model- It is similar to the Data Access Layer classes where the data access logic is written. So there is no difference between the Model and Data Access Layer classes. It
’s the programmer’s view that for the database operations what they use to connect to the database and do the operations.
How MVC works differently than 3-Tier architecture -
In MVC, the processing always starts from the Controller unlike from UI as compare to 3-Tier architecture. For example, the user has made the request to get the product details by supplying the product id and the URL is something like this
http://www.MyWebSite.com/ProductController/GetProductDetails?id=2 
Here you can see the name ProductController. This is the controller class which will handle the request to process and responsible for getting the product details for the product id as 2.
If there is a controller class, there must be the view for the same controller class. Like in this URL, the corresponding Product view will be in the View section for the ProductController class. Then only the application will recognize that which view should be populated here. 
In this example, first the URL will be checked for the controller class i.e. ProductController and then it will check for the next method which is used i.e.GetProductDetails and then parameter if any like in this case, id=2. It will then call the corresponding model class to get the product details for the id as 2. After retrieving the details, it will search for the Product view and populate the product view.
Now the task is done...
How it has benefit than old architecture-
1. No need to go from View <---> Controller <---> Model like in 3-tier architecture. So these layer are not tightly bounded to each other.
2. If we add any new method to the controller class or model class or add any new design to the view, it will not impact any other layer. Because if we add new method in the controller and didn
’t call it, it will not execute as it is not dependent to any other layer.
3. It's not necessary that the Controller will always call the Model class, if there is no need to do any database operations, then just call the view to open the blank screen.
So these are the main advantages of MVC architecture. Due to these advantages, it
’s faster in execution than the 3-Tier architecture.

Why MVP if MVC is fulfilling all the requirements? 
Yes, why MVP if the MVC was good enough.
No, MVC was not good enough because there were few issues with MVC:
1. The execution starts from Controller i.e. Business Logic which is violating the rules of W3C. All the web application should start from the user Interface not from the business logic. As business logic can be exposed if proper security is not implemented.
2. Model and UI are still connected as they are attached by a logical layer.
MVP mainly came for automated unit testing. It is a derivative of the MVC where the Controller of MVC replaced by the Presenter.
When the user triggers/calls a method of view, a method of presenter without parameter (default constructor) gets invoked. And the view is displayed.
Here the execution starts always from the View so the issue 1 of MVC gets resolved.
It mainly made to test the application without View. Only by using the presenter we can test the whole application. Presenter mainly acts upon model and updates the view accordingly. So there is no coupling between the View and Model here. Whenever the Model change, automatically the view gets updated.
So all the issues of MVC gets resolved in MVP pattern.
Now if MVP was fulfilling all the requirements and no coupling between the View and Model then why MVVM??
Why MVVM if MVP was fulfilling all the requirements and solving all the issues by 3-Tier architecture and MVC? 
Yes, MVVM is also called as Specialized MVC where the Controller is replaced by using the View Model.
Here the View is just below the UI layer. View Model exposes the data and command objects according to the need of View. We can say that View Model is the blue print of View which contains all the properties, methods, events which the View needs. View Model pulls the data from the Model and then accordingly the View gets updated. This works well because this model created by making the customer as the key value and keeping the user experience as the first priority. It uses the observer pattern to make the changes as soon as the View Model is updated so the user will not get any flicking of the current page. It
’s looking like working with windows application. 

Thursday, 13 March 2014

WPF Interview Questions


  1. What are the advantages of WPF over WinForms?
  2. What are the types of Binding modes?
  3. What is the default Binding mode?
  4. What is the use of INotifyPropertyChanged?
  5. What is the difference between PRISM and MVVM?
  6. What is PRISM?
  7. Explain the process behind WPF.
  8. What is the difference between StaticResourece and DynamicResoures?
  9. What is the difference between CustomControl and UserControl.
  10. How to communicate  View to ViewModel for button click?
  11. Where to write ICommand?
  12. What is difference between Visual Tree and Logical Tree?
  13. What is the difference between CLR property and Dependency property?
  14. What are Routed Events?

Sunday, 2 March 2014

Complicated queries

--*******Create Employee_Test Table ******
--Drop Table Employee
CREATE TABLE Employee
(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)

INSERT INTO Employee VALUES ('Anees',1000);
INSERT INTO Employee VALUES ('Rick',1200);
INSERT INTO Employee VALUES ('John',1100);
INSERT INTO Employee VALUES ('Stephen',1300);
INSERT INTO Employee VALUES ('Maria',1400);

--******Select All
Select * from employee

--*****Find Heighest Salary
Select Max(Emp_Sal) From Employee

--*****Find 3rd Heighest Salary
Select Min(Emp_Sal)
From Employee
Where Emp_Sal IN (Select Distinct top 3 Emp_Sal From Employee Order By Emp_Sal Desc)

--*******Find Nth Heighest Salary
Select Min(Emp_Sal) From Employee Where Emp_Sal IN (Select Distinct Top N Emp_Sal From Employee Order By Emp_Sal Desc)

--****Create another table
create table photo_test
(
pgm_main_Category_id int,
pgm_sub_category_id int,
file_path varchar(MAX)
)

insert into photo_test values
(17,15,'photo/bb1.jpg');    
                                               
insert into photo_test values(17,16,'photo/cricket1.jpg');                                                  
insert into photo_test values(17,17,'photo/base1.jpg');                                                      
insert into photo_test values(18,18,'photo/forest1.jpg');                                                      
insert into photo_test values(18,19,'photo/tree1.jpg');                                                          
insert into photo_test values(18,20,'photo/flower1.jpg');                                                    
insert into photo_test values(19,21,'photo/laptop1.jpg');                                                      
insert into photo_test values(19,22,'photo/camer1.jpg');                                                

insert into photo_test values(19,23,'photo/cybermbl1.jpg');                                                  
insert into photo_test values
(17,24,'photo/F1.jpg');

--*****select top 2 records from each group
select pgm_main_category_id,pgm_sub_category_id,file_path from
(
select pgm_main_category_id,pgm_sub_category_id,file_path,
rank() over (partition by pgm_main_category_id order by pgm_sub_category_id asc) as rankid
from photo_test
) photo_test
where rankid < 3 -- replace 3 by any number 2,3 etc for top2 or top3.
order by pgm_main_category_id,pgm_sub_category_id

--*********
CREATE TABLE Employee_Test1
(
Emp_ID INT,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)

INSERT INTO Employee_Test1 VALUES (1,'Anees',1000);
INSERT INTO Employee_Test1 VALUES (2,'Rick',1200);
INSERT INTO Employee_Test1 VALUES (3,'John',1100);
INSERT INTO Employee_Test1 VALUES (4,'Stephen',1300);
INSERT INTO Employee_Test1 VALUES (5,'Maria',1400);
INSERT INTO Employee_Test1 VALUES (6,'Tim',1150);
INSERT INTO Employee_Test1 VALUES (6,'Tim',1150);

--**********Deleting duplicate rows from a table
;with T as
(
select * , row_number() over (partition by Emp_ID order by Emp_ID) as rank
from employee_test1
)

delete
from T
where rank > 1


--***Date Functions
--Get the first day of the month

SELECT DATENAME(dw, DATEADD(dd, - DATEPART(dd, GETDATE()) + 1, GETDATE())) AS FirstDay

---*****SCHEMA LEVEL QUERIES****---------
select * from INFORMATION_SCHEMA.TABLES
SELECT * FROM ORDERS
SELECT * FROM EMPLOYEE
SELECT * FROM photo_test
SELECT * FROM Employee_Test1
SELECT * FROM sysdiagrams
SELECT * FROM ImageUpload
SELECT * FROM Customer
---*****SCHEMA LEVEL QUERIES****---------

Some Interview Questions about WPF asked in one level 5 company


  1. When to use Delegates and when to use Events in C# code?
  2. What are the uses of LINQ? 
  3. How to check performance of code? Are you using any Profilers?
  4. Are you using any test driven apps to test the code?
  5. Are you familiar with any Design patterns?
  6. Are you familiar with any Software Development Models?
  7. What are the new features in .net Framework 3.5/4.0/4.5
  8. What is the difference between Anonymous methods and Lambda expressions?
  9. How to handle unmanaged code in C#.
  10. Explain Memory management in C#.
  11. How helps CLR in C# memory management.
  12. What is Dispose method?
  13. Have you ever used Multi Threading?
  14. What is Thread Synchronization?
  15. What is the Architecture of our project?
  16. What are State management techniques in Asp.net?

Monday, 24 February 2014

Some Interview Questions about C# ASP OOP asked in one level 5 company-2

1.    What are OOPs concepts?
2.    What is Polymorphism?
3.    What is Runtime Polymorphism?
4.    What is Overloading and Overriding?
5.    Can’t we override without having Virtual keyword for base class method?
6.    Is overloading method change the body?
7.    Is it really required to implement all the method of an Interface in child class?
8.    How can we create object for Interface?
9.    How can we prevent overriding an abstract method?
10.  How can we write a abstract method in a non abstract class?
11.  How can we create object for a abstract class?
12.  Explain the real time scenario, when to use abstract class?
13.  What are the Access modifiers we have in C#?
14.  What is Enum? Have you used. Why and How?
15.  How to handle exceptions in C#?
16.  Can we have multiple Catch blocks?
17.  If one catches the next catch will be executed or not?
18.  Can we have multiple Web.config files?
19.  If yes which one executes first?
20.  Is it possible to have a vb.net class file in C# project?
21.  How they communicate?
22.  What is Viewstate?
23.  Can we pass data in Viewstate object to next page?
24.  In which page event you will get Viewstate data?
25.  Where the Viewstate data saves?
26.  What is Session?
27.  Where the Session data saves?
28.  How the server knows that a particular user session has been created. Because Session is Server side.
29.  What are the types of session modes?
30.  What is cookie less session? What happens if browser stops cookies?
31.  Have you ever used Query Strings what are limitations of Query Strings?
32.  Do you know WCF?
33.  Do you know Web Services?
34.  Explain Page life cycle?
35.  In which event will all the controls loaded?
36.  Where will check about post back page?
37.  What is the last page life cycle event?
38.  What happens in that?
39.  What is the use of Global.asax file?
40.  How can you display a common error page when an error occurs?
41.  What is Hash table?
42.  What is difference between Hash table and Dictionary
43.  Can’t we hold any type of data in Dictionary?
44.  What is a Heap?
45.  What is Value type and Reference type?
46.  Give me two examples of Value type and Reference type?
47.  Why to use stored procedures over inline queries?
48.  What is a View?
49.  What is temporary table?
50.  Can we create index on temporary table?
51.  What is clustered and non-clustered index?
52.  What is the deference between Primary key and Unique key?
53.  When you apply Unique on a table which index will be created?
54.  How to get a current inserted record id after executing stored procedure?
55.  Can we get XML script from stored procedures?
56.  Write the logic to get row number and duplicate row from table?
57.  What is the use of Having clause?
58.  Can we use Where and Having clause in same query?
59.  Explain about all joins?
60.  What result will get if right hand side table not having matching data?
61.  What is cross join?
62.  If I have two tables with 10 records in each, how many I get in resultant cross join?
63.  What is Equiv. join?
Employess
EmpId
Name
Sal
DeptId
MgrId
1
Ram
10000
1
4
2
Srinu
4000
2
4
3
Ravi
20000
3
2
4
Vinod
2000
1
3

Dept
DeptId
DeptName
Location
1
Dept1
HYD
2
Dept2
HYD
3
Dept3
Bangalore

64.  Display Employee name, Salarry and location whose location belongs to HYD
65.  Display Employees name, Salary, location and employee related manager whose location is HYD
66.  Display Employee count whose location is HYD and having salary >4000
67.  What is the difference between Data set and Data Reader?. Which one is preferable and Why?
68.  What is disconnected architecture?
69.  What happens if I not close a connection in connected architecture?
70.  Write a script of using ado objects for “Select * from Employees”. Which one you use Datareader or Data set.
71.  If use data reader how cans you bind data to data grid?
72.  What is event bubbling?
73.  How the parents know that child invoked an event?
74.  How to update a 5th cell in a data grid?
75.  .What is the types of Authentications are there and which one is preferred mostly?
76.  How can we stop Sessions?
77.  Which file is needed in all type of web application to run?