Learn C#.NET, ASP.NET MVC, Design Principles with the simple example.

Monday, May 6, 2019

Action Method in ASP.NET MVC-5 With Example

We will learn Action Method in ASP.NET MVC with example and example of Action Method in ASP.NET MVC with example in this article. The default configuration of the action method in asp.net MVC.
Action Method in ASP.NET MVC-5 With Example


Following are the previews article on ASP.NET MVC


Action Method in ASP.NET MVC-5:

ASP.NET MVC action methods are responsible to execute the request and generate a response on it. All the public method of MVC controller are action method. If we want the public method is non-action method then we can decorate the action method by “NonAction” attribute. It will restrict the action method to render on the browser.

To work with action method need to remember the following points. 
  • The action method is a public non-static method.
  • Action method cannot be a private or protected method.
  • Action method cannot contain ref and out parameters.
  • Action method cannot be an extension method.
  • Action method cannot be overloaded.

  

Example of an action method in ASP.NET MVC as follows.

Action Method in ASP.NET MVC-5 With Example


public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
}

You can see in the above action method example, the Index() method is a public method which is inside the HomeController class. And the return type of the action method is ActionResult it can return using View()method. So every public method inside the Controller is an action method in MVC. 

How to restrict the public action method in MVC.

To restrict the public action method in MVC. We can use “NonAction” attribute. The “NonAction” attribute exists in System.Web.Mvcnamespace. 

Example of NonAction attribute in ASP.NET MVC:

  [NonAction]
    public string GetFullName(string strFirstName, string strLastName)
    {
            return strFirstName + " " + strLastName;
    }

Default Action Method in MVC: 

Default controller and Action method are configured in RouteConfig.cs class. By default, Controller is the Home controller and default Action method is the “Index” action method.

Example of default action method as follows:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }


I hope you should understand the concept of an action method in asp.net MVC.  If you like my article please add your valuable comment and share this article that will help to encourage me to create more article.


Share:

Saturday, March 30, 2019

Controller in ASP.NET MVC

We will learn Controller in ASP.NET MVC and controller example in ASP.NET MVC in this article.
Controller in ASP.NET MVC

Introduction:

MVC stands for Model, View, and Controller. MVC separates the application into three component like Model, View, and Controller. We will learn Controller in detail. The controller is one of the important components of MVC.

Controller in ASP.NET MVC:

  • Controller can contain action and non-action method.
  • It is used to handle the user request coming from the browser.
  • It will check the request from the browser and identify the action method and return the respective view.
  • A controller is inherited from ControllerBaseclass which is inside the System.Web.Mvcnamespace.
  • While creating controller remember it always suffix as Controllerkeyword.
  • Default Controller is HomeControllerand Indexview.

 Following are the steps to create a controller in ASP.NET MVC.


Step-1: Open >> Visual Studio 2017 or Lower version. I am using VS 2017

Step-2: Go to File >> New >> Project as follow.

Controller in ASP.NET MVC



 Step-3: Select “ASP.NET Web Application” and provide the meaning full name like “MVCControllerDemo” and Click on “Ok” button.
Controller in ASP.NET MVC

 Step-4: Select “Empty” template and check the “MVC” checkbox from “New Web Application” window as follow.
Controller in ASP.NET MVC

Default project structure will create as follow.

Controller in ASP.NET MVC


Step-5: Go to solution explorer Right click on “Controller” Folder >> Click on  Add >> click on “Controller” as follow.
Controller in ASP.NET MVC


Select “Empty Controller” from the open window and click on “Add” button.

Controller in ASP.NET MVC


Step-6: Provide the meaning full name like “HomeController”.
  
Sample code of Controller as follow:

 
using System.Web.Mvc;
namespace MVCControllerDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
    }
}

Default Routing provides as follow in Routeconfig.cs file.

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
 Following is the diagram of ASP.NET MVC

Controller in ASP.NET MVC

As per the above figure, user request enters the URL on the browser the given request go to the server and call the routing which will execute the appropriate controller. And based on request controller execute the appropriate controller action method. It will pass the request to model if the controller action method has data from the database. After completing this controller return the response to the user.

Share:

Sunday, March 24, 2019

Introduction of ASP.NET MVC

We will learn the introduction of ASP.NET MVC and advantages using ASP.NET MVC in this article.

Introduction:
MVC is an architectural design pattern which is used to develop the web application which will be maintainable, reusable, testable. It is used for separation of concerns.

MVC stands for Model, View, and Controller. MVC separates the application into three component like Model, View, and Controller. We will learn in detail what is Model, View, and Controller.

What is the design pattern?

A design pattern is a solution to the problems that occur in software development.
Design pattern helps us in applications development to resolve problems easily.

Example of design pattern 
  • MVC
  • Proxy
  • Singleton
  • Iterator
  • DAO 

What is ASP.NET MVC:

ASP.NET MVC is a web framework from Microsoft which is used to develop the web application. ASP.NET MVC use MVC architecture pattern. 
ASP.NET MVC are introduced in 2009 by Microsoft.

Advantages of using ASP.NET MVC

  1. Separation of code:

  • This feature is specified separating presentation logic from the database related logic. Easy to develop and modify using this feature.
  • In MVC based application the development process is split into three modules like Model - View - Controller.


2. Loosely coupling:
  • Loosely coupling represents less dependency.
  • Views is mvc are less dependency on models.
  • We can easily edit, expand and enhance the application.


3. Parallel Development:
  • This feature specifies developing models and views parallel.
  • It means multiple programmers work parallel on views and models.
  • This feature makes your application development is faster and easier.


4. Easy to perform unit testing:
  • Unit testing is the concept of testing only a certain portion of the application which is modified.
  • In MVC based application, we can easily perform unit testing.


5. TDD(Test Driven Development):
  • In TDD testers will guide by the development process.
  • MVC application is more comfortable for TDD because It will easy to modify at any point of time and any module.
  • TDD is one of the latest development process using in real time projects.


Model: 
Model is a normal C# class.
Model is responsible to handle data and business logic.
A model represents the shape of the data.
Model is responsible for handling database related changes.

Sample code of model as follow:
namespace MVCDemo.Models

{
    public class Employee

    {
        public int EmpId { get; set; }

        public string Name { get; set; }

        public string Email { get; set; }

        public double Salary { get; set; }
    }
}

View:
A view is responsible for UI(user interface).
View displays the data coming from the model.
A view is an HTML template which will be binding and displaying HTML controls with data.
The “.cshtml” file use the Razor view engine. And .cshtml views are use C# programming.
The view contains the following extension depends on languages.
1. .aspx
2. .asp
3. .html
4. .cshtml
5. .vbhtml
Following are the types of view in ASP.NET MVC -5
1. Layout View
2. Partial view
3. Normal view

Controller:
A controller can contain action and non-action method. It is used to handle the user request coming from the browser. It will check the request from the browser and identify the action method and return the respective view.
A controller is inherited from “ControllerBase” class which is inside the “System.Web.Mvc” namespace.
While creating controller remember it always suffix as “Controller” keyword. Default Controller is “HomeController” and “Index” view.

Sample code of Controller as follow:
 
namespace MVCDemo.Controllers
{
    public class HomeController : Controller
    {
       // GET: Home
        public ActionResult Index()
        {
            return View();
        }
    }
}

Following is the diagram of ASP.NET MVC
what is ASP.NET MVC
                                             Fig. ASP.NET MVC Architecture
ASP.NET MVC
                                      Fig. ASP.NET MVC Architecture

As per the above figure, user request enters the URL on the browser the given request go to the server and call the routing which will execute the appropriate controller. And based on request controller execute the appropriate controller action method. It will pass the request to model if the controller action method has data from the database. After completing this request, the controller returns the response to the user.
Share:

Tuesday, September 18, 2018

What is SOLID ? Why should we use SOLID design Principles

In this article, we will learn what is solid? Why should we use solid design principle?.

What is SOLID? Why should we use SOLID principle?

What is SOLID:

SOLID is basically design principle. which is used to create good software Architecture to develop a good software.
S.O.L.I.D is an acronym for the first five object-oriented design(OOD) principles by Robert C. Martin, popularly known as Uncle Bob.

What is meant by good software?



What is meant by good software?

The software has a good structure that is flexible, maintainable, and reusable.
These principles, when combined together, make it easy for a programmer to develop software that is easy to maintain and extend.

Why should we use SOLID design Principles? 

 To avoid the symptoms of poor design, or design smells.

Symptoms of poor design, or design smells:



• Rigidity: (The design is difficult to change)
A design is rigid if a single change causes a cascade of subsequent changes in independent modules. The more modules that must be changed, the more rigid the design.

• Fragility: (The design is easy to break)
Fragility is the tendency of a program to break in many places when a single change is made.

• Immobility:
The design is difficult to reusable.

• Viscosity: (The design is difficult to do the right thing.)
It is easy to do the wrong thing but difficult to do the right thing.

• Overdesign
• Needless repetition:
When the same code appears over and over again, in slightly different forms, the developers are missing an abstraction. Finding all the repetition and eliminating it with an appropriate abstraction
may not be high on their priority list, but it would go a long way toward making the system easier to understand and maintain.
When there is redundant code in the system, the job of changing the system can become difficult.
Bugs found in such a repeating unit have to be fixed in every repetition. However, since each repetition is slightly different from every other, the fix is not always the same.

An advantage of the SOLID principle or How do we know how whether the design of a software system is good?

• The design is easy to change.
• The design is easy to Flexible and Extensible
• The design is easy to Testable
• The design is easy to Reusable
• The design is easy to avoid the needless repetition
• The design is easy to do the right thing

The principles are there to help us eliminate bad smells. They are not a perfume to be liberally scattered all over the system. Over-conformance to the principles leads to the design smell of needless complexity.

S.O.L.I.D Stand for:



S: Stand for Single Responsibility Principle (SRP)
O: Stand for Open Close Principle (OCP)
L: Stand for Stand for Liscko Substitution Principle (LSP)
I: Stand for Interface Segregation Principle (ISP)
D: Dependency Inversion Principle (DIP)
We will go with one by one principle in detail with the example of the solid principle in the next article of this series.
I hope you understood the basic knowledge of what SOLID Is and Why should we use and advantages of using it.
If you like my article then please add your valuable comment on below comment box that will encourage me a lot to writing more and more articles.
Share:

Wednesday, September 5, 2018

What is Diamond Problem in C#

In this blog, we will learn what is Diamond problem in c# and We will learn Diamond problem in c# with simple example.


Prerequisites:
  • Basic knowledge of Inheritance in c#.


Why c# does not supports multiple inheritances in c#?
One of the main reason behind this is the “diamond problem.

What is the Diamond Problem:

The "diamond problem" is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which class of the method does D inherit: that of B, or that of C? So this is an ambiguity problem in multiple inheritances in c#. So that c# does not support multiple inheritances.


diamond problem in c#

Example 1: 

This is a simple example to understand the concepts of the diamond problem in c#.

First of all, we will check when does the diamond problem will arise with programmatically.
We will be creating class A, class B, class C, class D having Print() method in base class. The class B and C inherits from class A. and class D inherit from class B and Class C as follow.

using System;

namespace CSharpConsoleApp.DiamondProblemExample
{
   public class A
    {
        public virtual void Print()
        {
            Console.WriteLine("Print method of class A.");
        }
    }

    public class B:A
    {
        public override void Print()
        {
            Console.WriteLine("Print method of class B");
        }
    }

    public class C:A
    {
        public override void Print()
        {
            Console.WriteLine("Print method of class C");
        }
    }

    // Error: D class can not have multiple base classes.
    public class DC,B
    {
        
    }

    class DiamondProblemExample
    {
        static void Main(string[] args)
        {
            D obj = new D();
            obj.Print();
        }
    }
}

How can we resolve the diamond problem in c#?

We can resolve the diamond problem by using Interface in c#.


Example:
using System;

namespace CSharpConsoleApp.DiamondProblemExample
{
    interface IA
    {
        void PrintIA();
    }

    interface IB
    {
        void PrintIB();
    }

    interface IC
    {
        void PrintIC();
    }

   public class A : IA
    {
        public void PrintIA()
        {
            Console.WriteLine("PrintIA method from class A.");
        }
    }

    public class B:IB
    {
        public void PrintIB()
        {
            Console.WriteLine("PrintIB method from class B.");
        }
    }

    public class C:IC
    {
        public void PrintIC()
        {
            Console.WriteLine("PrintIC method from class C.");
        }
    }
    
    public class D: IA, IB,IC
    {
        public void PrintIA()
        {
            Console.WriteLine("PrintIA method from class D.");
        }
        public void PrintIB()
        {
            Console.WriteLine("PrintIB method from class D.");
        }

        public void PrintIC()
        {
            Console.WriteLine("PrintIC method from class D.");
        }
    }

    class DiamondProblemExample
    {
        static void Main(string[] args)
        {
            D obj = new D();
            obj.PrintIA();
            obj.PrintIB();
            obj.PrintIC();
            Console.ReadLine();
        }
    }
}

Real Time Example: 

Real Time Problem: 


diamond problem in c#


Real Time Solution: 


diamond problem in c#


A real-time example of resolving a diamond problem by using Interface in c#. 

using System;

namespace CSharpConApp.DiamondProblem1
{
    interface IMother
    {
        void Loan();
    }
    interface IFather
    {
        void Loan();
    }
    public class Mother:IMother
    {
        public void Loan()
        {
            Console.WriteLine("Loan taken by mother.");
        }
    }
    public class Father:IFather
    {
        public void Loan()
        {
            Console.WriteLine("Loan taken by father");
        }
    }
    public class Child:IMother,IFather
    {
        void IMother.Loan()
        {            
            Console.WriteLine("Mother's loan paid by child");
        }

        void IFather.Loan()
        {
            Console.WriteLine("Father's loan paid by child");
        }
    }

   public class DiamondProblem1
    {
        static void Main(string[] args)
        {
            Child child = new Child();
            ((IMother)child).Loan();
            ((IFather)child).Loan();

            Console.ReadLine();
        }
    }
}

To understand the concepts of Implicit and Explicit interface visit the following link.

I hope you understood the concept of the diamond problem in C#.
Share:

Upcoming Articles/Videos

Design Pattern
SOLID Design Principles
Copyright © Programming With Shri | Powered by Shrimant Telgave Home | Disclaimer | Privacy Policy | Terms and Conditions Design by Shrimant Telgave