├── .gitattributes ├── .gitignore ├── AspNetCoreRepositoryPattern.sln ├── MyMDB ├── Controllers │ ├── MoviesController.cs │ ├── MyMDBController.cs │ ├── StarsController.cs │ └── ValuesController.cs ├── Data │ ├── EFCore │ │ ├── EfCoreMovieRepository.cs │ │ ├── EfCoreRepository.cs │ │ ├── EfCoreStarRepository.cs │ │ └── MyMDBContext.cs │ ├── IEntity.cs │ └── IRepository.cs ├── Migrations │ ├── 20190606090345_Initial.Designer.cs │ ├── 20190606090345_Initial.cs │ ├── 20190606103405_Star.Designer.cs │ ├── 20190606103405_Star.cs │ └── MyMDBContextModelSnapshot.cs ├── Models │ ├── Movie.cs │ └── Star.cs ├── MyMDB.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── Startup.cs ├── appsettings.Development.json └── appsettings.json └── 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 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /AspNetCoreRepositoryPattern.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28922.388 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyMDB", "MyMDB\MyMDB.csproj", "{5B88C28F-C740-4DED-BEE6-19AC73925B37}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {5B88C28F-C740-4DED-BEE6-19AC73925B37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {5B88C28F-C740-4DED-BEE6-19AC73925B37}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {5B88C28F-C740-4DED-BEE6-19AC73925B37}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {5B88C28F-C740-4DED-BEE6-19AC73925B37}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {527590EE-6272-4679-9F36-D20022F87FFC} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /MyMDB/Controllers/MoviesController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using Microsoft.AspNetCore.Mvc; 4 | using MyMDB.Data.EFCore; 5 | using MyMDB.Models; 6 | 7 | namespace MyMDB.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | [ApiController] 11 | public class MoviesController : MyMDBController 12 | { 13 | public MoviesController(EfCoreMovieRepository repository) : base(repository) 14 | { 15 | 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /MyMDB/Controllers/MyMDBController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using Microsoft.AspNetCore.Mvc; 4 | using MyMDB.Data; 5 | 6 | namespace MyMDB.Controllers 7 | { 8 | [Route("api/[controller]")] 9 | [ApiController] 10 | public abstract class MyMDBController : ControllerBase 11 | where TEntity : class, IEntity 12 | where TRepository : IRepository 13 | { 14 | private readonly TRepository repository; 15 | 16 | public MyMDBController(TRepository repository) 17 | { 18 | this.repository = repository; 19 | } 20 | 21 | 22 | // GET: api/[controller] 23 | [HttpGet] 24 | public async Task>> Get() 25 | { 26 | return await repository.GetAll(); 27 | } 28 | 29 | // GET: api/[controller]/5 30 | [HttpGet("{id}")] 31 | public async Task> Get(int id) 32 | { 33 | var movie = await repository.Get(id); 34 | if (movie == null) 35 | { 36 | return NotFound(); 37 | } 38 | return movie; 39 | } 40 | 41 | // PUT: api/[controller]/5 42 | [HttpPut("{id}")] 43 | public async Task Put(int id, TEntity movie) 44 | { 45 | if (id != movie.Id) 46 | { 47 | return BadRequest(); 48 | } 49 | await repository.Update(movie); 50 | return NoContent(); 51 | } 52 | 53 | // POST: api/[controller] 54 | [HttpPost] 55 | public async Task> Post(TEntity movie) 56 | { 57 | await repository.Add(movie); 58 | return CreatedAtAction("Get", new { id = movie.Id }, movie); 59 | } 60 | 61 | // DELETE: api/[controller]/5 62 | [HttpDelete("{id}")] 63 | public async Task> Delete(int id) 64 | { 65 | var movie = await repository.Delete(id); 66 | if (movie == null) 67 | { 68 | return NotFound(); 69 | } 70 | return movie; 71 | } 72 | 73 | } 74 | } -------------------------------------------------------------------------------- /MyMDB/Controllers/StarsController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using MyMDB.Data.EFCore; 3 | using MyMDB.Models; 4 | 5 | namespace MyMDB.Controllers 6 | { 7 | [Route("api/[controller]")] 8 | [ApiController] 9 | public class StarsController : MyMDBController 10 | { 11 | public StarsController(EfCoreStarRepository repository) : base(repository) 12 | { 13 | 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /MyMDB/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace MyMDB.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | [ApiController] 11 | public class ValuesController : ControllerBase 12 | { 13 | // GET api/values 14 | [HttpGet] 15 | public ActionResult> Get() 16 | { 17 | return new string[] { "value1", "value2" }; 18 | } 19 | 20 | // GET api/values/5 21 | [HttpGet("{id}")] 22 | public ActionResult Get(int id) 23 | { 24 | return "value"; 25 | } 26 | 27 | // POST api/values 28 | [HttpPost] 29 | public void Post([FromBody] string value) 30 | { 31 | } 32 | 33 | // PUT api/values/5 34 | [HttpPut("{id}")] 35 | public void Put(int id, [FromBody] string value) 36 | { 37 | } 38 | 39 | // DELETE api/values/5 40 | [HttpDelete("{id}")] 41 | public void Delete(int id) 42 | { 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /MyMDB/Data/EFCore/EfCoreMovieRepository.cs: -------------------------------------------------------------------------------- 1 | using MyMDB.Models; 2 | using MyMDB.Models.EFCore; 3 | 4 | namespace MyMDB.Data.EFCore 5 | { 6 | public class EfCoreMovieRepository : EfCoreRepository 7 | { 8 | public EfCoreMovieRepository(MyMDBContext context) : base(context) 9 | { 10 | 11 | } 12 | // We can add new methods specific to the movie repository here in the future 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /MyMDB/Data/EFCore/EfCoreRepository.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System.Collections.Generic; 3 | using System.Threading.Tasks; 4 | 5 | namespace MyMDB.Data.EFCore 6 | { 7 | public abstract class EfCoreRepository : IRepository 8 | where TEntity : class, IEntity 9 | where TContext : DbContext 10 | { 11 | private readonly TContext context; 12 | public EfCoreRepository(TContext context) 13 | { 14 | this.context = context; 15 | } 16 | public async Task Add(TEntity entity) 17 | { 18 | context.Set().Add(entity); 19 | await context.SaveChangesAsync(); 20 | return entity; 21 | } 22 | 23 | public async Task Delete(int id) 24 | { 25 | var entity = await context.Set().FindAsync(id); 26 | if (entity == null) 27 | { 28 | return entity; 29 | } 30 | 31 | context.Set().Remove(entity); 32 | await context.SaveChangesAsync(); 33 | 34 | return entity; 35 | } 36 | 37 | public async Task Get(int id) 38 | { 39 | return await context.Set().FindAsync(id); 40 | } 41 | 42 | public async Task> GetAll() 43 | { 44 | return await context.Set().ToListAsync(); 45 | } 46 | 47 | public async Task Update(TEntity entity) 48 | { 49 | context.Entry(entity).State = EntityState.Modified; 50 | await context.SaveChangesAsync(); 51 | return entity; 52 | } 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /MyMDB/Data/EFCore/EfCoreStarRepository.cs: -------------------------------------------------------------------------------- 1 | using MyMDB.Models; 2 | using MyMDB.Models.EFCore; 3 | 4 | namespace MyMDB.Data.EFCore 5 | { 6 | public class EfCoreStarRepository : EfCoreRepository 7 | { 8 | public EfCoreStarRepository(MyMDBContext context) : base(context) 9 | { 10 | 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MyMDB/Data/EFCore/MyMDBContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace MyMDB.Models.EFCore 4 | { 5 | public class MyMDBContext : DbContext 6 | { 7 | public MyMDBContext (DbContextOptions options) 8 | : base(options) 9 | { 10 | } 11 | 12 | public DbSet Movie { get; set; } 13 | public DbSet Star { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MyMDB/Data/IEntity.cs: -------------------------------------------------------------------------------- 1 | namespace MyMDB.Data 2 | { 3 | public interface IEntity 4 | { 5 | int Id { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /MyMDB/Data/IRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace MyMDB.Data 5 | { 6 | public interface IRepository where T : class, IEntity 7 | { 8 | Task> GetAll(); 9 | Task Get(int id); 10 | Task Add(T entity); 11 | Task Update(T entity); 12 | Task Delete(int id); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MyMDB/Migrations/20190606090345_Initial.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 MyMDB.Models.EFCore; 9 | 10 | namespace MyMDB.Migrations 11 | { 12 | [DbContext(typeof(MyMDBContext))] 13 | [Migration("20190606090345_Initial")] 14 | partial class Initial 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "2.2.3-servicing-35854") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("MyMDB.Models.Movie", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 29 | 30 | b.Property("Genre") 31 | .IsRequired() 32 | .HasMaxLength(30); 33 | 34 | b.Property("ReleaseDate"); 35 | 36 | b.Property("Title") 37 | .IsRequired() 38 | .HasMaxLength(60); 39 | 40 | b.HasKey("Id"); 41 | 42 | b.ToTable("Movie"); 43 | }); 44 | #pragma warning restore 612, 618 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /MyMDB/Migrations/20190606090345_Initial.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using Microsoft.EntityFrameworkCore.Migrations; 4 | 5 | namespace MyMDB.Migrations 6 | { 7 | public partial class Initial : Migration 8 | { 9 | protected override void Up(MigrationBuilder migrationBuilder) 10 | { 11 | migrationBuilder.CreateTable( 12 | name: "Movie", 13 | columns: table => new 14 | { 15 | Id = table.Column(nullable: false) 16 | .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), 17 | Title = table.Column(maxLength: 60, nullable: false), 18 | Genre = table.Column(maxLength: 30, nullable: false), 19 | ReleaseDate = table.Column(nullable: false) 20 | }, 21 | constraints: table => 22 | { 23 | table.PrimaryKey("PK_Movie", x => x.Id); 24 | }); 25 | } 26 | 27 | protected override void Down(MigrationBuilder migrationBuilder) 28 | { 29 | migrationBuilder.DropTable( 30 | name: "Movie"); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /MyMDB/Migrations/20190606103405_Star.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 MyMDB.Models.EFCore; 9 | 10 | namespace MyMDB.Migrations 11 | { 12 | [DbContext(typeof(MyMDBContext))] 13 | [Migration("20190606103405_Star")] 14 | partial class Star 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "2.2.3-servicing-35854") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("MyMDB.Models.Movie", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 29 | 30 | b.Property("Genre") 31 | .IsRequired() 32 | .HasMaxLength(30); 33 | 34 | b.Property("ReleaseDate"); 35 | 36 | b.Property("Title") 37 | .IsRequired() 38 | .HasMaxLength(60); 39 | 40 | b.HasKey("Id"); 41 | 42 | b.ToTable("Movie"); 43 | }); 44 | 45 | modelBuilder.Entity("MyMDB.Models.Star", b => 46 | { 47 | b.Property("Id") 48 | .ValueGeneratedOnAdd() 49 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 50 | 51 | b.Property("BirthDate"); 52 | 53 | b.Property("Name") 54 | .IsRequired() 55 | .HasMaxLength(30); 56 | 57 | b.Property("Nationality") 58 | .IsRequired() 59 | .HasMaxLength(30); 60 | 61 | b.Property("Surname") 62 | .IsRequired() 63 | .HasMaxLength(30); 64 | 65 | b.Property("WonOscar"); 66 | 67 | b.HasKey("Id"); 68 | 69 | b.ToTable("Star"); 70 | }); 71 | #pragma warning restore 612, 618 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /MyMDB/Migrations/20190606103405_Star.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using Microsoft.EntityFrameworkCore.Migrations; 4 | 5 | namespace MyMDB.Migrations 6 | { 7 | public partial class Star : Migration 8 | { 9 | protected override void Up(MigrationBuilder migrationBuilder) 10 | { 11 | migrationBuilder.CreateTable( 12 | name: "Star", 13 | columns: table => new 14 | { 15 | Id = table.Column(nullable: false) 16 | .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), 17 | Name = table.Column(maxLength: 30, nullable: false), 18 | Surname = table.Column(maxLength: 30, nullable: false), 19 | Nationality = table.Column(maxLength: 30, nullable: false), 20 | BirthDate = table.Column(nullable: false), 21 | WonOscar = table.Column(nullable: false) 22 | }, 23 | constraints: table => 24 | { 25 | table.PrimaryKey("PK_Star", x => x.Id); 26 | }); 27 | } 28 | 29 | protected override void Down(MigrationBuilder migrationBuilder) 30 | { 31 | migrationBuilder.DropTable( 32 | name: "Star"); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /MyMDB/Migrations/MyMDBContextModelSnapshot.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 MyMDB.Models.EFCore; 8 | 9 | namespace MyMDB.Migrations 10 | { 11 | [DbContext(typeof(MyMDBContext))] 12 | partial class MyMDBContextModelSnapshot : ModelSnapshot 13 | { 14 | protected override void BuildModel(ModelBuilder modelBuilder) 15 | { 16 | #pragma warning disable 612, 618 17 | modelBuilder 18 | .HasAnnotation("ProductVersion", "2.2.3-servicing-35854") 19 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 20 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 21 | 22 | modelBuilder.Entity("MyMDB.Models.Movie", b => 23 | { 24 | b.Property("Id") 25 | .ValueGeneratedOnAdd() 26 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 27 | 28 | b.Property("Genre") 29 | .IsRequired() 30 | .HasMaxLength(30); 31 | 32 | b.Property("ReleaseDate"); 33 | 34 | b.Property("Title") 35 | .IsRequired() 36 | .HasMaxLength(60); 37 | 38 | b.HasKey("Id"); 39 | 40 | b.ToTable("Movie"); 41 | }); 42 | 43 | modelBuilder.Entity("MyMDB.Models.Star", b => 44 | { 45 | b.Property("Id") 46 | .ValueGeneratedOnAdd() 47 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 48 | 49 | b.Property("BirthDate"); 50 | 51 | b.Property("Name") 52 | .IsRequired() 53 | .HasMaxLength(30); 54 | 55 | b.Property("Nationality") 56 | .IsRequired() 57 | .HasMaxLength(30); 58 | 59 | b.Property("Surname") 60 | .IsRequired() 61 | .HasMaxLength(30); 62 | 63 | b.Property("WonOscar"); 64 | 65 | b.HasKey("Id"); 66 | 67 | b.ToTable("Star"); 68 | }); 69 | #pragma warning restore 612, 618 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /MyMDB/Models/Movie.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using MyMDB.Data; 4 | 5 | namespace MyMDB.Models 6 | { 7 | public class Movie : IEntity 8 | { 9 | public int Id { get; set; } 10 | 11 | [Required] 12 | [StringLength(60, MinimumLength = 3)] 13 | public string Title { get; set; } 14 | 15 | [Required] 16 | [StringLength(30)] 17 | [RegularExpression(@"^[A-Z]+[a-zA-Z""'\s-]*$")] 18 | public string Genre { get; set; } 19 | 20 | [DataType(DataType.Date)] 21 | public DateTime ReleaseDate { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /MyMDB/Models/Star.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using MyMDB.Data; 4 | 5 | namespace MyMDB.Models 6 | { 7 | public class Star : IEntity 8 | { 9 | public int Id { get; set; } 10 | 11 | [Required] 12 | [StringLength(30, MinimumLength = 2)] 13 | public string Name { get; set; } 14 | 15 | [Required] 16 | [StringLength(30, MinimumLength = 2)] 17 | public string Surname { get; set; } 18 | 19 | [Required] 20 | [StringLength(30)] 21 | [RegularExpression(@"^[A-Z]+[a-zA-Z""'\s-]*$")] 22 | public string Nationality { get; set; } 23 | 24 | [DataType(DataType.Date)] 25 | public DateTime BirthDate { get; set; } 26 | 27 | public bool WonOscar { get; set; } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /MyMDB/MyMDB.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.2 5 | InProcess 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /MyMDB/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace MyMDB 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | CreateWebHostBuilder(args).Build().Run(); 18 | } 19 | 20 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyMDB/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:64344", 8 | "sslPort": 44393 9 | } 10 | }, 11 | "profiles": { 12 | "IIS Express": { 13 | "commandName": "IISExpress", 14 | "launchBrowser": true, 15 | "launchUrl": "api/values", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "MyMDB": { 21 | "commandName": "Project", 22 | "launchBrowser": true, 23 | "launchUrl": "api/values", 24 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /MyMDB/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.AspNetCore.HttpsPolicy; 8 | using Microsoft.AspNetCore.Mvc; 9 | using Microsoft.Extensions.Configuration; 10 | using Microsoft.Extensions.DependencyInjection; 11 | using Microsoft.Extensions.Logging; 12 | using Microsoft.Extensions.Options; 13 | using Microsoft.EntityFrameworkCore; 14 | using MyMDB.Models; 15 | using MyMDB.Models.EFCore; 16 | using MyMDB.Data.EFCore; 17 | 18 | namespace MyMDB 19 | { 20 | public class Startup 21 | { 22 | public Startup(IConfiguration configuration) 23 | { 24 | Configuration = configuration; 25 | } 26 | 27 | public IConfiguration Configuration { get; } 28 | 29 | // This method gets called by the runtime. Use this method to add services to the container. 30 | public void ConfigureServices(IServiceCollection services) 31 | { 32 | services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); 33 | 34 | services.AddDbContext(options => 35 | options.UseSqlServer(Configuration.GetConnectionString("MyMDBContext"))); 36 | 37 | services.AddScoped(); 38 | 39 | services.AddScoped(); 40 | } 41 | 42 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 43 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 44 | { 45 | if (env.IsDevelopment()) 46 | { 47 | app.UseDeveloperExceptionPage(); 48 | } 49 | else 50 | { 51 | // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. 52 | app.UseHsts(); 53 | } 54 | 55 | app.UseHttpsRedirection(); 56 | app.UseMvc(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /MyMDB/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /MyMDB/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Warning" 5 | } 6 | }, 7 | "AllowedHosts": "*", 8 | "ConnectionStrings": { 9 | "MyMDBContext": "Server=(localdb)\\mssqllocaldb;Database=MyMDBContext-571a8a53-3df5-4441-a56b-6224c45b9376;Trusted_Connection=True;MultipleActiveResultSets=true" 10 | } 11 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AspNetCoreRepositoryPattern 2 | 3 | This API — MyMDB (My Movie Database) — provides endpoints that perform CRUD operations on a database of movies and stars. 4 | 5 | It is written in ASP.NET Core using EF Core and SQL Server. 6 | 7 | The development process of the API is explained in [this article](https://medium.com/net-core/repository-pattern-implementation-in-asp-net-core-21e01c6664d7). 8 | The aim of the article is to show the generic repository pattern with asynchronous methods. 9 | --------------------------------------------------------------------------------