Friday, June 27, 2014

UML Class Diagram in 10 Steps Using Microsoft Visio 2010
This article describes how to create a UML Class Diagram in 10 steps. In this article I have created a sample class diagram using Microsoft Visio 2010. So there might be a need of Visio to create it at your end. Though I'll share screen shots that might be helpful to understand UML class diagrams.
You can also have a look at MVC related articles here:
  1. Smart Working With Custom Value Providers in ASP.NET MVC
  2. EXECUTION ORDER OF FILTERS IN MVC 4 WITH PRACTICES: IMPORTANT FAQ
  3. Exception Handling at Filter Level Using IExceptionFilter in MVC4
  4. Invoke Action With Model Binders in MVC
  5. Extension Helpers Method in MVC
  6. Custom Button With TagBuilder Using MVC Razor Engine
  7. RETURN MULTIPLE MODELS IN SINGLE VIEW IN MVC3
  8. Precompiled Razor View Using RazorGenerator MVC and PreCompiledViewEngine in MVC 4
Let's now explore how to create a Class Diagram step-by-step.
Class diagram

create Class diagram
Step 1: Kindly open Microsoft Visio 2010 from Microsoft Office as in the image below:

Microsoft office

Step 2: Please select New and then select Software and the database option from the template categories as shown in the image below:

template categories

Step 3: Kindly select UML Model Diagram from the template option and click OK. Kindly look at the image depicted below for reference.

UML Model diagram

Step 4: You will get a default look and feel after opening the Visio 2010 as shown in the image below:

Visio 2010
Step 5: Select a class option and drag it onto the page as in the image depicted below:
class option

Step 6: I have dragged a class and renamed it to C#Corner. Kindly have a look of the image depicted below:

image

Step 7: Here I created an attribute as AuthorName as private. Kindly refer to the more descriptive image below:
attribute
It will be a string type and looks such as in the image depicted below.
string type
You can create as many as you like.

Step 8: Create an operation as you need. Here I created an operation "AuthorDetails" also shown in the image depicted below:
AuthorDetails
An operation/method will be a public and returns a string. Kindly see the image shown below:

operation

Step 9: There are three main notations that are generally used (public, protected and private) in a Class Diagram; they are “+”, ”#” and ”-“ respectively. It applies both on attributes and operations also.

Step 10: To understand the three sections of the Class Diagram, kindly see the image depicted below:

Class diagram

I tried to make it simple so anyone can understand. I hope you enjoyed this demonstration:
enjoyed
I hope you enjoyed this and it may help you down the line.

Tuesday, June 24, 2014

Exception Handling at Filter Level Using IExceptionFilter in MVC4

 

MVC

 

This article describes how to return a desired view from an IExceptionFilter in MVC with practices. It is a part of exception handling at the filter level. Custom filters and attributes are an excellent way of injecting extra processing logic into the MVC request response pipeline. This is another most frequently asked question.


frequent question asked


In MVC applications the user first lands on a routing module and matches the URL to best fit the route collection, this module parses the request and invokes the respected controller and action and then the controller renders the view in the browser.
Now somewhere we would like to inject some pre-processing and post-processing logic for actions and controllers. In that case we use filters. IExceptionFilter is also a part of filters.
You can also have a look at articles related to MVC here:

 

 

Result filter
This filter executes before and after the result of the action method has executed. We can use this filter if we want some modification to be done in the action's result.

  • OnResultExecuting occurs just before the result is executed (before the view is rendered)
  • OnResultExecuted occurs after the result is executed (after the view is rendered)

To implement the result filters we need to create a custom filter attribute class and implement the IResultFilter interface. This interface provides the two methods OnResultExecuting and OnResultExecuted.
OnResultExecuted
When we execute the code we get an exception as shown in the image below:

execute the code
image
Realax
We can handle an error using an Exception Filter.
This filter will be invoked whenever a controller or action of the controller throws an exception. This is particularly useful when we need a custom error logging module.
To implement this filter we need to create a custom filter attribute class that implements an IExceptionFilter. Here we have also set the ExceptionHandled property to let the filter know that an exception has been handled at the filter level. Kindly have a look at the image depicted below:
filter level
Here I've also added a code segment to handle an error at the OnException method level.
OnException method
We can easily identify an error and related details after placing the code segment into an OnException filter. Kindly see the image shown below:
into OnException filter
Now press F5 and see the output to understand the execution order of filters. Please see the depicted image below.
As shown in the above image we have been redirected to a Custom Error page from the action level. I hope it will help you to understand the exception handling in MVC.

I hope you enjoyed this and that it may help you down the line.
You can download related code from given below link:

 

Exception Handling at Filter Level Using IExceptionFilter in MVC4

 

Keep coding and Smile.

Wednesday, June 18, 2014

Expanding the Results View Will Enumerate the IEnumerable Using DB Context

image
This article describes the most frequent terms confronted by software geeks during LINQ execution. Let's understand the term “results view will enumerate the ienumerable”. Initially it may be slightly confusing for those who are new to LINQ and DB Context terminology but after this demonstration they may be able to understand this term. This entire process is also well known as deferred execution and immediate execution in LINQ.


Learn

You can also have a look at the MVC related articles here:
  1. Custom Button With TagBuilder Using MVC Razor Engine
  2. Smart Working With Custom Value Providers in ASP.NET MVC
  3. Invoke Action With Model Binders in MVC
  4. Extension Helpers Method in MVC
  5. Precompiled Razor View Using RazorGenerator MVC and PreCompiledViewEngine in MVC 4
  6. RETURN MULTIPLE MODELS IN SINGLE VIEW IN MVC3
 
Let's proceed step-by-step to understand the real facts.
real facts
Step 1: I've chosen already a built application of MVC that deals with entities (DB context). When I first run an application it reaches to the code segment as shown in the following depicted image:
run an application
Step 2: Execute it further and see the result variable, it prompts you with “expanding the results view will enumerate the ienumerable”.
ienumerable
Or you need to iterate over it to get the current value from collection.
Step 3: You can make it enumerable after converting to a list type with the help of the .ToList() method. This is also known as immediate execution in LINQ.
Kindly have a look at the more descriptive image shown below:
descriptive
I hope you enjoyed this demonstration. A sample application is attached as reference.

reference
I hope you enjoyed and that it may help you down the line.Download the related code from the link given below:
Expanding the Results View Will Enumerate the IEnumerable Using DB Context
Cheers .Net Smile


Monday, June 16, 2014

Call Controller Action Method From JQuery Using Ajax

Call Controller Action Method From JQuery Using Ajax

 

image

 

Today I just came across an issue and implemented code that I thought to share with my techie friends who may also face such an issue, which is to call a Controller's action method from jQuery using Ajax.
Calling from the Client Side.

 

The following is a sample of calling an Ajax method from the client side:
image1.jpg

Down here I've demonstrated the code implementation and some builtin functions to prevent its natural behavior using preventDefault.
After clicking on the selected class element it first calls the ev.preventDefault(); that prevents the stopping of its natural event functionality. The URL accepts the ControllerName/Action name. The data : takes a parameter that is passed to a specified action method that executes and later returns the Model to the View. The type "post" means it's a POST method that takes some input.
If everything goes ok than it redirects to the desired page with the prevention of the natural behavior.
Cheers .Net


Thanks & Regards,

http://www.dotnetpiper.com/

Friday, June 13, 2014

Execution Order of Filters in MVC 4 With Practices: Important FAQ

This article describes the execution order of filters in MVC with practices. Custom filters and attributes are an excellent way to inject extra processing logic into the MVC request response pipeline.

Type of filters

 

This is another most frequently asked question in an interview.


interview


In a MVC application the user first lands on a routing module and matches the URL that best fits a route collection, this module parses the request and invokes the respected controller and action and then the controller will render the view in the browser.
Now somewhere we would like to inject some pre-processing or post-processing logic for actions and controllers. In that case we use filters.
You can also have a look at MVC related articles here:

 

Types of filters

There are the following types of filters that can be implemented to inject custom processing logic.

  • Authorization filter
  • Action filter
  • Result filter
  • Exception filter

Type of filters

Let's proceed step-by-step to understand the filters execution order.
filters execution
Step1
Authorization filter: This filter provides authentication and authorization logic. It will be executed before the action is executed. To implement this action an interface IAuthorizationFilter should be implemented by the custom attribute class. Kindly see the image shown below:
image
Step 2: Action filter

This filter will be called before and after the action starts executing and after the action has executed.

  • OnActionExecuting occurs just before the action method is called
  • OnActionExecuted occurs after the action method is called, but before the result is executed (before the view is rendered)
Now to implement this filter we need to create a custom filter attribute class and implement an IActionFilterfilter interface. This interface provides us two methods OnActionExecuting and OnActionExecuted.kindly look at an image depicted below:
code
Step 3: Result filter

This filter executes before and after the result of the action method has been executed. We can use this filter if we want some modification to be done in the action's result.
  • OnResultExecuting occurs just before the result is executed (before the view is rendered)
  • OnResultExecuted occurs after the result is executed (after the view is rendered)

To implement the result filters we need to create a custom filter attribute class and implement the IResultFilter interface. This interface provides two methods OnResultExecuting and OnResultExecuted.

OnResultExecuted
Step 4: Exception Filter

This filter will be invoked whenever a controller or action of the controller throws an exception. This is particularly useful when we need a custom error logging module.
To implement this filter we need to create a custom filter attribute class that implements IExceptionFilter. Here we have also set the ExceptionHandled property to let the filter know that the exception has been handled at the filter level. Kindly have a look at the image depicted below:
Exception Filter
Now press F5 and see the output to know the execution order of filters. Please see the depicted image below:

F5
result
I hope you enjoyed this and that it may help you down the line.

Keep touch base with me on http://www.dotnetpiper.com/

 

Cheers .Net Smile

Wednesday, June 4, 2014

Smart Working with ASP.NET MVC Value Providers: Custom Value Providers

 

clip_image002

 

In this article I am going to describe Custom Value Providers in MVC and its uses. If I say about value providers, Value Providers are the components that feed data to model binders. Feeding the data means instills the data to Model binder for further use at action level.

The framework contains a few built-in value providers named as FormValueProvider, RouteDataValueProvider, QueryStringValueProvider andHttpFileCollectionValueProvider that fetches data from Request.Form, Request.QueryString, Request.Files and RouteData.Values. These Value Providers are called in the order they are registered and so the one that registered earlier get the first chance. We can easily restrict the model to bind with data from a particular Value Provider.

In this article we will see how to create Custom value providers which fetches value from cookie and pass model binders at action level.

Down the level, I have created a controller which has an action declare as index. When the Index action is called using GET a cookie is added to the browser called “Id” with the value “E001” assigned to it.

clip_image001

When the form is posted [HttpPost] an index action is again called and the cookie value is automatically assigned to the Id parameter on the POST index method. But how?

clip_image002

.

Each value provider implements an interface IValueProvider.which has two methods as shown below in image:

clip_image003

The ContainsPrefix method is called by the model binder to determine whether the value provider has the data for a given prefix. The GetValue method returns a value for a given data key or returns null if the provider doesn't have any suitable data. Here is an implementation of both methods as shown below in image:

clip_image005

In shown above image the ContainsPrefix method checks whether the passed parameter is stored in cookie (or the value user has registered in other request/response parameters) and returns true or false. In the GetValue method it returns value from the cookie collection for the passed key (in our case it’s Id).

Now it’s time to register value provider through factories to make them instill data to model binder. We have to create a factory to register our CustomValueProvider by deriving from the abstract class ValueProviderFactory. The factory contains a single method GetValueProvider where we should instantiate our custom value proivder and return it.

clip_image007

Now we have to register CustomValueProviderFactory to ValueProviderFactories.Factories collection in the Application_Start event of Global.asax.cs as shown in below image:

clip_image008

Now I press F5 and run the application.

clip_image010

Let’s run the application and discuss points step by steps to fetch value from CustomValueProvider.

Step1: As soon as it runs the application it registers cookie value as depict in image below:

clip_image011

Step2: Fill the required values and press ok button, Kindly follow below shown image:

clip_image013

Step3: It calls CustomValueProvideFactory class to instantiate CustomValueProvider as depict in image below:

clip_image015

Step4: At the time of model binding the DefaultModelBinder checks with the value providers could they return value for the parameter Id by calling the ContainsPrefix method. If none of the value providers registered could return than it checks through CustomValueProvider whether such a parameter is stored and if yes it return the value. Kindly refer screen shot given below:

clip_image017

Step5: A final step is at post action method when it retrieves cookie value from CustomValueProvides.

clip_image019

Important Note: If we change the parameter exists in action method than it doesn’t find value from registered custom value provider due to mismatch in and it returns null.You can also retrieve multiple values from value providers. Kindly find a attach sample application.

Kindly refer image below:

clip_image020

Value Providers Magic is Over.

clip_image022.

Hope you enjoyed and it may help you down the line.

Keep coding and Smile Smile