- What are SOLID principles
- Explain about OPEN CLOSE principle
- Explain about Dependency Inversion principle
- What Mediator Pattern and Explain
- What are the design Patterns you have used explain with scenario
- What is Entity Framwork
- What is Lazy Loading and Eagerly Loading
- How to get Parent Child collections in EF
- What is the difference between IEnumarable and IQueriable
venu.moguram's blog spot
Sunday, 30 August 2020
Random interview questions
Monday, 4 November 2019
Simple Factory Vs Factory Method Vs Abstract Factory
https://vivekcek.wordpress.com/2013/03/17/simple-factory-vs-factory-method-vs-abstract-factory-by-example/
Simple Factory Vs Factory Method Vs Abstract Factory by Example
Posted by vivekcek on March 17, 2013
I came across lots of situations in which, I have to explain the difference between a Simple Factory, Factory Method and Abstract Factory patterns.
The main objective of this post is how simply you can explain the difference among these patterns using some example. I hope the readers are aware of these patterns, because I am not going to explain each pattern in depth.
Simple Factory Pattern
Definition:
Creates objects without exposing the instantiation logic to the client.
Refers to the newly created object through a common interface
Diagram:
Explanation:
The heart of above Simple Factory pattern is the ‘MobileFactory’ class. From the Client when we create an instance of this ‘MobileFactory’, this class will load and cache all the classes that implement the ‘IMobile’ interface by using reflection or some other logic. After that we can call the ‘GetMobile(string Name)’ method in the ‘MobileFactory’ class which will return the specified object through the parameter.
The Client will expect an object that implements the ‘IMobile’ interface.
Factory Method
Definition:
Defines an interface for creating objects, but let subclasses to decide which class to instantiate
Refers the newly created object through a common interface.
Diagram:
Explanation:
In Factory Method pattern we will introduce a new interface called ‘IMobileFactory’ and two concrete implementation’s ‘NokiaFactory’ and ‘IphoneFactory’. These concrete classes control the object creation.
In my example the client want a Nokia object. So the steps are given below.
1.The client will load a reference to ‘NokiaFactory’. But Client won’t refer the ‘NokiaFactory’ class directly like the Simple Factory pattern. The client refers the concrete implementation through an interface ‘IMobileFactory’.
2.Then the Client call the ‘CreateMobile()’ method that will return an object of type ‘IMobile’.
3.Here we have to inform the client the concrete implementation to be used through some configuration or parameters.
Abstract Factory
Definition:
Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes
Diagram:
Explanation:
The main point regarding Abstract Factory pattern is that, this pattern creates a family of related objects that have different parent class or interface.
In Abstract Factory pattern the object creation happens in the same way as Factory Method. The only difference is the creation of related objects.
I now updated our old ‘IMobileFactory’ interface with two new methods.
CreateNokiaMobile() – Returns a Nokia objects that implements an ‘INokia’ interface.
CreateAppleMobile()- Returns Iphone objects that implements ‘IApple’ interface.
There are two concrete implementation of ‘IMobileFactory’ named 3GMobileFactory and 4GMobileFactory.
3GMobileFactory – Can return 3G supported mobiles of Nokia and Apple. So as per definition confirms, which returns a family of related objects that have different parents (INokia, IApple).
4GMobileFactory – Can return 4G supported mobiles of Nokia and Apple
Friday, 10 May 2019
Random interview questions
What is Covariance and Contravariance?
What is the difference between FirstOrDefatul,First,SingleOrDefault and SingleOrDefault?
What are Generic delegates?
Why Deletegates? Where did you used delegates in your project?
What is the difference between IQuarrable and IEnumarable and which one considered to be best for Performance point of view?
What are the Generations of Garbage Collector?
What is the difference between Var and Dynamic keywords?
Write down the query for inner join using LINQ?
What is the difference between Event and Deletegates?
Write down the code for Singleton patter for thread safty?
What are Extension methods?
Why to use Static Constructor and When it will called?
What is lazyloading and Eagerlyloading?
What is DBContext?
What is IDisposable?
Thursday, 19 March 2015
Single,SingleOrDefault,First and FirstOrDefault in linq C#
LINQ Single vs SingleOrDefault vs First vs FirstOrDefault
Single() | SingleOrDefault() | First() | FirstOrDefault() | |
---|---|---|---|---|
Description | Returns a single, specific element of a sequence | Returns a single, specific element of a sequence, or a default value if that element is not found | Returns the first element of a sequence | Returns the first element of a sequence, or a default value if no element is found |
Exception thrown when | There are 0 or more than 1 elements in the result | There is more than one element in the result | There are no elements in the result | Only if the source is null (they all do this) |
When to use | If exactly 1 element is expected; not 0 or more than 1 | When 0 or 1 elements are expected | When more than 1 element is expected and you want only the first | When more than 1 element is expected and you want only the first. Also it is ok for the result to be empty |
Examples
- Employeeid = 1: Only one employee with this ID
- Firstname = Robert: More than one employee with this name
- Employeeid = 10: No employee with this ID
Employeeid | Lastname | Firstname | Birthdate |
1 | Davolio | Nancy | 12/8/1948 12:00:00 AM |
2 | Fuller | Andrew | 2/19/1952 12:00:00 AM |
3 | Leverling | Janet | 8/30/1963 12:00:00 AM |
4 | Peacock | Margaret | 9/19/1937 12:00:00 AM |
5 | Buchanan | Robert | 3/4/1955 12:00:00 AM |
6 | Suyama | Michael | 7/2/1963 12:00:00 AM |
7 | King | Robert | 5/29/1960 12:00:00 AM |
8 | Callahan | Laura | 1/9/1958 12:00:00 AM |
9 | Dodsworth | Anne | 1/27/1966 12:00:00 AM |
Thursday, 8 May 2014
3-Tire, MVC , MVP and MVVM architectures
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 tobusiness layer and then to the UI.
Hope till now its 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 dont 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. Its the programmers 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 didnt 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, its 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. Its looking like working with windows application.
Sunday, 4 May 2014
Thursday, 13 March 2014
WPF Interview Questions
- What are the advantages of WPF over WinForms?
- What are the types of Binding modes?
- What is the default Binding mode?
- What is the use of INotifyPropertyChanged?
- What is the difference between PRISM and MVVM?
- What is PRISM?
- Explain the process behind WPF.
- What is the difference between StaticResourece and DynamicResoures?
- What is the difference between CustomControl and UserControl.
- How to communicate View to ViewModel for button click?
- Where to write ICommand?
- What is difference between Visual Tree and Logical Tree?
- What is the difference between CLR property and Dependency property?
- What are Routed Events?
Sunday, 2 March 2014
Complicated queries
--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
- When to use Delegates and when to use Events in C# code?
- What are the uses of LINQ?
- How to check performance of code? Are you using any Profilers?
- Are you using any test driven apps to test the code?
- Are you familiar with any Design patterns?
- Are you familiar with any Software Development Models?
- What are the new features in .net Framework 3.5/4.0/4.5
- What is the difference between Anonymous methods and Lambda expressions?
- How to handle unmanaged code in C#.
- Explain Memory management in C#.
- How helps CLR in C# memory management.
- What is Dispose method?
- Have you ever used Multi Threading?
- What is Thread Synchronization?
- What is the Architecture of our project?
- 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?
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
|
DeptId
|
DeptName
|
Location
|
1
|
Dept1
|
HYD
|
2
|
Dept2
|
HYD
|
3
|
Dept3
|
Bangalore
|