when does an DSLR camera help over point and shoot one

How would it help in having a DSLR , than a point and shoot?

A point and shoot camera does not allow the user to control the shutter speed and the aperture. The camera decides whether the settings are perfect to get a a good quality picture (i.e proper bright picture) and then does auto focus and takes the snap. In such conditions it is difficult to carry out activities such as panning, low shutter photography or high speed photography (i.e freezing the frame). All this things might get caught if you have a stint of luck on your side (i.e just clicking the button and hoping that you might get one of these effects as controlling the settings are not possible.) But in SLR this is possible.  Point and shoot are generally useful for landscape photography or still photography (i.e people standing still – note that this is different from high speed photography where capturing a frame of a fast moving object) .

Instances where you want to do panning and in the next minute freeze a fast moving object is not possible in point and shoot cameras… for that you would require to set the shutter speed and this is possible only in SLR cameras.

Advertisements

Controllers in ASP.Net MVC.

Reference : Understanding Controllers, Controller Action and ActionResult and ASP.Net MVC Controller Overview.

In ASP.Net MVC, The ‘M’ stands for Model, The ‘V’ stands for View and ‘C’ stands for Controller. The Controller is the most important part in the MVC triad.  Let us discuss more about Controllers, Controller actions and Action results  in this post.

So , What is a Controller?
In the most simplest terms, a controller is a module that is responsible for responding to a request (generally a browser request). Every request that is made to an ASP.Net MVC application is mapped over (routed) to a particular controller ( in other words, every controller is designed to handle\ serve a specific kind of request). Hence a controller is actually something that controls our interaction with an ASP.Net MVC application or we can say that a controller is responsible for controlling the interaction of an ASP.Net MVC application.

The controller serves every request that it receives by performing an action. These actions are generally called as Controller actions. Every action that the controller performs generates a result. This result is then send back to the entity that initiated the request (mostly a browser). These generated results are called as Action Results.

So, Let us now understand what Controllers, Controller actions and Action Results are in ASP.Net MVC framework

Controllers
So , A controller is just a class that takes a request and then returns some response to the  request.  Every controller derives from a base Controller class which belongs to System.Web.MVCController.

Every controller class should end with the suffix ‘Controller’.  Eg: ProductController, SampleController etc..

Controller Actions
Every controller is made up of actions.  A controller action is the actual method that gets invoked when we make a request against the controller. The particular action that is to be called depends on what we type in the url  , in other words,  what functionality has to be performed by the controller is determined by the kind or type of url it gets.

However, there are few requirements that a controller action has to satisfy –

  1. An action is a method that is exposed by the controller. Hence, a controller action has to be a public function.
  2. Every controller performs a unique action i.e no two actions are ambiguous. Hence a controller can have only one version of action which means that a controller function cannot be overloaded (to have multiple versions of  the same function).
  3. A controller action cannot be shared i.e every instance of the controller will have its own action instance which means that actions should always be instance methods..
  4. A controller action can have any name i.e we can use any function that we want as a controller action.
ActionResult
Every action performed by a controller generates a result. This is called as ActionResult (i.e result of an action).  This result is returned back as the response or in other words every request that is made to the controller recives  an ActionResult as a response.
Every action can produce different kinds of result, hence, depending on this there are number of different types of ActionResult that a controller action might return – viz..
  1. ViewResult.
  2. RedirectResult.
  3. JSONResult.
  4. JavaScriptResult.
  5. ContentResult.
  6. FileContentResult.
  7. FilePathResult.
  8. FileStreamResult.
  9. EmptyResult.
(Do you smell an implementation of State Pattern in case of ActionResult and also Null Pattern in case of EmptyResult.)
The most common type of ActionResult that is returned is a View or a ViewResult.  So, what is a ViewResult? A ViewResult is an ActionResult that returns a View. A View contains markups, Html markups and some content that you want to send back to the browser.
Similiarly, there are different types of data that the action can return.
Typically, we do not create these results directly. What we do instead is that we call one of the methods of the base controller class (remember – our controller class inherits from System.Web.MVCController), to return the different types of results. The different types of Controller class methods available are :
  1. View.
  2. Redirect.
  3. RedirectToAction.
  4. RedirectToRoute.
  5. Json.
  6. JavaScript.
  7. Content.
  8. File.
So, if we want to return a ViewResult, we do not create a View, instead,  we return a View by calling the View() method of the controller base class.
Let us see how this works :
When  a View() method is called on the base controller class, it returns a ViewResult. Now, there is a specific pattern that is followed by the View method to find the ViewResult that is to be returned.
If no name of the ViewResult is specified then the View() method returns the  ViewResult that corresponds to the name of the Controller action i.e if the controller action from where View() is called is named Specification, then the View() method will try to return a ViewResult named “SpecificationView”, which should be located in the Views subfolder that corresponds to the name of the controller.
Eg :
if we have the following action in Controller named Product :
public ActionResult Specification()
{
    return View();
}
Then the ViewResult returned will be SpecificationView which should be located in Views>Product>Specification.aspx.
To do this we will add a subfolder in View folder having name of the Controller (i.e Product) and then add a MVC View page which should have the name of the action i.e Specification, in this case.

(to be continued …)

Routing in ASP.NET MVC

As discussed in the previous post, an ASP.Net MVC  application is a complete shift from traditional ASP.Net application.

A traditional ASP.Net application is a collection of webpages (generally aspx files) hosted on a server which can be requested by a client, while an ASP.Net MVC application is a system , which acts as  a service. It can be considered as a collection of methods\ activities that are executed on client request. The result of this execution is returned back to the client.

An ASP.Net application lifecycle mainly involves following activities :

  1. Routing incoming requests  –  performed by routing engine and router.
  2. Performing actions corresponding to the request  – performed by Controllers and Controller actions.
  3. Returning back the result to the client  -performed by  Views or ActionResults.
Let us first start with Routing.
URL ROUTING
URL routing is a critical component in ASP.Net MVC application. This is because URL routing is the one that determines how an incoming client (browser) request gets mapped onto a MVC controller and controller action i.e. in other words which controller will handle which request.
URL Routing uses something called as route table. A route table is nothing but a collection of routes. And what is a route?
Well, a route is a complete definition for how to dispatch a web request to a controller. So, basically a route holds an information of which request should be handled by which controller.
The following diagram shows a brief overview of how routing happens.
URL Routing module 
The URL routing is generally carried out by a URL routing module. The URL routing module is othing but an HTTPModule class in ASP.Net MVC. Hence a URL routing module is generally an instance of HTTPModule class.
The first thing to do in an ASP.Net MVC application is to register the URL Routing module i.e to assign an HTTPModule class as the URL Routing Module. This is generally done in the web.config file, present in the project solution. In the file, there is an element called as httpModules.
Making an entry in this httpModules element marks the registeration of URL Routing Module.  Generally, we do not have to register the routing module , as it part of the template which is created by the Visual Studio IDE during the creation of the project.
As discussed before, the Url Routing Module is implemented by creating an HTTPModule instance. The HTTPModule  interscepts all the request coming into your MVC web application.

Always be careful not to delete this module section from the web.config file as everything would stop working.

Route Table 
The URL routing module takes advantage of something called as a “route table” and the route table is used to do the mapping between the incoming request and MVC Controller action.

The route table is generally setup and manipulated in the global.asax file.  The global.asax file generally handles the application life cycle events such as Application start, Application end.

The route table is setup in the Application Start event through a function called as RegisterRouter.  The RegisterRoute  sets up the route table which contains just one route entry.

We create the route in the route table by calling the MapRoute method. In this case, the MapRoute method is creating a “default route” which is used by the MVC application. This “default route” is used to handle around 85% of incoming requests.

There are different types of System defined routes, “default” route is one of them. You can also create your own routes too. We will have a look at this later in the blog.

Getting back to MapRoute, MapRoute is used to make a route entry in the route table. It takes three parameters, viz. ,

  • Route name: There are some unique ways to identify different routes in the routes table. “default” route is one of them, which is used to identify a default route i.e if no matching route is obtained for an incoming request then the default route is called.
  • Url : This parameter does two things :
    1. It sets the pattern of the request that this route is going to intercept i.e what kind (pattern)  of incoming request will this route act on.
    Eg: In case of the entry MapRoute (“Default”, {controller}/{action}/{id}, new {controller=”Home”, action=”Index”, id=””}), it says that the “Default” route will handle a request which will have the pattern {some xxx}/{some xxx}/ {some xxx} – such as http://localhost:8080/Samples/Entry/9.2. The other thing that this url parameter does is map different segments in the Url to different parameter names.
    Eg: In case of the entry MapRoute (“Default”, {controller}/{action}/{id}, new {controller=”Home”, action=”Index”, id=””}), The first value should be stored in a parameter (or variable) called as controller,  the second value should be stored in a parameter (or variable) called as action and third value should be stored in parameter(or variable)  called as id. So in http://localhost:8080/Samples/Entry/9 – Samples will be stored in a parameter (or variable) called as controller, Entry will be stored in a parameter (or variable) called as action and 9 will be stored in a parameter (or variable) called as id.In short : MapRoute (“Default”, {controller}/{action}/{id}, new {controller=”Home”, action=”Index”, id=””}) ,says,
    The URL will be in the form {firstvalue}/{secondvalue}/{thirdvalue} where first value will be called controller, second value will be called action and third value will be  called id.
  • Defaults Value : This 3rd parameter sets the default values to each of the parameters established \ created in the 2nd parameter. So in case if either of the parameter does not have any value then the take on these default values.  Eg:  In case of the entry MapRoute (“Default”, {controller}/{action}/{id}, new {controller=”Home”, action=”Index”, id=””}),  controller, action and id are the parameter names or variables created in second parameter. So if the incoming request has no value for controller then the controller will have value “Home” ,  Similarly if  request has no corresponding value for action then the action will have value “Index” and Similarly if no corresponding value is found for id then id will be ” ” (empty).
So, in case if the browser makes a request http://localhost:8080/ then the request is executed as if http://localhost:8080/Home/Index/ was called, because the default values are “Home” for first value, “Index” for second, hence as no value were specified the incoming request was converted to http://localhost:8080/Home/Index/.
If the above concept is understood, then lets try intercepting one more route.
MapRoute(“ArchiveRoute”, Archive/{entrydata}, new {controller=”Archive”,  action=”Entry”})
This route is made to handle any request which starts with Archive. Eg: http://localhost:8080/Archive/….
The secondvalue  i.e the one after Archive will be represented by entryData (0r will be stored in a variable called entryData).
The default values for controller will be “Archive” and for action will be “Entry”.
controller and action are predefined parameters while entryData is userdefined parameter which can be accessed as a parameter in the ArchiveController’s  entry function (or action).

We will make this new entry using MapRoute method in the RegisterRoutes function. Now the routes table works on the basis of  First Match Algorithm i.e the first matching route is executed. Hence if default route is kept as the first entry in the table then all the incoming request will be intercepted using the default route or in other words will be mapped to this route. This is because the default route is the one which has to be executed if no matching routes are found. Hence always add “default” route at the end so that it gets executed if no relevant route is found.
 
(To be continued…)

MVC on web – A complete shift in concept?

References : ASP.Net MVC in action and Why ASP.NET MVC

The beauty of MVC  (Model – View – Controller) design lies in the distribution of responsibilities it provides due to its architecture.  This has been the sole reason why MVC has dominated as the sole design pattern for simplifying monolith applications having extensive UI and business logic.

MVC is generally used to refactor huge UI classes that hold all the UI events along with the related application logic.  The latest web application framework to adopt this pattern is ASP.Net. The result of this is ASP.Net MVC.

However, the use of  MVC brings a complete change in the concept of web applications.  Let us understand what is this change:

Request – Response flow

  • A general ASP.Net application is a collection of  files(generally aspx files) hosted on the web server at  a specific location ( path).
  • A client which wants to access any of these files requests for the file by specifying the location.
    Eg :  A file help.aspx which is kept in folder /Samples on the server is requested as follows – http://server:port/Samples/help.aspx which raises the following request  GET /Samples/help.aspx
  • The server responds by returning the contents of the aspx files in HTML format, which is then rendered by the client mostly a browser.

Hence, an ASP.Net application is nothing but a collection of aspx files which are hosted on the server. Whenever a request is made one of these files are send back to the client (This is nothing but the flow of HTTP  GET command) . 

However, the introduction of MVC changed this complete scenario.  An ASP.Net application is no more a collection of  aspx files. It is now a service which holds executable methods (also called as actions ).  The client instead of requesting for an aspx file, now needs to request the execution of one of these actions. The result of this execution is then returned back to the client. Let us see how this works.

MVC  (Model – View – Controller)

Let us first begin by understanding – what is MVC and how it is implemented in ASP.Net MVC.

The main idea behind an MVC pattern is to seperate the control logic from the user interface. The MVC pattern divides the ASP.Net application into three parts – Model, View and Controller.

  • The Controller acts as the brain of the application and holds most of the application logic.
  • The View handles\ manages the display of information. In other words,  it represents the user interface (web pages) of the application.  However the views are dumb i.e they do not hold any domain logic (application logic) and are decoupled from any data access calls (i.e no data access calls or other kind of calls can be made from the view). Hence, the UI can stay the same, even if there is a change in the domain or data access logic, within the application.
  • The Model represents the data source. This could be databases, text files, XML files etc.  Every element related to fetching, storing or manipulation of information comes under Model. 
These three components are often referred to as MVC triads.  
  • The controller is the most important component, which is also incharge that decides what model objects to use and what views to render.
  • The controller is in charge of coordination and is the first to get executed, when a web request comes to the application.
  • The controller is responsible for deciding what response is appropriate for the request

Let us now understand how the MVC has changed the face of ASP.Net web application.

  1. As said earlier, the ASP.Net application is no more a collection of  aspx files,  but it is now a service which carries out activities on requests.
  2. An ASP.Net MVC application is internally, a collection of  MVC triads.
  3. When a request comes in, it is handled by a routing engine. The routing engine decides what controler and action should it use to serve that request. The actions are simply public methods on a controller.
  4. Once the actions are executed, steps relevant to that actions are  carried out and the control is handed over to the Views, where the data is rendered to HTML and returned to the client.
  5. Hence every action is performed by a controller.  The final result of this action is generally a View which is returned back to the client. Hence Views are also called as ActionResult (i.e result of an action) in ASP.Net MVC.

Hence, in case of ASP.NET MVC, a client is no more requesting a resource from the server, but its requesting the execution of an action (method) on the server, the result of whcih is returned back to the client in the form of HTML.

So, ASP.NET application (using ASP.NET MVC framework) works with methods and not files.

Evolution of Microsoft web technologies

The biggest challenge that was faced in web based applications was building of interactive websites.

This post tries to summarize how Microsoft tried to solve this challenge with various technologies, the problems that arised in each of these  and how these problems were solved by their successor. (Reference : ASP.Net MVC in Action, 2nd Edition)

CGI (Comman Gateway Interface)

The first solution to this challenge was to build websites using executable programs that ran on a server. The programs (also called as scripts) accepted a web request and were responsible for creating an HTML response.

The technologies used initially to write these programs (scripts) were CGI (Comman Gateway Interface).

Drawbacks of CGI 

  1. Adhoc Templating.
  2. Programs were difficult to write, debug and test.

ASP (Active Server Pages)

In late 1990s, Microsoft introduced Active Server Pages (ASP).

ASP ran on the concept of having server pages (pages that got executed on server side) , which was nothing but an HTML document that was embedded with dynamic scripts (code that got executed at runtime) . So an ASP page was a mixture of (or made up of)  HTML markups and scripts where the HTML markups were used to handle the User interface (or presentation of the page) and scripts were used to handle the the action (program logic) of the page.

Advantages

  1. ASP brought templating  to web applications.  (not clear what templating is.)

Drawbacks

  1. As the executable script had to be embedded \ mixed in with the HTML document, the world soon saw massive server pages with code indecipherable (inseparable \ unidentifiable)  from the HTML markups.

ASP.Net

In early 2002, Microsoft launched ASP.Net and the Web Forms Framework.

Web Forms was a huge shift as it moved most of the program logic, which was previously embedded between HTML markups, into a class file ( called as code behind ) and replaced the HTML markups with something called as dynamic server controls, which were written in an XML syntax. 

Hence in ASP.Net the following changes were made :

  1. XML based dynamic server controls instead of HTML markups.
  2. Program logic was to be written in class files called as code behind, instead of embedding them between markups.
Advantages
  1. Performance of web applications increased.
  2. Debugging experience improved.
Complications
  1. ASP.Net introduced the concept of postback event lifecycle , which saw heavy query mails on newgroups, as confused developers searched for that magic event in which to add those two simple lines of code that was necessary to make the page work as needed.
  2. The concept of ViewState, which although good in theory, broke down as the application scaled with complexity.
  3. Simple pages surpassed 100kb in size, as the entire state of the application had to be stored in the output of every generated page.
  4. Development best practices were ignored as tools like Visual Studio encouraged data access concerns (data access activities) such as SQL queries to be embedded within view logic.
  5. The biggest sin of Web Forms Framework was the tight coupling to everything in the System.Web namespace.
  6. There was no scope of unit testing any code in code-behind file, due to the way the flow of  ASP.Net pages was designed (eg .. any task that was to be automatically performed in the page had to be written in the  Page_Load event) and due to this we saw Page_Load methods that took several trees to print.
Although there were these complexities, the .Net framework had made huge inroads into the web application market. The combination of ASP.Net platform along with IIS on Windows proved to be a hit as they could support huge complex web applications running in large data centers.
Hence we could see that there were few aspects of ASP.Net that were not feasible for development and major aspects that were quite commendable.
Thus it was necessary to come out with a framework rather than a new technology that used the strong aspects of ASP.Net but meanwhile also simplified the unnecessary complication introduced in ASP.Net. The answer to this was ASP.Net MVC.
ASP.Net MVC
The ASP.Net MVC Framework is a framework made using ASP.Net and runs on top of ASP.Net.  It simplifies the complex parts of Web Forms application development while retaining the power and flexibility of the ASP.Net pipeline.
Enhancements
  1. Seperation of Concerns Using MVC Architecture
    • The ASP.Net MVC works on the concept of seperation of concerns (i.e seperation of responsibilities).
    • This is done by providing support for development of ASP.Net applications using Model – View – Presenter (MVP) web presentation pattern.
    •   With this the concerns of the data model, the application logic and data presentation are clearly separated.   The application logic is kept in a class which is seperated from the hard dependencies on how the data will be presented.  Server pages have become simple views, which are nothing more than HTML templates waiting to be populated with objects(models) passed in by the controller.
  2. The postback event lifecycle is no more and ViewState is no longer necessary.
So the evolution started from CGI and has currently reached ASP.Net MVC. The next posts would be mostly about  my learnings on ASP.Net and ASP.NET MVC.

Generics – Method Declarations

Generic methods are used in situations where we perform similar operations on different types of data. In normal scenario this is done by using function overriding and creating separate functions for each type.

Let us have a look at an example, which prints the array of  different types :

public void PrintArray(int[] inputArray)
{
foreach(int input in inputArray)
{
Console.Write(input +  ”  ” );
}

}

.. for float types we will have to override the function as follows,

public void PrintArray(float[] inputArray)
{
foreach(float input in inputArray)
{
Console.Write(input +  ”  ” );
}
}

.. for double types we will have to override the function as follows,

public void PrintArray(double[] inputArray)
{
foreach(double input in inputArray)
{
Console.Write(input +  ”  ” );
}
}

The function will be called as follows :

PrintArray(integerArray);

PrintArray(floatArray);

PrintArray(doubleArray);

So we can see that we now have three implementations of same function for different types.  This can be solved by the help of Generic methods. Using Generic methods we can create one single function and pass the required types as parameters to this function.

Let us see how this is done:

public void PrintArray<E>(E[] inputArray)
{
foreach(E input in inputArray)
{
Console.Write(input  + ” “);
}
}

where ‘E’ represents the type of array that we will be passing to the function. The function will be called in this way, with different types :

PrintArray<int>(integerArray);

PrintArray<float>(floatArray);

PrintArray<double>(doubleArray);


Generic declaration
A generic method is declared in the same way as a normal function, except that, the actual parameter types, are replaced with  placeholders called as “type parameter”.

Eg : Instead of

PrintArray(int[] integerArray)
{
– – – – –      ;
– – – – –      ;
– – – – –      ;
}

we write

PrintArray(E[] inputArray)
{
– – – – –      ;
– – – – –      ;
– – – – –      ;
}

Now, before specifying the type parameter, we need to create a type parameter list in the function declaration.  The type parameter list is a list of type parameter that we will be using in the generic function.  Type argument list can be considered as  a way of  announcing in advance that these will be the placeholders we will be using in the function, so do not give compilation errors when you encounter them.

The type parameter list is declared as follows:

PrintArray<E>(E[] inputArray)
{
– – – – ;
– – – – ;
}

The <E> portion in the above declaration is the type parameter list.  The type parameter list is delimited by the angular brackets and follows the method’s name.  Each type parameter list contains one or more type parameters, separated by commas.

As mentioned before, a type parameter is an identifier that is used in place of actual type names. The type parameters can be used to declare the return type, the parameter types and the local variable types in a generic method declaration.

The type parameter names throughout the method declaration must match those declared in the type parameter list.

Also, a type parameter can be declared only once in the type parameter list but can appear more than once in the method’s parameter list. Type parameter names need not be unique among different generic methods.

A generic method having multiple arguments is declared as follows:

void PrintKeyAndValue<TKey, TValue>(TKey key, TValue value)
{

Console.Write(“Key :  “);
Console.WriteLine(key.ToString());

Console.Write(“Value :  “);
Console.WriteLine(value.ToString());
}

A generic method having a return type is declared as follows:

TValue returnPassedArgument<TValue>(TValue value)
{

return value;

}

Beginning ASP.Net

Knowing atleast one web – based technology is mandatory for any developer, as it proves out to be a great weapon in his armor. I thought of learning ASP.Net for one obvious reason that i am from a C# background. Well though have heard about newer languages such as Ruby, ASP.Net MVC… thought of being comfortable with one and then moving ahead…..