Skip to content
This repository was archived by the owner on Dec 6, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions RepositoryPattern_UnitOfWork/DataAccess/ApplicationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Domain.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace DataAccess
{
public class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
{
}
public DbSet<Developer> Developers { get; set; }
public DbSet<Project> Projects { get; set; }
}
}
18 changes: 18 additions & 0 deletions RepositoryPattern_UnitOfWork/DataAccess/DataAccess.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Domain.Entities;
using Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccess.Repositories
{
public class DeveloperRepository : GenericRepository<Developer>, IDeveloperRepository
{
public DeveloperRepository(ApplicationContext context) : base(context)
{
}
public IEnumerable<Developer> GetPopularDevelopers(int count)
{
return _context.Developers.OrderByDescending(d => d.Followers).Take(count).ToList();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Domain.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.Repositories
{
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
protected readonly ApplicationContext _context;
public GenericRepository(ApplicationContext context)
{
_context = context;
}

public void Add(T entity)
{
_context.Set<T>().Add(entity);
}

public void AddRange(IEnumerable<T> entities)
{
_context.Set<T>().AddRange(entities);
}

public IEnumerable<T> Find(Expression<Func<T, bool>> expression)
{
return _context.Set<T>().Where(expression);
}

public IEnumerable<T> GetAll()
{
return _context.Set<T>().ToList();
}

public T GetById(int id)
{
return _context.Set<T>().Find(id);
}

public void Remove(T entity)
{
_context.Set<T>().Remove(entity);
}

public void RemoveRange(IEnumerable<T> entities)
{
_context.Set<T>().RemoveRange(entities);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Domain.Entities;
using Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;

namespace DataAccess.Repositories
{
public class ProjectRepository : GenericRepository<Project>, IProjectRepository
{
public ProjectRepository(ApplicationContext context) : base(context)
{
}
}
}
29 changes: 29 additions & 0 deletions RepositoryPattern_UnitOfWork/DataAccess/UnitOfWorks/UnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using DataAccess.Repositories;
using Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;

namespace DataAccess.UnitOfWorks
{
public class UnitOfWork : IUnitOfWork
{
private readonly ApplicationContext _context;
public UnitOfWork(ApplicationContext context)
{
_context = context;
Developers = new DeveloperRepository(_context);
Projects = new ProjectRepository(_context);
}
public IDeveloperRepository Developers { get; private set; }
public IProjectRepository Projects { get; private set; }
public int Complete()
{
return _context.SaveChanges();
}
public void Dispose()
{
_context.Dispose();
}
}
}
9 changes: 9 additions & 0 deletions RepositoryPattern_UnitOfWork/Domain/Domain.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
13 changes: 13 additions & 0 deletions RepositoryPattern_UnitOfWork/Domain/Entities/Developer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Domain.Entities
{
public class Developer
{
public int Id { get; set; }
public string Name { get; set; }
public int Followers { get; set; }
}
}
12 changes: 12 additions & 0 deletions RepositoryPattern_UnitOfWork/Domain/Entities/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Domain.Entities
{
public class Project
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Domain.Entities;
using System;
using System.Collections.Generic;
using System.Text;

namespace Domain.Interfaces
{
public interface IDeveloperRepository : IGenericRepository<Developer>
{
IEnumerable<Developer> GetPopularDevelopers(int count);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Interfaces
{
public interface IGenericRepository<T> where T : class
{
T GetById(int id);
IEnumerable<T> GetAll();
IEnumerable<T> Find(Expression<Func<T, bool>> expression);
void Add(T entity);
void AddRange(IEnumerable<T> entities);
void Remove(T entity);
void RemoveRange(IEnumerable<T> entities);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Domain.Entities;
using System;
using System.Collections.Generic;
using System.Text;

namespace Domain.Interfaces
{
public interface IProjectRepository : IGenericRepository<Project>
{
}
}
13 changes: 13 additions & 0 deletions RepositoryPattern_UnitOfWork/Domain/Interfaces/IUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Domain.Interfaces
{
public interface IUnitOfWork : IDisposable
{
IDeveloperRepository Developers { get; }
IProjectRepository Projects { get; }
int Complete();
}
}
37 changes: 37 additions & 0 deletions RepositoryPattern_UnitOfWork/RepositoryPattern_UnitOfWork.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataAccess", "DataAccess\DataAccess.csproj", "{B239FB66-45BA-437B-879F-DF3ECF58B9D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Domain\Domain.csproj", "{85DFFD9D-FCF3-4EC8-AAC0-4184F44B2F7F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi", "WebApi\WebApi.csproj", "{0018265D-C3F4-4BCB-AA0A-F287F65D9396}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B239FB66-45BA-437B-879F-DF3ECF58B9D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B239FB66-45BA-437B-879F-DF3ECF58B9D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B239FB66-45BA-437B-879F-DF3ECF58B9D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B239FB66-45BA-437B-879F-DF3ECF58B9D6}.Release|Any CPU.Build.0 = Release|Any CPU
{85DFFD9D-FCF3-4EC8-AAC0-4184F44B2F7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85DFFD9D-FCF3-4EC8-AAC0-4184F44B2F7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85DFFD9D-FCF3-4EC8-AAC0-4184F44B2F7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85DFFD9D-FCF3-4EC8-AAC0-4184F44B2F7F}.Release|Any CPU.Build.0 = Release|Any CPU
{0018265D-C3F4-4BCB-AA0A-F287F65D9396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0018265D-C3F4-4BCB-AA0A-F287F65D9396}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0018265D-C3F4-4BCB-AA0A-F287F65D9396}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0018265D-C3F4-4BCB-AA0A-F287F65D9396}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {41445BC4-6BC4-40FB-BE22-D4BD7BCE9C64}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Domain.Entities;
using Domain.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DeveloperController : ControllerBase
{
private readonly IUnitOfWork _unitOfWork;
public DeveloperController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult GetPopularDevelopers([FromQuery]int count)
{
var popularDevelopers = _unitOfWork.Developers.GetPopularDevelopers(count);
return Ok(popularDevelopers);

}
[HttpPost]
public IActionResult AddDeveloperAndProject()
{
var developer = new Developer
{
Followers = 35,
Name = "Mukesh Murugan"
};
var project = new Project
{
Name = "codewithmukesh"
};
_unitOfWork.Developers.Add(developer);
_unitOfWork.Projects.Add(project);
_unitOfWork.Complete();
return Ok();

}
}
}
26 changes: 26 additions & 0 deletions RepositoryPattern_UnitOfWork/WebApi/Pages/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@page
@model ErrorModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
27 changes: 27 additions & 0 deletions RepositoryPattern_UnitOfWork/WebApi/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Diagnostics;

namespace WebApi.Pages
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
}
Loading