19 Dec 2023
In the context of a simple ASP.NET Core WebAPI using C#, applying the Single Responsibility Principle (SRP) involves ensuring that each class or module has only one reason to change. Here's an example that illustrates SRP in an ASP.NET Core WebAPI:
Let's consider a scenario where you have a user management system with responsibilities for handling user-related operations, such as creating and retrieving users.
User Service (High-level policy module):
// Interface defining the high-level user service
public interface IUserService
{
void CreateUser(string username);
string GetUserById(int userId);
// Other user-related methods...
}
// High-level user service implementation
public class UserService : IUserService
{
private readonly IUserRepository _userRepository;
public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public void CreateUser(string username)
{
// Business logic for creating a user...
_userRepository.SaveUser(new User { Username = username });
}
public string GetUserById(int userId)
{
// Business logic for retrieving a user...
var user = _userRepository.GetUserById(userId);
return user?.Username;
}
// Other user-related methods...
}
User Repository (Low-level detail module):
// Interface defining the low-level user repository
public interface IUserRepository
{
void SaveUser(User user);
User GetUserById(int userId);
// Other data access methods...
}
// Low-level user repository implementation
public class UserRepository : IUserRepository
{
private readonly List<User> _users = new List<User>();
public void SaveUser(User user)
{
// Database or storage access logic...
_users.Add(user);
}
public User GetUserById(int userId)
{
// Database or storage access logic...
return _users.FirstOrDefault(u => u.Id == userId);
}
// Other data access methods...
}
Controller in ASP.NET Core WebAPI:
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
private readonly IUserService _userService;
public UserController(IUserService userService)
{
_userService = userService;
}
[HttpPost]
public IActionResult CreateUser([FromBody] string username)
{
_userService.CreateUser(username);
return Ok();
}
[HttpGet("{userId}")]
public IActionResult GetUserById(int userId)
{
var username = _userService.GetUserById(userId);
return Ok(username);
}
// Other controller actions...
}
In this example:
- The
UserControllerin the ASP.NET Core WebAPI is responsible for handling HTTP requests and delegating user-related operations to theUserService. - The
UserServicefocuses on high-level user-related business logic. - The
UserRepositorydeals with low-level details of user data storage.
Each class has a distinct responsibility, adhering to the Single Responsibility Principle. The IUserService and IUserRepository interfaces provide clear abstractions, allowing for flexibility and testability in the system. This separation of concerns makes the codebase more maintainable and adaptable to changes in user-related functionality or data storage mechanisms.