├── .gitattributes ├── .gitignore ├── ProCodeGuide.RepositoryPattern.Api.sln ├── ProCodeGuide.RepositoryPattern.Api ├── Adapters │ ├── IStudentAdapter.cs │ └── StudentAdapter.cs ├── Controllers │ └── StudentController.cs ├── Models │ └── Student.cs ├── ProCodeGuide.RepositoryPattern.Api.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── Services │ ├── IStudentService.cs │ ├── IUnitOfWorkService.cs │ ├── StudentService.cs │ └── UnitOfWorkService.cs ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── ProCodeGuide.RepositoryPattern.DAL ├── DbContexts │ └── ApplicationDbContext.cs ├── Migrations │ ├── 20210831035653_FirstMigration.Designer.cs │ ├── 20210831035653_FirstMigration.cs │ └── ApplicationDbContextModelSnapshot.cs ├── ProCodeGuide.RepositoryPattern.DAL.csproj └── Repositories │ ├── GenericRepository.cs │ ├── StudentAddressRepository.cs │ ├── StudentRepository.cs │ └── StudentSportRepository.cs ├── ProCodeGuide.RepositoryPattern.Shared ├── DbEntities │ ├── StudentAddressEntity.cs │ ├── StudentEntity.cs │ └── StudentSportEntity.cs ├── Interfaces │ ├── IGenericRepository.cs │ ├── IStudentAddressRepository.cs │ ├── IStudentRepository.cs │ └── IStudentSportRepository.cs └── ProCodeGuide.RepositoryPattern.Shared.csproj └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31613.86 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProCodeGuide.RepositoryPattern.Api", "ProCodeGuide.RepositoryPattern.Api\ProCodeGuide.RepositoryPattern.Api.csproj", "{50AEA4CD-372D-4DC9-A763-7019A141DCE6}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProCodeGuide.RepositoryPattern.DAL", "ProCodeGuide.RepositoryPattern.DAL\ProCodeGuide.RepositoryPattern.DAL.csproj", "{8375CB66-E33F-42D3-BD9C-B2A54B4A5AD6}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProCodeGuide.RepositoryPattern.Shared", "ProCodeGuide.RepositoryPattern.Shared\ProCodeGuide.RepositoryPattern.Shared.csproj", "{6EBAAC45-DDBD-44E4-B9CF-4DE75D64AA77}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {50AEA4CD-372D-4DC9-A763-7019A141DCE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {50AEA4CD-372D-4DC9-A763-7019A141DCE6}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {50AEA4CD-372D-4DC9-A763-7019A141DCE6}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {50AEA4CD-372D-4DC9-A763-7019A141DCE6}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {8375CB66-E33F-42D3-BD9C-B2A54B4A5AD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {8375CB66-E33F-42D3-BD9C-B2A54B4A5AD6}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {8375CB66-E33F-42D3-BD9C-B2A54B4A5AD6}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {8375CB66-E33F-42D3-BD9C-B2A54B4A5AD6}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {6EBAAC45-DDBD-44E4-B9CF-4DE75D64AA77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {6EBAAC45-DDBD-44E4-B9CF-4DE75D64AA77}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {6EBAAC45-DDBD-44E4-B9CF-4DE75D64AA77}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {6EBAAC45-DDBD-44E4-B9CF-4DE75D64AA77}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {DC9460F2-90E9-42A4-BC31-F307E0B5631B} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Adapters/IStudentAdapter.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.Api.Models; 2 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | 8 | namespace ProCodeGuide.RepositoryPattern.Api.Adapters 9 | { 10 | public interface IStudentAdapter 11 | { 12 | StudentEntity Adapt(Student student); 13 | StudentAddressEntity AdaptToStudentAddress(Student student); 14 | StudentSportEntity AdaptToStudentSport(Student student); 15 | Student Adapt(StudentEntity studentEntity, StudentAddressEntity studentAddressEntity, StudentSportEntity studentSportEntity); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Adapters/StudentAdapter.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.Api.Models; 2 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | 8 | namespace ProCodeGuide.RepositoryPattern.Api.Adapters 9 | { 10 | public class StudentAdapter : IStudentAdapter 11 | { 12 | public StudentEntity Adapt(Student student) 13 | { 14 | return new StudentEntity() 15 | { 16 | StudentCode = student.StudentCode, 17 | Name = student.Name, 18 | Grade = student.Grade, 19 | Age = student.Age, 20 | CreatedOn = DateTime.Now, 21 | IsDeleted = false, 22 | LastModifiedOn = DateTime.Now, 23 | LastModifiedBy = "User 1" 24 | }; 25 | } 26 | 27 | public StudentSportEntity AdaptToStudentSport(Student student) 28 | { 29 | return new StudentSportEntity() 30 | { 31 | StudentCode = student.StudentCode, 32 | Sport = student.PreferredSport, 33 | IsDeleted = false 34 | }; 35 | } 36 | 37 | public StudentAddressEntity AdaptToStudentAddress(Student student) 38 | { 39 | return new StudentAddressEntity() 40 | { 41 | Address = student.Address, 42 | State = student.State, 43 | City = student.City, 44 | Country = student.Country, 45 | PinCode = student.PinCode, 46 | StudentCode = student.StudentCode, 47 | IsPrimary = true, 48 | IsDeleted = false 49 | }; 50 | } 51 | 52 | public Student Adapt(StudentEntity studentEntity, StudentAddressEntity studentAddressEntity, StudentSportEntity studentSportEntity) 53 | { 54 | return new Student() 55 | { 56 | StudentCode = studentEntity.StudentCode, 57 | Name = studentEntity.Name, 58 | Age = studentEntity.Age, 59 | Grade = studentEntity.Grade, 60 | 61 | Address = studentAddressEntity.Address, 62 | City = studentAddressEntity.City, 63 | Country = studentAddressEntity.Country, 64 | PinCode = studentAddressEntity.PinCode, 65 | State = studentAddressEntity.State, 66 | 67 | PreferredSport = studentSportEntity.Sport 68 | }; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Controllers/StudentController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Http; 2 | using Microsoft.AspNetCore.Mvc; 3 | using ProCodeGuide.RepositoryPattern.Api.Models; 4 | using ProCodeGuide.RepositoryPattern.Api.Services; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace ProCodeGuide.RepositoryPattern.Api.Controllers 11 | { 12 | [Route("api/[controller]")] 13 | [ApiController] 14 | public class StudentController : ControllerBase 15 | { 16 | private readonly IStudentService _studentService; 17 | public StudentController(IStudentService studentService) 18 | { 19 | _studentService = studentService; 20 | } 21 | 22 | [HttpGet] 23 | public IEnumerable Get() 24 | { 25 | return _studentService.GetAll(); 26 | } 27 | 28 | [HttpGet("{studentCode}")] 29 | public Student GetByStudentCode(string studentCode) 30 | { 31 | return _studentService.GetByStudentCode(studentCode); 32 | } 33 | 34 | [HttpPost] 35 | [Route("Add")] 36 | public void Add([FromBody] Student student) 37 | { 38 | _studentService.Add(student); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Models/Student.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace ProCodeGuide.RepositoryPattern.Api.Models 7 | { 8 | public class Student 9 | { 10 | public string StudentCode { get; set; } 11 | public string Name { get; set; } 12 | public int Age { get; set; } 13 | public string Address { get; set; } 14 | public string City { get; set; } 15 | public string State { get; set; } 16 | public string PinCode { get; set; } 17 | public string Country { get; set; } 18 | public int Grade { get; set; } 19 | public string PreferredSport { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/ProCodeGuide.RepositoryPattern.Api.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | all 10 | runtime; build; native; contentfiles; analyzers; buildtransitive 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Hosting; 2 | using Microsoft.Extensions.Configuration; 3 | using Microsoft.Extensions.Hosting; 4 | using Microsoft.Extensions.Logging; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace ProCodeGuide.RepositoryPattern.Api 11 | { 12 | public class Program 13 | { 14 | public static void Main(string[] args) 15 | { 16 | CreateHostBuilder(args).Build().Run(); 17 | } 18 | 19 | public static IHostBuilder CreateHostBuilder(string[] args) => 20 | Host.CreateDefaultBuilder(args) 21 | .ConfigureWebHostDefaults(webBuilder => 22 | { 23 | webBuilder.UseStartup(); 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:36942", 8 | "sslPort": 0 9 | } 10 | }, 11 | "profiles": { 12 | "IIS Express": { 13 | "commandName": "IISExpress", 14 | "launchBrowser": true, 15 | "launchUrl": "swagger", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "ProCodeGuide.RepositoryPattern.Api": { 21 | "commandName": "Project", 22 | "dotnetRunMessages": "true", 23 | "launchBrowser": true, 24 | "launchUrl": "swagger", 25 | "applicationUrl": "http://localhost:5000", 26 | "environmentVariables": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Services/IStudentService.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.Api.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace ProCodeGuide.RepositoryPattern.Api.Services 8 | { 9 | public interface IStudentService 10 | { 11 | void Add(Student student); 12 | Student GetByStudentCode(string studentCode); 13 | IEnumerable GetAll(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Services/IUnitOfWorkService.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.Shared.Interfaces; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace ProCodeGuide.RepositoryPattern.Api.Services 8 | { 9 | public interface IUnitOfWorkService 10 | { 11 | int Save(); 12 | IStudentRepository Student { get; set; } 13 | IStudentAddressRepository StudentAddress { get; set; } 14 | IStudentSportRepository StudentSport { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Services/StudentService.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.Api.Adapters; 2 | using ProCodeGuide.RepositoryPattern.Api.Models; 3 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 4 | using ProCodeGuide.RepositoryPattern.Shared.Interfaces; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace ProCodeGuide.RepositoryPattern.Api.Services 11 | { 12 | public class StudentService : IStudentService 13 | { 14 | private readonly IStudentAdapter _studentAdapter; 15 | private readonly IUnitOfWorkService _unitOfWorkService; 16 | public StudentService(IUnitOfWorkService unitOfWorkService, IStudentAdapter studentAdapter) 17 | { 18 | _unitOfWorkService = unitOfWorkService; 19 | _studentAdapter = studentAdapter; 20 | } 21 | 22 | public void Add(Student student) 23 | { 24 | if (Validate(student)) 25 | { 26 | _unitOfWorkService.Student.Add(_studentAdapter.Adapt(student)); 27 | _unitOfWorkService.StudentSport.Add(_studentAdapter.AdaptToStudentSport(student)); 28 | _unitOfWorkService.StudentAddress.Add(_studentAdapter.AdaptToStudentAddress(student)); 29 | 30 | _unitOfWorkService.Save(); 31 | } 32 | } 33 | 34 | public IEnumerable GetAll() 35 | { 36 | List listStudent = new List(); 37 | foreach (StudentEntity studentEntity in _unitOfWorkService.Student.GetAll()) 38 | { 39 | listStudent.Add(_studentAdapter.Adapt(studentEntity, 40 | _unitOfWorkService.StudentAddress.GetByStudentCode(studentEntity.StudentCode), 41 | _unitOfWorkService.StudentSport.GetByStudentCode(studentEntity.StudentCode))); 42 | } 43 | return listStudent; 44 | } 45 | 46 | public Student GetByStudentCode(string studentCode) 47 | { 48 | StudentEntity studentEntity = _unitOfWorkService.Student.GetByStudentCode(studentCode); 49 | return _studentAdapter.Adapt(studentEntity, 50 | _unitOfWorkService.StudentAddress.GetByStudentCode(studentEntity.StudentCode), 51 | _unitOfWorkService.StudentSport.GetByStudentCode(studentEntity.StudentCode)); 52 | } 53 | 54 | public void Remove(Student student) 55 | { 56 | _unitOfWorkService.Student.Remove(_studentAdapter.Adapt(student)); 57 | _unitOfWorkService.Save(); 58 | } 59 | 60 | public bool Validate(Student student) 61 | { 62 | //Validate Students Data 63 | return true; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Services/UnitOfWorkService.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.DAL.DbContexts; 2 | using ProCodeGuide.RepositoryPattern.Shared.Interfaces; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | 8 | namespace ProCodeGuide.RepositoryPattern.Api.Services 9 | { 10 | public class UnitOfWorkService : IUnitOfWorkService 11 | { 12 | private readonly ApplicationDbContext _dbContext; 13 | public UnitOfWorkService(ApplicationDbContext dbContext, IStudentRepository studentRepository, IStudentSportRepository studentSportRepository, IStudentAddressRepository studentAddressRepository) 14 | { 15 | _dbContext = dbContext; 16 | Student = studentRepository; 17 | StudentSport = studentSportRepository; 18 | StudentAddress = studentAddressRepository; 19 | } 20 | 21 | public IStudentRepository Student { get; set; } 22 | public IStudentSportRepository StudentSport { get; set; } 23 | public IStudentAddressRepository StudentAddress { get; set; } 24 | 25 | public int Save() 26 | { 27 | return _dbContext.SaveChanges(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.AspNetCore.Mvc; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using Microsoft.Extensions.Hosting; 8 | using Microsoft.Extensions.Logging; 9 | using Microsoft.OpenApi.Models; 10 | using ProCodeGuide.RepositoryPattern.Api.Adapters; 11 | using ProCodeGuide.RepositoryPattern.Api.Services; 12 | using ProCodeGuide.RepositoryPattern.DAL.DbContexts; 13 | using ProCodeGuide.RepositoryPattern.DAL.Repositories; 14 | using ProCodeGuide.RepositoryPattern.Shared.Interfaces; 15 | using System; 16 | using System.Collections.Generic; 17 | using System.Linq; 18 | using System.Threading.Tasks; 19 | 20 | namespace ProCodeGuide.RepositoryPattern.Api 21 | { 22 | public class Startup 23 | { 24 | public Startup(IConfiguration configuration) 25 | { 26 | Configuration = configuration; 27 | } 28 | 29 | public IConfiguration Configuration { get; } 30 | 31 | // This method gets called by the runtime. Use this method to add services to the container. 32 | public void ConfigureServices(IServiceCollection services) 33 | { 34 | services.AddDbContext(options => 35 | options.UseSqlServer( 36 | Configuration.GetConnectionString("DefaultConnection"), 37 | ef => ef.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)), 38 | ServiceLifetime.Scoped); 39 | 40 | services.AddControllers(); 41 | services.AddSwaggerGen(c => 42 | { 43 | c.SwaggerDoc("v1", new OpenApiInfo { Title = "ProCodeGuide.RepositoryPattern.Api", Version = "v1" }); 44 | }); 45 | 46 | services.AddTransient(); 47 | services.AddTransient(); 48 | services.AddTransient(); 49 | 50 | services.AddTransient(); 51 | 52 | services.AddTransient(); 53 | services.AddTransient(); 54 | } 55 | 56 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 57 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 58 | { 59 | if (env.IsDevelopment()) 60 | { 61 | app.UseDeveloperExceptionPage(); 62 | app.UseSwagger(); 63 | app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ProCodeGuide.RepositoryPattern.Api v1")); 64 | } 65 | 66 | app.UseRouting(); 67 | 68 | app.UseAuthorization(); 69 | 70 | app.UseEndpoints(endpoints => 71 | { 72 | endpoints.MapControllers(); 73 | }); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Api/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ConnectionStrings": { 3 | "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ProCodeGuide.Repository.API;Trusted_Connection=True;MultipleActiveResultSets=true" 4 | }, 5 | "Logging": { 6 | "LogLevel": { 7 | "Default": "Information", 8 | "Microsoft": "Warning", 9 | "Microsoft.Hosting.Lifetime": "Information" 10 | } 11 | }, 12 | "AllowedHosts": "*" 13 | } 14 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.DAL/DbContexts/ApplicationDbContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace ProCodeGuide.RepositoryPattern.DAL.DbContexts 10 | { 11 | public class ApplicationDbContext : DbContext 12 | { 13 | public ApplicationDbContext(DbContextOptions options) 14 | : base(options) 15 | { 16 | } 17 | 18 | public DbSet Students { get; set; } 19 | public DbSet StudentSport { get; set; } 20 | public DbSet StudentAddress { get; set; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.DAL/Migrations/20210831035653_FirstMigration.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Migrations; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | using ProCodeGuide.RepositoryPattern.DAL.DbContexts; 9 | 10 | namespace ProCodeGuide.RepositoryPattern.DAL.Migrations 11 | { 12 | [DbContext(typeof(ApplicationDbContext))] 13 | [Migration("20210831035653_FirstMigration")] 14 | partial class FirstMigration 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 21 | .HasAnnotation("ProductVersion", "5.0.9") 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("ProCodeGuide.RepositoryPattern.Shared.DbEntities.StudentAddressEntity", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("int") 29 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 30 | 31 | b.Property("Address") 32 | .HasColumnType("nvarchar(max)"); 33 | 34 | b.Property("City") 35 | .HasColumnType("nvarchar(max)"); 36 | 37 | b.Property("Country") 38 | .HasColumnType("nvarchar(max)"); 39 | 40 | b.Property("IsDeleted") 41 | .HasColumnType("bit"); 42 | 43 | b.Property("IsPrimary") 44 | .HasColumnType("bit"); 45 | 46 | b.Property("PinCode") 47 | .HasColumnType("nvarchar(max)"); 48 | 49 | b.Property("State") 50 | .HasColumnType("nvarchar(max)"); 51 | 52 | b.Property("StudentCode") 53 | .HasColumnType("nvarchar(max)"); 54 | 55 | b.HasKey("Id"); 56 | 57 | b.ToTable("StudentAddress"); 58 | }); 59 | 60 | modelBuilder.Entity("ProCodeGuide.RepositoryPattern.Shared.DbEntities.StudentEntity", b => 61 | { 62 | b.Property("Id") 63 | .ValueGeneratedOnAdd() 64 | .HasColumnType("int") 65 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 66 | 67 | b.Property("Age") 68 | .HasColumnType("int"); 69 | 70 | b.Property("CreatedOn") 71 | .HasColumnType("datetime2"); 72 | 73 | b.Property("Grade") 74 | .HasColumnType("int"); 75 | 76 | b.Property("IsDeleted") 77 | .HasColumnType("bit"); 78 | 79 | b.Property("LastModifiedBy") 80 | .HasColumnType("nvarchar(max)"); 81 | 82 | b.Property("LastModifiedOn") 83 | .HasColumnType("datetime2"); 84 | 85 | b.Property("Name") 86 | .HasColumnType("nvarchar(max)"); 87 | 88 | b.Property("StudentCode") 89 | .HasColumnType("nvarchar(max)"); 90 | 91 | b.HasKey("Id"); 92 | 93 | b.ToTable("Students"); 94 | }); 95 | 96 | modelBuilder.Entity("ProCodeGuide.RepositoryPattern.Shared.DbEntities.StudentSportEntity", b => 97 | { 98 | b.Property("Id") 99 | .ValueGeneratedOnAdd() 100 | .HasColumnType("int") 101 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 102 | 103 | b.Property("IsDeleted") 104 | .HasColumnType("bit"); 105 | 106 | b.Property("Sport") 107 | .HasColumnType("nvarchar(max)"); 108 | 109 | b.Property("StudentCode") 110 | .HasColumnType("nvarchar(max)"); 111 | 112 | b.HasKey("Id"); 113 | 114 | b.ToTable("StudentSport"); 115 | }); 116 | #pragma warning restore 612, 618 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.DAL/Migrations/20210831035653_FirstMigration.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace ProCodeGuide.RepositoryPattern.DAL.Migrations 5 | { 6 | public partial class FirstMigration : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.CreateTable( 11 | name: "StudentAddress", 12 | columns: table => new 13 | { 14 | Id = table.Column(type: "int", nullable: false) 15 | .Annotation("SqlServer:Identity", "1, 1"), 16 | StudentCode = table.Column(type: "nvarchar(max)", nullable: true), 17 | Address = table.Column(type: "nvarchar(max)", nullable: true), 18 | City = table.Column(type: "nvarchar(max)", nullable: true), 19 | State = table.Column(type: "nvarchar(max)", nullable: true), 20 | PinCode = table.Column(type: "nvarchar(max)", nullable: true), 21 | Country = table.Column(type: "nvarchar(max)", nullable: true), 22 | IsPrimary = table.Column(type: "bit", nullable: false), 23 | IsDeleted = table.Column(type: "bit", nullable: false) 24 | }, 25 | constraints: table => 26 | { 27 | table.PrimaryKey("PK_StudentAddress", x => x.Id); 28 | }); 29 | 30 | migrationBuilder.CreateTable( 31 | name: "Students", 32 | columns: table => new 33 | { 34 | Id = table.Column(type: "int", nullable: false) 35 | .Annotation("SqlServer:Identity", "1, 1"), 36 | StudentCode = table.Column(type: "nvarchar(max)", nullable: true), 37 | Name = table.Column(type: "nvarchar(max)", nullable: true), 38 | Age = table.Column(type: "int", nullable: false), 39 | Grade = table.Column(type: "int", nullable: false), 40 | CreatedOn = table.Column(type: "datetime2", nullable: false), 41 | LastModifiedOn = table.Column(type: "datetime2", nullable: false), 42 | LastModifiedBy = table.Column(type: "nvarchar(max)", nullable: true), 43 | IsDeleted = table.Column(type: "bit", nullable: false) 44 | }, 45 | constraints: table => 46 | { 47 | table.PrimaryKey("PK_Students", x => x.Id); 48 | }); 49 | 50 | migrationBuilder.CreateTable( 51 | name: "StudentSport", 52 | columns: table => new 53 | { 54 | Id = table.Column(type: "int", nullable: false) 55 | .Annotation("SqlServer:Identity", "1, 1"), 56 | StudentCode = table.Column(type: "nvarchar(max)", nullable: true), 57 | Sport = table.Column(type: "nvarchar(max)", nullable: true), 58 | IsDeleted = table.Column(type: "bit", nullable: false) 59 | }, 60 | constraints: table => 61 | { 62 | table.PrimaryKey("PK_StudentSport", x => x.Id); 63 | }); 64 | } 65 | 66 | protected override void Down(MigrationBuilder migrationBuilder) 67 | { 68 | migrationBuilder.DropTable( 69 | name: "StudentAddress"); 70 | 71 | migrationBuilder.DropTable( 72 | name: "Students"); 73 | 74 | migrationBuilder.DropTable( 75 | name: "StudentSport"); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.DAL/Migrations/ApplicationDbContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | using ProCodeGuide.RepositoryPattern.DAL.DbContexts; 8 | 9 | namespace ProCodeGuide.RepositoryPattern.DAL.Migrations 10 | { 11 | [DbContext(typeof(ApplicationDbContext))] 12 | partial class ApplicationDbContextModelSnapshot : ModelSnapshot 13 | { 14 | protected override void BuildModel(ModelBuilder modelBuilder) 15 | { 16 | #pragma warning disable 612, 618 17 | modelBuilder 18 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 19 | .HasAnnotation("ProductVersion", "5.0.9") 20 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 21 | 22 | modelBuilder.Entity("ProCodeGuide.RepositoryPattern.Shared.DbEntities.StudentAddressEntity", b => 23 | { 24 | b.Property("Id") 25 | .ValueGeneratedOnAdd() 26 | .HasColumnType("int") 27 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 28 | 29 | b.Property("Address") 30 | .HasColumnType("nvarchar(max)"); 31 | 32 | b.Property("City") 33 | .HasColumnType("nvarchar(max)"); 34 | 35 | b.Property("Country") 36 | .HasColumnType("nvarchar(max)"); 37 | 38 | b.Property("IsDeleted") 39 | .HasColumnType("bit"); 40 | 41 | b.Property("IsPrimary") 42 | .HasColumnType("bit"); 43 | 44 | b.Property("PinCode") 45 | .HasColumnType("nvarchar(max)"); 46 | 47 | b.Property("State") 48 | .HasColumnType("nvarchar(max)"); 49 | 50 | b.Property("StudentCode") 51 | .HasColumnType("nvarchar(max)"); 52 | 53 | b.HasKey("Id"); 54 | 55 | b.ToTable("StudentAddress"); 56 | }); 57 | 58 | modelBuilder.Entity("ProCodeGuide.RepositoryPattern.Shared.DbEntities.StudentEntity", b => 59 | { 60 | b.Property("Id") 61 | .ValueGeneratedOnAdd() 62 | .HasColumnType("int") 63 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 64 | 65 | b.Property("Age") 66 | .HasColumnType("int"); 67 | 68 | b.Property("CreatedOn") 69 | .HasColumnType("datetime2"); 70 | 71 | b.Property("Grade") 72 | .HasColumnType("int"); 73 | 74 | b.Property("IsDeleted") 75 | .HasColumnType("bit"); 76 | 77 | b.Property("LastModifiedBy") 78 | .HasColumnType("nvarchar(max)"); 79 | 80 | b.Property("LastModifiedOn") 81 | .HasColumnType("datetime2"); 82 | 83 | b.Property("Name") 84 | .HasColumnType("nvarchar(max)"); 85 | 86 | b.Property("StudentCode") 87 | .HasColumnType("nvarchar(max)"); 88 | 89 | b.HasKey("Id"); 90 | 91 | b.ToTable("Students"); 92 | }); 93 | 94 | modelBuilder.Entity("ProCodeGuide.RepositoryPattern.Shared.DbEntities.StudentSportEntity", b => 95 | { 96 | b.Property("Id") 97 | .ValueGeneratedOnAdd() 98 | .HasColumnType("int") 99 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 100 | 101 | b.Property("IsDeleted") 102 | .HasColumnType("bit"); 103 | 104 | b.Property("Sport") 105 | .HasColumnType("nvarchar(max)"); 106 | 107 | b.Property("StudentCode") 108 | .HasColumnType("nvarchar(max)"); 109 | 110 | b.HasKey("Id"); 111 | 112 | b.ToTable("StudentSport"); 113 | }); 114 | #pragma warning restore 612, 618 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.DAL/ProCodeGuide.RepositoryPattern.DAL.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.DAL/Repositories/GenericRepository.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.DAL.DbContexts; 2 | using ProCodeGuide.RepositoryPattern.Shared.Interfaces; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace ProCodeGuide.RepositoryPattern.DAL.Repositories 10 | { 11 | public class GenericRepository : IGenericRepository where T : class 12 | { 13 | protected readonly ApplicationDbContext _dbcontext; 14 | public GenericRepository(ApplicationDbContext dbcontext) 15 | { 16 | _dbcontext = dbcontext; 17 | } 18 | 19 | public void Add(T entity) 20 | { 21 | _dbcontext.Set().Add(entity); 22 | } 23 | 24 | public T GetById(int id) 25 | { 26 | return _dbcontext.Set().Find(id); 27 | } 28 | 29 | public void Remove(T entity) 30 | { 31 | _dbcontext.Set().Remove(entity); 32 | } 33 | 34 | public IEnumerable GetAll() 35 | { 36 | return _dbcontext.Set().ToList(); 37 | } 38 | 39 | public int Complete() 40 | { 41 | return _dbcontext.SaveChanges(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.DAL/Repositories/StudentAddressRepository.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.DAL.DbContexts; 2 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 3 | using ProCodeGuide.RepositoryPattern.Shared.Interfaces; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace ProCodeGuide.RepositoryPattern.DAL.Repositories 11 | { 12 | public class StudentAddressRepository : GenericRepository, IStudentAddressRepository 13 | { 14 | public StudentAddressRepository(ApplicationDbContext dbContext) : base(dbContext) 15 | { 16 | 17 | } 18 | 19 | public StudentAddressEntity GetByStudentCode(string studentCode) 20 | { 21 | return _dbcontext.StudentAddress.Where(address => address.StudentCode.Equals(studentCode)).FirstOrDefault(); ; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.DAL/Repositories/StudentRepository.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.DAL.DbContexts; 2 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 3 | using ProCodeGuide.RepositoryPattern.Shared.Interfaces; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace ProCodeGuide.RepositoryPattern.DAL.Repositories 11 | { 12 | public class StudentRepository : GenericRepository, IStudentRepository 13 | { 14 | public StudentRepository(ApplicationDbContext dbContext) : base(dbContext) 15 | { 16 | 17 | } 18 | 19 | public StudentEntity GetByStudentCode(string studentCode) 20 | { 21 | return _dbcontext.Students.Where(student => student.StudentCode.Equals(studentCode)).FirstOrDefault(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.DAL/Repositories/StudentSportRepository.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.DAL.DbContexts; 2 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 3 | using ProCodeGuide.RepositoryPattern.Shared.Interfaces; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace ProCodeGuide.RepositoryPattern.DAL.Repositories 11 | { 12 | public class StudentSportRepository : GenericRepository, IStudentSportRepository 13 | { 14 | public StudentSportRepository(ApplicationDbContext dbContext) : base(dbContext) 15 | { 16 | 17 | } 18 | 19 | public StudentSportEntity GetByStudentCode(string studentCode) 20 | { 21 | return _dbcontext.StudentSport.Where(sport => sport.StudentCode.Equals(studentCode)).FirstOrDefault(); ; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Shared/DbEntities/StudentAddressEntity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace ProCodeGuide.RepositoryPattern.Shared.DbEntities 9 | { 10 | public class StudentAddressEntity 11 | { 12 | public int Id { get; set; } 13 | public string StudentCode { get; set; } 14 | public string Address { get; set; } 15 | public string City { get; set; } 16 | public string State { get; set; } 17 | public string PinCode { get; set; } 18 | public string Country { get; set; } 19 | public bool IsPrimary { get; set; } 20 | public bool IsDeleted { get; set; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Shared/DbEntities/StudentEntity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace ProCodeGuide.RepositoryPattern.Shared.DbEntities 9 | { 10 | public class StudentEntity 11 | { 12 | public int Id { get; set; } 13 | public string StudentCode { get; set; } 14 | public string Name { get; set; } 15 | public int Age { get; set; } 16 | public int Grade { get; set; } 17 | public DateTime CreatedOn { get; set; } 18 | public DateTime LastModifiedOn { get; set; } 19 | public string LastModifiedBy { get; set; } 20 | public bool IsDeleted { get; set; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Shared/DbEntities/StudentSportEntity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ProCodeGuide.RepositoryPattern.Shared.DbEntities 8 | { 9 | public class StudentSportEntity 10 | { 11 | public int Id { get; set; } 12 | public string StudentCode { get; set; } 13 | public string Sport { get; set; } 14 | public bool IsDeleted { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Shared/Interfaces/IGenericRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ProCodeGuide.RepositoryPattern.Shared.Interfaces 8 | { 9 | public interface IGenericRepository where T : class 10 | { 11 | void Add(T entity); 12 | T GetById(int id); 13 | void Remove(T entity); 14 | IEnumerable GetAll(); 15 | int Complete(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Shared/Interfaces/IStudentAddressRepository.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace ProCodeGuide.RepositoryPattern.Shared.Interfaces 9 | { 10 | public interface IStudentAddressRepository : IGenericRepository 11 | { 12 | StudentAddressEntity GetByStudentCode(string studentCode); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Shared/Interfaces/IStudentRepository.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace ProCodeGuide.RepositoryPattern.Shared.Interfaces 9 | { 10 | public interface IStudentRepository : IGenericRepository 11 | { 12 | StudentEntity GetByStudentCode(string studentCode); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Shared/Interfaces/IStudentSportRepository.cs: -------------------------------------------------------------------------------- 1 | using ProCodeGuide.RepositoryPattern.Shared.DbEntities; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace ProCodeGuide.RepositoryPattern.Shared.Interfaces 9 | { 10 | public interface IStudentSportRepository : IGenericRepository 11 | { 12 | StudentSportEntity GetByStudentCode(string studentCode); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ProCodeGuide.RepositoryPattern.Shared/ProCodeGuide.RepositoryPattern.Shared.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ProCodeGuide.Sample.RepositoryPattern 2 | Complete source code developed as part of article - Repository Pattern in ASP.NET Core with Adapter Pattern 3 | 4 | In this article, we will learn about Repository Pattern in ASP.NET Core along with Unit of Work Pattern & also learn about why & how to combine Repository Pattern with Adapter Pattern for better implementation & testability of data access layer. 5 | 6 | We will also see whether we really need a Repository Pattern in today’s modern application which makes use of ORM that is entity framework core for the data access layer. 7 | 8 | The complete article is available at https://procodeguide.com/programming/repository-pattern-in-aspnet-core/ 9 | --------------------------------------------------------------------------------