Sunday, May 25, 2014

Describe some of the pros and cons of ASP.NET MVC.

ASP.NET MVC advantages:
- Separation of concers (Soc): From a technical standpoint, the organization of code within MVC is very clear, organized and granular, making it easier for a web application to scale in terms of functionality. Promotes great design from a development standpoint.
- Eaiser integration with client side tools: More than ever, web applications are increasingly becoming as rich as the applications you see on your desktops. With MVC, it gives you the ability to integrate with such toolkits e.g. jQuery with greater ease and more seamless than in WebForms.
- Search Engine Optimization: URL's are more friendly to search engines. MVC is stateless, so this removes the headache of users who spawn multiple web browsers from the same window (session collisions).
- Works well with developers who need high degree of control: Many controls in ASP.NET web forms automatically generate much of the raw HTML you see when an page is rendered. This can cause headaches for developers. With MVC, it lends itself better towards having complete control with what is rendered and there are no surprises. Even more important, is that the HTML forms typically are much smaller than the Web forms which can equate to a performance boost - something to seriously consider.
- Test Driven Development (TDD) With MVC, you can more easily create tests for the web side of things. An additional layer of testing will provide yet another layer of defense against unexpected behavior.

ASP.NET disadvantages:
Complexity:
The MVC pattern introduces new levels of indirenction so increases the complexity of the solution.
It also increases the event-driven nature of the user-interface code, which can become more difficult to debug.
Cost of frequest updates:
Developers cannot completely ignore the view of the model even if they are decoupled. If the model undergoes frequest changes, the view could be flooded with update requests. Views like graphical displays may take some time to render. As a result, the view may fall behind update requests.

Wednesday, May 14, 2014

When making calls to a DB from an application when and why would you use stored procedures, and when and why would you create the SQL script on the fly within the application

Stored Procedures: One of the most beneficial reasons to use stored procedures is the added layer of security that can be placed on the database from the calling applications. If the user account created for the application or web site is configured with EXECUTE permissions only then the underlying tables cannot be accessed directly by the user account. This helps prevent hacking directly into the database tables. The risk of a hacker using the user account to run a stored procedure that has been written is far safer than having the user account have full insert, update and delete authority on the tables directly.
Another advantage to using stored procedures, especially in medium to large scale web sites or applications, is the data functionality is separated from the application making it easier to manage, document, and maintain. For example, if an application updates the customer table in ten different places, there can be a single stored procedure and a standard procedure call from the application for this functionality. If a change needs to be made to the way a customer record is managed, then the SQL statements only need to be changed in one place, in the database layer. In most cases, the application is not affected unless the procedure call requires modification. Changing the procedure call is also easier, because a standard call is already in place. Managing the data in the data layer avoids having to keep track of embedded SQL calls that may be different in each place, whenever a change is required.
Stored procedures provide improved performance because fewer calls need to be sent to the database. For example, if a stored procedure has four SQL statements in the code, then there only needs to be a single call to the database instead of four calls for each individual SQL statement. Of course there is always a tradeoff. There is an increased workload on the server side that needs to be taken into account.
Another advantage to using stored procedures allows for multiple client applications written in any language and running on any platform to have consistent database routines. Each application uses the same procedures and simply has to embed a standard procedure call for the language in the calling program.
SQL Script on the fly: In some applications having hard coded SQL statements is not appealing, because of the dynamic nature of the queries being issued against the database server. Because of this sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can be done quite simply from the application perspective where the statement is built on the fly whether you are using ASP.NET.
When we need to solve a tricky database problem, the ability to generate SQL statements is a powerful tool. A dynamic SQL statement is constructed at execution time, for which different conditions generate different SQL statements.              

Monday, May 12, 2014

Within a single IIS website is it possible to have 2 Web Applications running, where one is utilizes the .NET 2.0 frameworks while the other utilizes the .NET 4 framework? If so how

It is possible to run two different versions of ASP.NET in the same IIS process. For this we need to reconfigure the server using IIS Administration Tools to run the application in a seperate process.

What is the numeric IP Address for the localhost

127.0.0.1

What is the difference between executing a dynamically built SQL statement @SQL (NVARCHAR(4000) with “EXEC” statement as opposed to the “sp_executesql” statement

- sp_executesql gives the possibility to use parameterised statements, EXECUTE does not.
- Parameterised statements gives no risk to SQL injection and also gives advantage of cached query plan.
- The sp_executesql stored procedure supports parameters. So, using the sp_executesql stored procedure instead of the EXECUTE statement improve readability of your code when there are many parameters used.
- When you use thesp_executesql stored procedure to executes a Transact-SQL statements that will be reused many times, the SQL Server query optimizer will reuse the execution plan it generates for the first execution when the change in parameter values to the statement is the only variation.
- sp_executesql can be used instead of stored procedures to execute a Transact-SQL statement a number of times when the change in parameter values to the statement is the only variation. Because the Transact-SQL statement itself remains constant and only the parameter values change, the SQL Server query optimizer is likely to reuse the execution plan it generates for the first execution.

Console Output

Question: What would be the output of the following block of code: 
     DECLARE @TEMP varchar(100)
     SET @TEMP = NULL

            IF @TEMP = ''
                PRINT('A')
            IF LTRIM(RTRIM(@TEMP )) = ''
                PRINT('B')
            IF @TEMP IS NULL
                PRINT('C')
            IF @TEMP = NULL
                PRINT('D')
            IF ISNULL(@TEMP,'') = ''
                PRINT('E')
        
Answer:
The code will not compile and will generate an errors. Error in first line.

What is the possible value range of a tinyint variable

0-255. Storage size is 1 byte.

Console Output

Question: Write out the output (what would be written to the console), when this following block of code is run:
            public class ZipData
                {
                    string ZipCode { get; set; }
                    string CityName { get; set; }
                    string StateName { get; set; }
                }

                public static class ZipHelper
                {
                    public static string WhatsMyCity(string Zip)
                    {
                        //Returns a string representing the City for the Zip provided
                        //Returns null if string is not a zip or not found in database
                    }
                    public static string WhatsMyState(string Zip)
                    {
                        //Returns a string representing the State for the Zip provided
                        //Returns null if string is not a zip or not found in database
                    }
                } 
            
Answer:
WhatsMyCity and WhatsMyState are not returning anything so the program will not compile.

Console Output


Question: Please write out the output (what would be written to the console), when this following block of code is run:

                      object x = null;
                      if (x.Equals(null)) Console.WriteLine("null");
                      else Console.WriteLine("not null");
      
 
Answer: Unhandled Exception: System.NullReferenceException

Console Output

Question: Write the output (what would be written to the console), when this following block of code is run:

 static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("A");
                // Some Code Here
                Exception ex = new Exception("123456");
                throw (ex);
                Console.WriteLine("B");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Console.WriteLine("Done");
                Console.ReadLine();
            }
        }
        
Answer:
A
System.Exception: 123456
Done

Describe the difference between an Abstract Class and an Interface

- An Abstract class doesnot provide full abstraction but an interface provides full abstraction. We can declare and define methods in an abstract class but only declare in an
interface.
- Abstract class does not allow multiple interitance while an interface allows multiple inheritance.
- Interface does not allow to declare member field.
- In Interface the default access modifier is public. Private, protected, internal access modifer are not allowed.
- Keywords static, virtual, abstract or sealed are not allowed in an interface.

Describe the difference between a "Static" and "Non-Static" method

- A static method can be accessed directly from a class, while a non-static method is accessed from the class instance. In other words we can say, static methods are accessed by their class names which encapsulates them, while non-static methods are accessed by object reference. Static methods cannot access instance methods or variables within a class.
- Static methods are one per class while non-static methods are one per instance.
- Static methods cannot use non-static methods and variables without object instantiation while non-static methods can use static methods directly.

Describe the difference between a Data Reader and a DataSet

- Data Reader is used to read the data from the database and it is a read and forward only connection oriented architecture while fetching data from database. DataReader fetches data faster as compared to dataset. Usually ExecuteReader object is used to bind data to DataReader.

- DataSet is a disconnected oriented architecture i.e. active connections are not required while working with datasets. DataSet is a collection of DataTables and relations between tables. It is used to hold multiple tables with data. DataSet can hold data from tables, views. Data in dataset can be saved in XML format.