Post

Absract controller

Why is the abstraction in your ASP.NET MVC application useful?

  • Code Reusability: You can avoid duplicating code across multiple controllers. This promotes code reusability and helps keep your codebase clean and maintainable.

  • Centralized Logic: The Abstract class allows you to centralize common logic, such as tracking user visits, in one place. This makes it easier to manage and update the behavior of your application, as changes only need to be made in one location.

  • Consistent Behavior: By inheriting from the Abstract class, your controllers inherit the common functionality defined in the base class. This ensures consistent behavior across different controllers in your application.

  • Abstraction of Implementation Details: The Abstract class abstracts away implementation details from the individual controllers. This means that controllers don’t need to know how visit tracking or other common functionality is implemented; they simply inherit and use it.

  • Separation of Concerns: By separating common functionality into a base controller, you adhere to the principle of separation of concerns. Each controller is responsible for its specific functionality, while common functionality is encapsulated in the base controller.

I have two controllers and one abstract controller to be inherited from those controllers.

│   ├── Controllers
│   │   ├── AbstractBaseController.cs
│   │   ├── HomeController.cs
│   │   └── OtherController.cs

1
2
3
4
5
6
7
public class HomeController : controller
{
    public IActionResult Index()
        {
            return View();
        }
}
1
2
3
4
5
6
7
public class OtherController : controller
{
    public IActionResult Index()
        {
            return View();
        }
}

If those controller have same logic to do something, we can add the same logic in both controllers at the same time.

But I’d like to make single method to do that and inherit it from the controllers. This class is to using cookie and session.

1
2
3
4
5
6
7
8
9
10
11
12
public abstract class AbstractBaseController : Controller
{
    protected string GenerateUserTrackingMessage(string pageName)
        {
            int totalPageVisitCount = GetAndIncrementTotalPageVisitCount(pageName);
            int sessionVisitCount = GetAndIncrementVisitCountForSession(pageName);

            // Complete the tracking message by the current page name.
            return $"Total visit :  {totalPageVisitCount} times, 
            Session visit count :  {sessionVisitCount}";
        }
...

And Both controllers inherit the abstract class and call the method of the base class

1
2
3
4
5
6
7
8
public class HomeController : AbstractBaseController // Inherit 
{
    public IActionResult Index()
    {
        string trackingMessage = GenerateUserTrackingMessage("Home");
        ViewBag.TrackingMessage = trackingMessage;
        return View();
    }
1
2
3
4
5
6
7
8
public class OtherController : AbstractBaseController // Inherit 
{
    public IActionResult Index()
    {
        string trackingMessage = GenerateUserTrackingMessage("Home");
        ViewBag.TrackingMessage = trackingMessage;
        return View();
    }

Then, you can share the message into views by the ViewBag.

1
2
3
<div class="container">
    @ViewBag.TrackingMessage
</div>
This post is licensed under CC BY 4.0 by the author.