Uni Of Work - Project Structure
Project Structure
Controllers
- Your ASP.NET Core MVC controllers handle incoming HTTP requests, interact with services, and return responses.
Services
- Interfaces: Define interfaces for your services that will interact with data.
- Implementations: Implement these interfaces to encapsulate business logic and data operations.
- Example:
Services/ ├── ICustomerService.cs ├── CustomerService.cs
Repositories
- Interfaces: Define interfaces for repositories that will handle data operations.
- Implementations: Implement these interfaces to perform CRUD operations on specific entities.
- Example:
Repository/ ├── ICustomerRepository.cs ├── CustomerRepository.cs
DbContext
- Your Entity Framework DbContext manages database connections and entity relationships.
- Example:
Data/ ├── ApplicationDbContext.cs
Unit of Work
- Implement the Unit of Work pattern to coordinate data operations across multiple repositories within a single transaction.
- Example:
Infrastructure/ ├── IUnitOfWork.cs ├── UnitOfWork.cs
Models
- Define your data models or entities that represent database tables.
- Example:
Models/ ├── Customer.cs
Implementation
Here’s how these components would interact:
DbContext (
ApplicationDbContext.cs
):csharppublic class ApplicationDbContext : DbContext { public DbSet<Customer> Customers { get; set; } // Other DbSets and configuration }
Repositories (
ICustomerRepository.cs
,CustomerRepository.cs
):csharppublic interface ICustomerRepository { Task<Customer> GetByIdAsync(int id); void Add(Customer customer); void Update(Customer customer); void Delete(Customer customer); // Other methods as needed } public class CustomerRepository : ICustomerRepository { private readonly ApplicationDbContext _context; public CustomerRepository(ApplicationDbContext context) { _context = context; } public async Task<Customer> GetByIdAsync(int id) { return await _context.Customers.FindAsync(id); } public void Add(Customer customer) { _context.Customers.Add(customer); } public void Update(Customer customer) { _context.Customers.Update(customer); } public void Delete(Customer customer) { _context.Customers.Remove(customer); } // Implement other repository methods }
Unit of Work (
IUnitOfWork.cs
,UnitOfWork.cs
):csharppublic interface IUnitOfWork : IDisposable { ICustomerRepository Customers { get; } // Other repositories as needed Task<int> CompleteAsync(); } public class UnitOfWork : IUnitOfWork { private readonly ApplicationDbContext _context; public UnitOfWork(ApplicationDbContext context) { _context = context; Customers = new CustomerRepository(_context); // Initialize other repositories } public ICustomerRepository Customers { get; private set; } // Other repositories as properties public async Task<int> CompleteAsync() { return await _context.SaveChangesAsync(); } public void Dispose() { _context.Dispose(); } }
Services (
ICustomerService.cs
,CustomerService.cs
):csharppublic interface ICustomerService { Task<Customer> GetCustomerByIdAsync(int id); Task CreateCustomerAsync(Customer customer); Task UpdateCustomerAsync(Customer customer); Task DeleteCustomerAsync(int id); } public class CustomerService : ICustomerService { private readonly IUnitOfWork _unitOfWork; public CustomerService(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; } public async Task<Customer> GetCustomerByIdAsync(int id) { return await _unitOfWork.Customers.GetByIdAsync(id); } public async Task CreateCustomerAsync(Customer customer) { _unitOfWork.Customers.Add(customer); await _unitOfWork.CompleteAsync(); } public async Task UpdateCustomerAsync(Customer customer) { _unitOfWork.Customers.Update(customer); await _unitOfWork.CompleteAsync(); } public async Task DeleteCustomerAsync(int id) { var customer = await _unitOfWork.Customers.GetByIdAsync(id); if (customer != null) { _unitOfWork.Customers.Delete(customer); await _unitOfWork.CompleteAsync(); } } }
Dependency Injection (Startup.cs)
In your Startup.cs
file, configure dependency injection for your services and the unit of work:
csharppublic void ConfigureServices(IServiceCollection services)
{
// DbContext
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// Repositories
services.AddScoped<ICustomerRepository, CustomerRepository>();
// Other repositories
// Unit of Work
services.AddScoped<IUnitOfWork, UnitOfWork>();
// Services
services.AddScoped<ICustomerService, CustomerService>();
// Other services
// MVC
services.AddControllersWithViews();
}
Controller Usage
Finally, in your controller, inject the service(s) you need and use them to handle requests:
csharppublic class CustomerController : Controller
{
private readonly ICustomerService _customerService;
public CustomerController(ICustomerService customerService)
{
_customerService = customerService;
}
public async Task<IActionResult> Index()
{
var customers = await _customerService.GetAllCustomersAsync();
return View(customers);
}
// Other action methods
}
Summary
This structured approach ensures separation of concerns and facilitates maintainability and scalability of your ASP.NET Core MVC application using the Unit of Work pattern. Each component (controllers, services, repositories, DbContext, and Unit of Work) plays a specific role in handling requests, business logic, data access, and transaction management, respectively.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home