Depending on the kind of solution you are developing, you use different entry points into the object model to obtain the appropriate objects(SPWebApplication, SPSite or SPWeb). Below is the graphical representation of the SharePoint Server Object Model hierarchy.
Physically, SPWebApplication represents an IIS load-balanced website containing a custom web.config and a standard set of SharePoint specific files and virtual directories including the _layouts directory and the
_controltemplates directory .
To get reference to target Web Application, you can use :
Uri webAppUrl = new Uri("http://sp2010site");
SPWebApplication webApp = SPWebApplication.Lookup(webAppUrl);
SPSite and SPWeb are fundamental types in the Server Object Model. They represent a site collection and a website, respectively.
By and large, To work with SharePoint from a browser-based application, your code must first establish the site context or site collection context for requests sent to the server.
To return the current site collection, you can use :
SPSite site = SPContext.Current.Site;
To return the web site of the current request, you can use :
SPWeb web = SPContext.Current.Web;