Ads

C# Programming 33 - Implementing the Factory Design Pattern in C# for Student Management in ASP.NET Core Web API

The Factory Design Pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In this article, we'll explore how to apply the Factory Design Pattern in C# for a Student Management system using ASP.NET Core Web API.


Prerequisites:

Make sure you have the following tools and technologies installed:

  • Visual Studio (or any preferred IDE for C#)
  • ASP.NET Core SDK

Step 1: 

Create an ASP.NET Core Web API Project

Start by creating a new ASP.NET Core Web API project in Visual Studio. Open Visual Studio, select "Create a new project," choose "ASP.NET Core Web API," and follow the project creation wizard.

Step 2: 

Define the Student Model

Create a Student model class to represent the data structure for students. Include properties such as StudentId, Name, Age, etc.


public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    // Other properties as needed
}

Step 3: 

Implement the Student Factory Interface

Define a StudentFactory interface with a CreateStudent method that returns an instance of the Student class.

public interface IStudentFactory
{
    Student CreateStudent();
}

Step 4: 

Create Concrete Student Factories

Implement concrete classes that inherit from the IStudentFactory interface. Each concrete class will override the CreateStudent method to instantiate a specific type of Student.

public class RegularStudentFactory : IStudentFactory
{
    public Student CreateStudent()
    {
        // Logic to create a regular student
        return new Student();
    }
}

public class InternationalStudentFactory : IStudentFactory
{
    public Student CreateStudent()
    {
        // Logic to create an international student
        return new Student();
    }
}

Step 5: 

Utilize the Factory in the Controller

In your ASP.NET Core Web API controller, use dependency injection to inject an instance of the IStudentFactory. Use the factory to create instances of students based on the desired type.

[ApiController]
[Route("api/students")]
public class StudentController : ControllerBase
{
    private readonly IStudentFactory _studentFactory;

    public StudentController(IStudentFactory studentFactory)
    {
        _studentFactory = studentFactory;
    }

    [HttpGet("regular")]
    public IActionResult GetRegularStudent()
    {
        var regularStudent = _studentFactory.CreateStudent();
        // Additional logic for regular students
        return Ok(regularStudent);
    }

    [HttpGet("international")]
    public IActionResult GetInternationalStudent()
    {
        var internationalStudent = _studentFactory.CreateStudent();
        // Additional logic for international students
        return Ok(internationalStudent);
    }
}

Finally:

The Factory Design Pattern in C# allows for flexible and maintainable code when dealing with object creation. In the context of a Student Management ASP.NET Core Web API, the pattern can be applied to create different types of students based on specific requirements. This approach promotes code scalability and separation of concerns, making it easier to adapt to changing business needs.


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !