Thursday, September 29, 2005

.NET Remoting Interview Questions

Came across a MSDn blog with these questions. They are quite useful.

What’s a Windows process?
It’s an application that’s running and had been allocated memory.

What’s typical about a Windows process in regards to memory allocation?
Each process is allocated its own block of available RAM space, no process can access another process’ code or data. If the process crashes, it dies alone without taking the entire OS or a bunch of other applications down.

Explain what relationship is between a Process, Application Domain, and Application?
A process is an instance of a running application. An application is an executable on the hard drive or network. There can be numerous processes launched of the same application (5 copies of Word running), but 1 process can run just 1 application.

What are possible implementations of distributed applications in .NET?
.NET Remoting and ASP.NET Web Services. If we talk about the Framework Class Library, noteworthy classes are in System.Runtime.Remoting and System.Web.Services.

What are the consideration in deciding to use .NET Remoting or ASP.NET Web Services?
Remoting is a more efficient communication exchange when you can control both ends of the application involved in the communication process. Web Services provide an open-protocol-based exchange of informaion. Web Services are best when you need to communicate with an external organization or another (non-.NET) technology.

What’s a proxy of the server object in .NET Remoting?
It’s a fake copy of the server object that resides on the client side and behaves as if it was the server. It handles the communication between real server object and the client object. This process is also known as marshaling.

What are remotable objects in .NET Remoting?
Remotable objects are the objects that can be marshaled across the application domains. You can marshal by value, where a deep copy of the object is created and then passed to the receiver. You can also marshal by reference, where just a reference to an existing object is passed.

What are channels in .NET Remoting?
Channels represent the objects that transfer the other serialized objects from one application domain to another and from one computer to another, as well as one process to another on the same box. A channel must exist before an object can be transferred.

What security measures exist for .NET Remoting in System.Runtime.Remoting?
None. Security should be taken care of at the application level. Cryptography and other security techniques can be applied at application or server level.

What is a formatter?
A formatter is an object that is responsible for encoding and serializing data into messages on one end, and deserializing and decoding messages into data on the other end.

Choosing between HTTP and TCP for protocols and Binary and SOAP for formatters, what are the trade-offs?
Binary over TCP is the most effiecient, SOAP over HTTP is the most interoperable.

What’s SingleCall activation mode used for?
If the server object is instantiated for responding to just one single request, the request should be made in SingleCall mode.

What’s Singleton activation mode?
A single object is instantiated regardless of the number of clients accessing it. Lifetime of this object is determined by lifetime lease.

How do you define the lease of the object?
By implementing ILease interface when writing the class code.

Can you configure a .NET Remoting object via XML file?
Yes, via machine.config and application level .config file (or web.config in ASP.NET). Application-level XML settings take precedence over machine.config.

How can you automatically generate interface for the remotable object in .NET with Microsoft tools?
Use the Soapsuds tool.

Basic C# fundamentals

In today blog post i want to discuss various general C# terms, which are useful for any .NET related interview.

Remember C# does not support multiple inheritance. But yes .NET does support multiple interface inheritance. It means that a class can implement more than one interface. The methods declared in an interface are implicitly abstract. If a class implements an interface, it becomes mandatory for the class to override all the methods declared in the interface, otherwise the derived class would become abstract.

e.g. Inheritance
class MyNewClass : MyBaseClass

Can you prevent your class from being inherited by another class?
Yes. The keyword “sealed” will prevent the class from being inherited.

Can you allow a class to be inherited, but prevent the method from being over-ridden?
Yes. Just leave the class public and make the method sealed.

What’s an abstract class?
A class that cannot be instantiated. An abstract class is a class that must be inherited and have the methods overridden. An abstract class is essentially a blueprint for a class without any implementation.

What is an interface class?
Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes.

What’s the difference between an interface and abstract class?
In an interface class, all methods are abstract - there is no implementation. In an abstract class some methods can be concrete. In an interface class, no accessibility modifiers are allowed. An abstract class may have accessibility modifiers.

What is the difference between a Struct and a Class?
Structs are value-type variables and are thus saved on the stack, additional overhead but faster retrieval. Another difference is that structs cannot inherit.



How does C# achieve polymorphism?
Polymorphism is also achieved through interfaces. Like abstract classes, interfaces also describe the methods that a class needs to implement. The difference between abstract classes and interfaces is that abstract classes always act as a base class of the related classes in the class hierarchy. For example, consider a hierarchy-car and truck classes derived from four-wheeler class; the classes two-wheeler and four-wheeler derived from an abstract class vehicle. So, the class 'vehicle' is the base class in the class hierarchy. On the other hand dissimilar classes can implement one interface. For example, there is an interface that compares two objects. This interface can be implemented by the classes like box, person and string, which are unrelated to each other.

What does the keyword “virtual” declare for a method or property?
The method or property can be overridden.

How is method overriding different from method overloading?
When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same name within the class.

Can you declare an override method to be static if the original method is not static?
No. The signature of the virtual method must remain the same. (Note: Only the keyword virtual is changed to keyword override)

How is method overriding different from method overloading?
When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same name within the class.

Can you declare an override method to be static if the original method is not static?
No. The signature of the virtual method must remain the same. (Note: Only the keyword virtual is changed to keyword override)

Tuesday, September 27, 2005

SQL Server 2005 and Visual Studio 2005 UK Launch

The launch of Visual studio 2005 and SQL Server 2005 in UK
Check this out:

Joke

I just came across a joke regrading programmers on dotnetjunkies.



The girl gets irritated with the smoke and says to her lover:

"Can't you see the warning written on the cigarette packet,

Smoking is dangerous to your health?"

The boy replies back

"Darling, I am a programmer, we don't worry about

WARNINGS, we only worry about ERRORS!

By skiff

Monday, September 26, 2005

Test Driven Development in .NET - NUnit forms and NunitASP

Continuing the discussion further on Test Driven Development, I came across two tools just a month back. And yes they are NUnitforms and NunitASP.

NUnitASP
NUnit is an indispensable tool. I use it as part of test driven development for all the applications i develop.But testing the the .aspx pages themselves is a problem. How can you test that the UI reacts to the user correctly? That the postback events happen in the right order? That the correct next page is loaded after the user completes the page? Testing these things requires that your code is running inside the ASP.NET worker process. Your pages need access to the HTTPContext, the Request and Response objects, everything else that ASP.NET provides them at runtime. If you attempt to test your compiled .aspx pages directly from the NUnit test runner, none of them will even load, let alone pass your tests.
Ref: Testing ASP.NET Applications with NUnitASP and NUnit. The server side.net

NUnitAsp is a tool for automatically testing ASP.NET web pages. NUnitAsp is for unit testing ASP.NET code-behind only. It's meant for programmers, not QA teams, and it's not very good for QA-style acceptance tests. It only tests server-side logic. JavaScript and other client-side code is ignored. But if you're using ASP.NET, it's an essential part of your programmers' toolset.

NUnitAsp is just an assembly, it'll work with all .NET languages, so yes it will work with VB.NET.

Controls supported in version 1.5.1:

System.Web.UI.WebControls
Button
CheckBox
DataGrid
DropDownList
ImageButton
Label
LinkButton
ListBox
Panel
RadioButton
TextBox
UserControl
ValidationSummary

System.Web.UI.HtmlControls
HtmlAnchor
HtmlInputCheckBox

NUnitForms
NUnitForms is an NUnit extension for unit and acceptance testing of Windows Forms applications. Support is already built in for Buttons, Labels, TabControls, and Context Menus, with many others coming soon. It is very easy to add support for additional types of controls.

NUnitForms also provides a Recorder application that will Record your interactions with a Windows Forms class and write a Unit Test for you that duplicates your actions. You can perform asserts during this process by right-clicking on your controls and selecting the property to assert.

Currently i am trying to use these tools and in my next blog entry, i would post some code to show as to how it works.

Friday, September 23, 2005

Test Driven Development in .NET - NUnit

I heard about Test Driven Developemnt for the first time when i was working on my coursework to develop an instant messenger back in October 2004. One of the requirements for coursework completion was to have unit tests for the piece of code to be written using NUnit. Thus i heard the word NUnit.

There are several articles available on thw web about Test driven developemnt and NUnit.

In this blog post, I am going to summarize my expereince and what i have learned till now regarding Test Driven Development.

The first question is
"What is Test Driven Development?"

According to me, it is a three step process:
- Write test,
- Write code,
- Refactor.
Putting it in another way:
Instead of designing a module, then coding it and then testing it, we turn the process around and do the testing first. The typical programming sequence is something like this:
- Write a test.
- Run the test. It fails to compile because the code you're trying to test doesn't even exist yet! (This is the same thing as failing.)
- Write a bare-bones stub to make the test compile.
- Run the test. It should fail. (If it doesn't, then the test wasn't very good.)
- Implement the code to make the test pass.
- Run the test. It should pass. (If it doesn't, back up one step and try again.)
- Start over with a new test!
(Ref:Test-Driven Development in .NET By Peter Provost)

Reasons for Test Driven Development (TDD)
A unit test provides an example of what a piece of code is supposed to do. It gives a much clearer idea as to how the code is executing, once a particular set of input is provided. Going through the code in the debugger only tells us what it is doing, not what it is supposed to do. Moreover when we fix any bug in the code or if we change even a single line of code, we can never be sure as to how that change would affect the rest of the code. Many times it happens that one fix leads to another bug. In this scenario unit tests can be very useful, because they allow checking to analyze if we broke something else in the code. Also Test Driven Development leads to the overall improvement of the design of the application, because it makes the developer think right form the starting point as to how the application should behave and thus how should it be designed.

What are Unit tests?
“A unit test verifies that a function or set of functions "honors its contract" - in other words, that the function(s) under test meet the requirements. Unit tests inspect both black boxes and white boxes.”

NUnit test Framework
NUnit is an application designed to facilitate unit testing. It consists of both a command line and Window's interface, allowing it to be used both interactively and in automated test batches or integrated with the build process.

Thursday, September 22, 2005

Error handling in Stored Procedures

I had an intrview today and the interviewer asked me about Error handling in SQL Server 2000. I am afraid to say that i could not answer as i had ignored this aspect for a long time. So today i decided to get my head around Error handling related to stored procedures.

I have used the help of article

http://www.sqlteam.com/item.asp?ItemID=2463

There are two type of errors in SQL Server: fatal and non-fatal.

Fatal errors cause a procedure to abort processing and terminate the connection with the client application.

e.g. The SELECT in the procedure references a table that does not exist, which produces a fatal error.

Non-fatal errors do not abort processing a procedure or affect the connection with the client application. When a non-fatal error occurs within a procedure, processing continues on the line of code that follows the one that caused the error.

The actions that cause a fatal error are not well documented. Each error has an associated severity level that is a value between 0?25. The errors with a severity level of 20 or above are all fatal, but once we get below this value there is no well-defined rule as to which errors are fatal. In truth, though, worrying about which errors are fatal is a bit useless because there is no code we can implement that will allow you us to handle them gracefully.

So how do we implement error handling code for these errors?

@@ERROR
The @@ERROR system function is used to implement error handling code. It contains the error ID produced by the last SQL statement executed during a client's connection. When a statement executes successfully, @@ERROR contains 0. To determine if a statement executes successfully, an IF statement is used to check the value of the function immediately after the target statement executes.

IF @@ERROR <>0
BEGIN
PRINT 'Error Occured'


RAISERROR

The RAISERROR statement is used to produce an ad hoc error message or to retrieve a custom message that is stored in the sysmessages table.We can use this statement with the error handling code to implement custom error messages in our applications.

To go into more depth, we can also do error handling using cursors.
Refer:

http://www.sommarskog.se/error-handling-I

Wednesday, September 21, 2005

LINQ.Its Amazing

I just came across the LINQ project two days back. Though did not have much time to try it out, but just by reading about it has got me really excited.
LINQ stands for Language Integrated Query.
LINQ project provides the ability to add general purpose query facility to the .NET framework.It can be used to query relational data sotres without leaving the compile time environment of the programming language being used.Thus we can make use of the existing CLR types to acress SQL Database directly frm or C# code. Wow!!!
Read more about it here:
http://msdn.microsoft.com/netframework/future/linq/default.aspx?pull=/library/en-us/dndotnet/html/linqprojectovw.asp

Also worth watching is the Anders Hejlsberg interview on Channel 9 regarding LINQ:
http://channel9.msdn.com/showpost.aspx?postid=114680