├── .gitignore
├── AulaRepositoryPattern.Core
├── AulaRepositoryPattern.Core.csproj
└── Models
│ ├── Entity.cs
│ └── IRepositoryBase.cs
├── AulaRepositoryPattern.Data
├── AulaRepositoryPattern.Data.csproj
├── Context
│ └── AppDbContext.cs
├── Extensions
│ └── DataExtensions.cs
├── Migrations
│ ├── 20210918183110_initial.Designer.cs
│ ├── 20210918183110_initial.cs
│ ├── 20210918190653_fixUsuario.Designer.cs
│ ├── 20210918190653_fixUsuario.cs
│ └── AppDbContextModelSnapshot.cs
└── Repositories
│ ├── Abstractions
│ ├── ICarroRepository.cs
│ └── IUsuarioRepository.cs
│ ├── CarroRepository.cs
│ ├── RepositoryBase.cs
│ └── UsuarioRepository.cs
├── AulaRepositoryPattern.Domain
├── AulaRepositoryPattern.Domain.csproj
├── Carro.cs
└── Usuario.cs
├── AulaRepositoryPattern.sln
├── AulaRepositoryPattern.sln.DotSettings
└── AulaRepositoryPattern
├── AulaRepositoryPattern.csproj
├── Controllers
├── CarroController.cs
├── UsuarioController.cs
└── WeatherForecastController.cs
├── Program.cs
├── Properties
└── launchSettings.json
├── Startup.cs
├── WeatherForecast.cs
├── appsettings.Development.json
└── appsettings.json
/.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 | [Ll]og/
33 | [Ll]ogs/
34 |
35 | # Visual Studio 2015/2017 cache/options directory
36 | .vs/
37 | # Uncomment if you have tasks that create the project's static files in wwwroot
38 | #wwwroot/
39 |
40 | # Visual Studio 2017 auto generated files
41 | Generated\ Files/
42 |
43 | # MSTest test Results
44 | [Tt]est[Rr]esult*/
45 | [Bb]uild[Ll]og.*
46 |
47 | # NUnit
48 | *.VisualState.xml
49 | TestResult.xml
50 | nunit-*.xml
51 |
52 | # Build Results of an ATL Project
53 | [Dd]ebugPS/
54 | [Rr]eleasePS/
55 | dlldata.c
56 |
57 | # Benchmark Results
58 | BenchmarkDotNet.Artifacts/
59 |
60 | # .NET Core
61 | project.lock.json
62 | project.fragment.lock.json
63 | artifacts/
64 |
65 | # Tye
66 | .tye/
67 |
68 | # ASP.NET Scaffolding
69 | ScaffoldingReadMe.txt
70 |
71 | # StyleCop
72 | StyleCopReport.xml
73 |
74 | # Files built by Visual Studio
75 | *_i.c
76 | *_p.c
77 | *_h.h
78 | *.ilk
79 | *.meta
80 | *.obj
81 | *.iobj
82 | *.pch
83 | *.pdb
84 | *.ipdb
85 | *.pgc
86 | *.pgd
87 | *.rsp
88 | *.sbr
89 | *.tlb
90 | *.tli
91 | *.tlh
92 | *.tmp
93 | *.tmp_proj
94 | *_wpftmp.csproj
95 | *.log
96 | *.vspscc
97 | *.vssscc
98 | .builds
99 | *.pidb
100 | *.svclog
101 | *.scc
102 |
103 | # Chutzpah Test files
104 | _Chutzpah*
105 |
106 | # Visual C++ cache files
107 | ipch/
108 | *.aps
109 | *.ncb
110 | *.opendb
111 | *.opensdf
112 | *.sdf
113 | *.cachefile
114 | *.VC.db
115 | *.VC.VC.opendb
116 |
117 | # Visual Studio profiler
118 | *.psess
119 | *.vsp
120 | *.vspx
121 | *.sap
122 |
123 | # Visual Studio Trace Files
124 | *.e2e
125 |
126 | # TFS 2012 Local Workspace
127 | $tf/
128 |
129 | # Guidance Automation Toolkit
130 | *.gpState
131 |
132 | # ReSharper is a .NET coding add-in
133 | _ReSharper*/
134 | *.[Rr]e[Ss]harper
135 | *.DotSettings.user
136 |
137 | # TeamCity is a build add-in
138 | _TeamCity*
139 |
140 | # DotCover is a Code Coverage Tool
141 | *.dotCover
142 |
143 | # AxoCover is a Code Coverage Tool
144 | .axoCover/*
145 | !.axoCover/settings.json
146 |
147 | # Coverlet is a free, cross platform Code Coverage Tool
148 | coverage*.json
149 | coverage*.xml
150 | coverage*.info
151 |
152 | # Visual Studio code coverage results
153 | *.coverage
154 | *.coveragexml
155 |
156 | # NCrunch
157 | _NCrunch_*
158 | .*crunch*.local.xml
159 | nCrunchTemp_*
160 |
161 | # MightyMoose
162 | *.mm.*
163 | AutoTest.Net/
164 |
165 | # Web workbench (sass)
166 | .sass-cache/
167 |
168 | # Installshield output folder
169 | [Ee]xpress/
170 |
171 | # DocProject is a documentation generator add-in
172 | DocProject/buildhelp/
173 | DocProject/Help/*.HxT
174 | DocProject/Help/*.HxC
175 | DocProject/Help/*.hhc
176 | DocProject/Help/*.hhk
177 | DocProject/Help/*.hhp
178 | DocProject/Help/Html2
179 | DocProject/Help/html
180 |
181 | # Click-Once directory
182 | publish/
183 |
184 | # Publish Web Output
185 | *.[Pp]ublish.xml
186 | *.azurePubxml
187 | # Note: Comment the next line if you want to checkin your web deploy settings,
188 | # but database connection strings (with potential passwords) will be unencrypted
189 | *.pubxml
190 | *.publishproj
191 |
192 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
193 | # checkin your Azure Web App publish settings, but sensitive information contained
194 | # in these scripts will be unencrypted
195 | PublishScripts/
196 |
197 | # NuGet Packages
198 | *.nupkg
199 | # NuGet Symbol Packages
200 | *.snupkg
201 | # The packages folder can be ignored because of Package Restore
202 | **/[Pp]ackages/*
203 | # except build/, which is used as an MSBuild target.
204 | !**/[Pp]ackages/build/
205 | # Uncomment if necessary however generally it will be regenerated when needed
206 | #!**/[Pp]ackages/repositories.config
207 | # NuGet v3's project.json files produces more ignorable files
208 | *.nuget.props
209 | *.nuget.targets
210 |
211 | # Microsoft Azure Build Output
212 | csx/
213 | *.build.csdef
214 |
215 | # Microsoft Azure Emulator
216 | ecf/
217 | rcf/
218 |
219 | # Windows Store app package directories and files
220 | AppPackages/
221 | BundleArtifacts/
222 | Package.StoreAssociation.xml
223 | _pkginfo.txt
224 | *.appx
225 | *.appxbundle
226 | *.appxupload
227 |
228 | # Visual Studio cache files
229 | # files ending in .cache can be ignored
230 | *.[Cc]ache
231 | # but keep track of directories ending in .cache
232 | !?*.[Cc]ache/
233 |
234 | # Others
235 | ClientBin/
236 | ~$*
237 | *~
238 | *.dbmdl
239 | *.dbproj.schemaview
240 | *.jfm
241 | *.pfx
242 | *.publishsettings
243 | orleans.codegen.cs
244 |
245 | # Including strong name files can present a security risk
246 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
247 | #*.snk
248 |
249 | # Since there are multiple workflows, uncomment next line to ignore bower_components
250 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
251 | #bower_components/
252 |
253 | # RIA/Silverlight projects
254 | Generated_Code/
255 |
256 | # Backup & report files from converting an old project file
257 | # to a newer Visual Studio version. Backup files are not needed,
258 | # because we have git ;-)
259 | _UpgradeReport_Files/
260 | Backup*/
261 | UpgradeLog*.XML
262 | UpgradeLog*.htm
263 | ServiceFabricBackup/
264 | *.rptproj.bak
265 |
266 | # SQL Server files
267 | *.mdf
268 | *.ldf
269 | *.ndf
270 |
271 | # Business Intelligence projects
272 | *.rdl.data
273 | *.bim.layout
274 | *.bim_*.settings
275 | *.rptproj.rsuser
276 | *- [Bb]ackup.rdl
277 | *- [Bb]ackup ([0-9]).rdl
278 | *- [Bb]ackup ([0-9][0-9]).rdl
279 |
280 | # Microsoft Fakes
281 | FakesAssemblies/
282 |
283 | # GhostDoc plugin setting file
284 | *.GhostDoc.xml
285 |
286 | # Node.js Tools for Visual Studio
287 | .ntvs_analysis.dat
288 | node_modules/
289 |
290 | # Visual Studio 6 build log
291 | *.plg
292 |
293 | # Visual Studio 6 workspace options file
294 | *.opt
295 |
296 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
297 | *.vbw
298 |
299 | # Visual Studio LightSwitch build output
300 | **/*.HTMLClient/GeneratedArtifacts
301 | **/*.DesktopClient/GeneratedArtifacts
302 | **/*.DesktopClient/ModelManifest.xml
303 | **/*.Server/GeneratedArtifacts
304 | **/*.Server/ModelManifest.xml
305 | _Pvt_Extensions
306 |
307 | # Paket dependency manager
308 | .paket/paket.exe
309 | paket-files/
310 |
311 | # FAKE - F# Make
312 | .fake/
313 |
314 | # CodeRush personal settings
315 | .cr/personal
316 |
317 | # Python Tools for Visual Studio (PTVS)
318 | __pycache__/
319 | *.pyc
320 |
321 | # Cake - Uncomment if you are using it
322 | # tools/**
323 | # !tools/packages.config
324 |
325 | # Tabs Studio
326 | *.tss
327 |
328 | # Telerik's JustMock configuration file
329 | *.jmconfig
330 |
331 | # BizTalk build output
332 | *.btp.cs
333 | *.btm.cs
334 | *.odx.cs
335 | *.xsd.cs
336 |
337 | # OpenCover UI analysis results
338 | OpenCover/
339 |
340 | # Azure Stream Analytics local run output
341 | ASALocalRun/
342 |
343 | # MSBuild Binary and Structured Log
344 | *.binlog
345 |
346 | # NVidia Nsight GPU debugger configuration file
347 | *.nvuser
348 |
349 | # MFractors (Xamarin productivity tool) working folder
350 | .mfractor/
351 |
352 | # Local History for Visual Studio
353 | .localhistory/
354 |
355 | # BeatPulse healthcheck temp database
356 | healthchecksdb
357 |
358 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
359 | MigrationBackup/
360 |
361 | # Ionide (cross platform F# VS Code tools) working folder
362 | .ionide/
363 |
364 | # Fody - auto-generated XML schema
365 | FodyWeavers.xsd
366 |
367 | ##
368 | ## Visual studio for Mac
369 | ##
370 |
371 |
372 | # globs
373 | Makefile.in
374 | *.userprefs
375 | *.usertasks
376 | config.make
377 | config.status
378 | aclocal.m4
379 | install-sh
380 | autom4te.cache/
381 | *.tar.gz
382 | tarballs/
383 | test-results/
384 |
385 | # Mac bundle stuff
386 | *.dmg
387 | *.app
388 |
389 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
390 | # General
391 | .DS_Store
392 | .AppleDouble
393 | .LSOverride
394 |
395 | # Icon must end with two \r
396 | Icon
397 |
398 |
399 | # Thumbnails
400 | ._*
401 |
402 | # Files that might appear in the root of a volume
403 | .DocumentRevisions-V100
404 | .fseventsd
405 | .Spotlight-V100
406 | .TemporaryItems
407 | .Trashes
408 | .VolumeIcon.icns
409 | .com.apple.timemachine.donotpresent
410 |
411 | # Directories potentially created on remote AFP share
412 | .AppleDB
413 | .AppleDesktop
414 | Network Trash Folder
415 | Temporary Items
416 | .apdisk
417 |
418 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
419 | # Windows thumbnail cache files
420 | Thumbs.db
421 | ehthumbs.db
422 | ehthumbs_vista.db
423 |
424 | # Dump file
425 | *.stackdump
426 |
427 | # Folder config file
428 | [Dd]esktop.ini
429 |
430 | # Recycle Bin used on file shares
431 | $RECYCLE.BIN/
432 |
433 | # Windows Installer files
434 | *.cab
435 | *.msi
436 | *.msix
437 | *.msm
438 | *.msp
439 |
440 | # Windows shortcuts
441 | *.lnk
442 |
443 | # JetBrains Rider
444 | .idea/
445 | *.sln.iml
446 |
447 | ##
448 | ## Visual Studio Code
449 | ##
450 | .vscode/*
451 | !.vscode/settings.json
452 | !.vscode/tasks.json
453 | !.vscode/launch.json
454 | !.vscode/extensions.json
455 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Core/AulaRepositoryPattern.Core.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net5.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Core/Models/Entity.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace AulaRepositoryPattern.Core.Models
4 | {
5 | public abstract class Entity
6 | {
7 | public Guid Id { get; private set; }
8 |
9 | protected Entity (Guid id)
10 | {
11 | Id = id;
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Core/Models/IRepositoryBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq.Expressions;
4 | using System.Threading.Tasks;
5 |
6 | namespace AulaRepositoryPattern.Core.Models
7 | {
8 | public interface IRepositoryBase where TEntity : Entity
9 | {
10 | Task> Obter(Expression> filter = null);
11 | Task ObterPorIdAsync(Guid id);
12 | Task AddAsync(TEntity entity);
13 | Task DeletarAsync(TEntity entity);
14 | Task Atualizar(TEntity entity);
15 | }
16 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/AulaRepositoryPattern.Data.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net5.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Context/AppDbContext.cs:
--------------------------------------------------------------------------------
1 | using AulaRepositoryPattern.Domain;
2 | using Microsoft.EntityFrameworkCore;
3 |
4 | namespace AulaRepositoryPattern.Data.Context
5 | {
6 | public class AppDbContext : DbContext
7 | {
8 | public AppDbContext(DbContextOptions options) : base(options)
9 | {
10 | }
11 |
12 | public DbSet Carros { get; set; }
13 | public DbSet Usuarios { get; set; }
14 | }
15 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Extensions/DataExtensions.cs:
--------------------------------------------------------------------------------
1 | using AulaRepositoryPattern.Core.Models;
2 | using AulaRepositoryPattern.Data.Context;
3 | using AulaRepositoryPattern.Data.Repositories;
4 | using AulaRepositoryPattern.Data.Repositories.Abstractions;
5 | using Microsoft.EntityFrameworkCore;
6 | using Microsoft.Extensions.Configuration;
7 | using Microsoft.Extensions.DependencyInjection;
8 |
9 | namespace AulaRepositoryPattern.Data.Extensions
10 | {
11 | public static class DataExtensions
12 | {
13 | public static IServiceCollection AddEntityFramework(this IServiceCollection services, IConfiguration configuration)
14 | {
15 | services.AddDbContext(options =>
16 | {
17 | options.UseSqlServer(configuration.GetConnectionString("Default"));
18 | });
19 |
20 | return services;
21 | }
22 |
23 | public static IServiceCollection AddRepositories(this IServiceCollection services)
24 | {
25 | services.AddScoped(typeof(IRepositoryBase<>), typeof(RepositoryBase<>));
26 | services.AddScoped();
27 | services.AddScoped();
28 | return services;
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Migrations/20210918183110_initial.Designer.cs:
--------------------------------------------------------------------------------
1 | //
2 | using System;
3 | using AulaRepositoryPattern.Data.Context;
4 | using Microsoft.EntityFrameworkCore;
5 | using Microsoft.EntityFrameworkCore.Infrastructure;
6 | using Microsoft.EntityFrameworkCore.Metadata;
7 | using Microsoft.EntityFrameworkCore.Migrations;
8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
9 |
10 | namespace AulaRepositoryPattern.Data.Migrations
11 | {
12 | [DbContext(typeof(AppDbContext))]
13 | [Migration("20210918183110_initial")]
14 | partial class initial
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.10")
22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
23 |
24 | modelBuilder.Entity("AulaRepositoryPattern.Domain.Carro", b =>
25 | {
26 | b.Property("Id")
27 | .ValueGeneratedOnAdd()
28 | .HasColumnType("uniqueidentifier");
29 |
30 | b.Property("Nome")
31 | .HasMaxLength(100)
32 | .HasColumnType("nvarchar(100)");
33 |
34 | b.Property("Potencia")
35 | .HasColumnType("int");
36 |
37 | b.HasKey("Id");
38 |
39 | b.ToTable("Carros");
40 | });
41 |
42 | modelBuilder.Entity("AulaRepositoryPattern.Domain.Usuario", b =>
43 | {
44 | b.Property("Id")
45 | .ValueGeneratedOnAdd()
46 | .HasColumnType("uniqueidentifier");
47 |
48 | b.Property("Email")
49 | .HasColumnType("nvarchar(max)");
50 |
51 | b.Property("Idade")
52 | .HasColumnType("nvarchar(max)");
53 |
54 | b.Property("Nome")
55 | .HasColumnType("nvarchar(max)");
56 |
57 | b.HasKey("Id");
58 |
59 | b.ToTable("Usuarios");
60 | });
61 | #pragma warning restore 612, 618
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Migrations/20210918183110_initial.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Microsoft.EntityFrameworkCore.Migrations;
3 |
4 | namespace AulaRepositoryPattern.Data.Migrations
5 | {
6 | public partial class initial : Migration
7 | {
8 | protected override void Up(MigrationBuilder migrationBuilder)
9 | {
10 | migrationBuilder.CreateTable(
11 | name: "Carros",
12 | columns: table => new
13 | {
14 | Id = table.Column(type: "uniqueidentifier", nullable: false),
15 | Nome = table.Column(type: "nvarchar(100)", maxLength: 100, nullable: true),
16 | Potencia = table.Column(type: "int", nullable: false)
17 | },
18 | constraints: table =>
19 | {
20 | table.PrimaryKey("PK_Carros", x => x.Id);
21 | });
22 |
23 | migrationBuilder.CreateTable(
24 | name: "Usuarios",
25 | columns: table => new
26 | {
27 | Id = table.Column(type: "uniqueidentifier", nullable: false),
28 | Nome = table.Column(type: "nvarchar(max)", nullable: true),
29 | Email = table.Column(type: "nvarchar(max)", nullable: true),
30 | Idade = table.Column(type: "nvarchar(max)", nullable: true)
31 | },
32 | constraints: table =>
33 | {
34 | table.PrimaryKey("PK_Usuarios", x => x.Id);
35 | });
36 | }
37 |
38 | protected override void Down(MigrationBuilder migrationBuilder)
39 | {
40 | migrationBuilder.DropTable(
41 | name: "Carros");
42 |
43 | migrationBuilder.DropTable(
44 | name: "Usuarios");
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Migrations/20210918190653_fixUsuario.Designer.cs:
--------------------------------------------------------------------------------
1 | //
2 | using System;
3 | using AulaRepositoryPattern.Data.Context;
4 | using Microsoft.EntityFrameworkCore;
5 | using Microsoft.EntityFrameworkCore.Infrastructure;
6 | using Microsoft.EntityFrameworkCore.Metadata;
7 | using Microsoft.EntityFrameworkCore.Migrations;
8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
9 |
10 | namespace AulaRepositoryPattern.Data.Migrations
11 | {
12 | [DbContext(typeof(AppDbContext))]
13 | [Migration("20210918190653_fixUsuario")]
14 | partial class fixUsuario
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.10")
22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
23 |
24 | modelBuilder.Entity("AulaRepositoryPattern.Domain.Carro", b =>
25 | {
26 | b.Property("Id")
27 | .ValueGeneratedOnAdd()
28 | .HasColumnType("uniqueidentifier");
29 |
30 | b.Property("Nome")
31 | .HasMaxLength(100)
32 | .HasColumnType("nvarchar(100)");
33 |
34 | b.Property("Potencia")
35 | .HasColumnType("int");
36 |
37 | b.HasKey("Id");
38 |
39 | b.ToTable("Carros");
40 | });
41 |
42 | modelBuilder.Entity("AulaRepositoryPattern.Domain.Usuario", b =>
43 | {
44 | b.Property("Id")
45 | .ValueGeneratedOnAdd()
46 | .HasColumnType("uniqueidentifier");
47 |
48 | b.Property("Email")
49 | .HasColumnType("nvarchar(max)");
50 |
51 | b.Property("Idade")
52 | .HasColumnType("int");
53 |
54 | b.Property("Nome")
55 | .HasColumnType("nvarchar(max)");
56 |
57 | b.HasKey("Id");
58 |
59 | b.ToTable("Usuarios");
60 | });
61 | #pragma warning restore 612, 618
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Migrations/20210918190653_fixUsuario.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore.Migrations;
2 |
3 | namespace AulaRepositoryPattern.Data.Migrations
4 | {
5 | public partial class fixUsuario : Migration
6 | {
7 | protected override void Up(MigrationBuilder migrationBuilder)
8 | {
9 | migrationBuilder.AlterColumn(
10 | name: "Idade",
11 | table: "Usuarios",
12 | type: "int",
13 | nullable: false,
14 | defaultValue: 0,
15 | oldClrType: typeof(string),
16 | oldType: "nvarchar(max)",
17 | oldNullable: true);
18 | }
19 |
20 | protected override void Down(MigrationBuilder migrationBuilder)
21 | {
22 | migrationBuilder.AlterColumn(
23 | name: "Idade",
24 | table: "Usuarios",
25 | type: "nvarchar(max)",
26 | nullable: true,
27 | oldClrType: typeof(int),
28 | oldType: "int");
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Migrations/AppDbContextModelSnapshot.cs:
--------------------------------------------------------------------------------
1 | //
2 | using System;
3 | using AulaRepositoryPattern.Data.Context;
4 | using Microsoft.EntityFrameworkCore;
5 | using Microsoft.EntityFrameworkCore.Infrastructure;
6 | using Microsoft.EntityFrameworkCore.Metadata;
7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
8 |
9 | namespace AulaRepositoryPattern.Data.Migrations
10 | {
11 | [DbContext(typeof(AppDbContext))]
12 | partial class AppDbContextModelSnapshot : 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.10")
20 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
21 |
22 | modelBuilder.Entity("AulaRepositoryPattern.Domain.Carro", b =>
23 | {
24 | b.Property("Id")
25 | .ValueGeneratedOnAdd()
26 | .HasColumnType("uniqueidentifier");
27 |
28 | b.Property("Nome")
29 | .HasMaxLength(100)
30 | .HasColumnType("nvarchar(100)");
31 |
32 | b.Property("Potencia")
33 | .HasColumnType("int");
34 |
35 | b.HasKey("Id");
36 |
37 | b.ToTable("Carros");
38 | });
39 |
40 | modelBuilder.Entity("AulaRepositoryPattern.Domain.Usuario", b =>
41 | {
42 | b.Property("Id")
43 | .ValueGeneratedOnAdd()
44 | .HasColumnType("uniqueidentifier");
45 |
46 | b.Property("Email")
47 | .HasColumnType("nvarchar(max)");
48 |
49 | b.Property("Idade")
50 | .HasColumnType("int");
51 |
52 | b.Property("Nome")
53 | .HasColumnType("nvarchar(max)");
54 |
55 | b.HasKey("Id");
56 |
57 | b.ToTable("Usuarios");
58 | });
59 | #pragma warning restore 612, 618
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Repositories/Abstractions/ICarroRepository.cs:
--------------------------------------------------------------------------------
1 | using AulaRepositoryPattern.Core.Models;
2 | using AulaRepositoryPattern.Domain;
3 |
4 | namespace AulaRepositoryPattern.Data.Repositories.Abstractions
5 | {
6 | public interface ICarroRepository : IRepositoryBase
7 | {
8 | }
9 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Repositories/Abstractions/IUsuarioRepository.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 | using AulaRepositoryPattern.Core.Models;
3 | using AulaRepositoryPattern.Domain;
4 |
5 | namespace AulaRepositoryPattern.Data.Repositories.Abstractions
6 | {
7 | public interface IUsuarioRepository
8 | {
9 | Task AddAsync(Usuario entity);
10 | }
11 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Repositories/CarroRepository.cs:
--------------------------------------------------------------------------------
1 | using AulaRepositoryPattern.Data.Context;
2 | using AulaRepositoryPattern.Data.Repositories.Abstractions;
3 | using AulaRepositoryPattern.Domain;
4 |
5 | namespace AulaRepositoryPattern.Data.Repositories
6 | {
7 | public class CarroRepository : RepositoryBase, ICarroRepository
8 | {
9 | public CarroRepository(AppDbContext appContext) : base(appContext)
10 | {
11 |
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Repositories/RepositoryBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Linq.Expressions;
5 | using System.Threading.Tasks;
6 | using AulaRepositoryPattern.Core.Models;
7 | using AulaRepositoryPattern.Data.Context;
8 | using Microsoft.EntityFrameworkCore;
9 |
10 | namespace AulaRepositoryPattern.Data.Repositories
11 | {
12 | public class RepositoryBase : IRepositoryBase where TEntity : Entity
13 | {
14 | public readonly DbSet _DbSet;
15 | public readonly AppDbContext _AppDbContext;
16 |
17 | public RepositoryBase(AppDbContext appContext)
18 | {
19 | _DbSet = appContext.Set();
20 | _AppDbContext = appContext;
21 | }
22 |
23 | public async Task> Obter(Expression> filter = null)
24 | {
25 | var query = _DbSet.AsQueryable();
26 |
27 | if (filter != null)
28 | query = query
29 | .Where(filter)
30 | .AsNoTracking();
31 |
32 | return await query.ToListAsync();
33 | }
34 |
35 | public async Task ObterPorIdAsync(Guid id)
36 | {
37 | return await _DbSet.FindAsync(id);
38 | }
39 |
40 | public async Task AddAsync(TEntity entity)
41 | {
42 | await _DbSet.AddAsync(entity);
43 | await _AppDbContext.SaveChangesAsync();
44 | }
45 |
46 | public async Task DeletarAsync(TEntity entity)
47 | {
48 | _DbSet.Remove(entity);
49 | await _AppDbContext.SaveChangesAsync();
50 | }
51 |
52 | public async Task Atualizar(TEntity entity)
53 | {
54 | _DbSet.Update(entity);
55 | await _AppDbContext.SaveChangesAsync();
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Data/Repositories/UsuarioRepository.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 | using AulaRepositoryPattern.Core.Models;
3 | using AulaRepositoryPattern.Data.Repositories.Abstractions;
4 | using AulaRepositoryPattern.Domain;
5 |
6 | namespace AulaRepositoryPattern.Data.Repositories
7 | {
8 | public class UsuarioRepository : IUsuarioRepository
9 | {
10 | private readonly IRepositoryBase _repositoryBase;
11 |
12 | public UsuarioRepository(IRepositoryBase repositoryBase)
13 | {
14 | _repositoryBase = repositoryBase;
15 | }
16 |
17 | public async Task AddAsync(Usuario entity)
18 | {
19 | await _repositoryBase.AddAsync(entity);
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Domain/AulaRepositoryPattern.Domain.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net5.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Domain/Carro.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.ComponentModel.DataAnnotations;
3 | using AulaRepositoryPattern.Core.Models;
4 |
5 | namespace AulaRepositoryPattern.Domain
6 | {
7 | public class Carro : Entity
8 | {
9 | [StringLength(100)]
10 | public string Nome { get; private set; }
11 |
12 | public int Potencia { get; private set; }
13 |
14 | public Carro(Guid id, string nome, int potencia) : base(id)
15 | {
16 | Nome = nome;
17 | Potencia = potencia;
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.Domain/Usuario.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using AulaRepositoryPattern.Core.Models;
3 |
4 | namespace AulaRepositoryPattern.Domain
5 | {
6 | public class Usuario : Entity
7 | {
8 | public string Nome { get; private set; }
9 | public string Email { get; private set; }
10 | public int Idade { get; private set; }
11 |
12 | public Usuario(Guid id, string nome, string email, int idade) : base(id)
13 | {
14 | Nome = nome;
15 | Email = email;
16 | Idade = idade;
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AulaRepositoryPattern", "AulaRepositoryPattern\AulaRepositoryPattern.csproj", "{80CDE2FE-29A0-4EA7-8ED7-6EE91D8EFF46}"
4 | EndProject
5 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AulaRepositoryPattern.Domain", "AulaRepositoryPattern.Domain\AulaRepositoryPattern.Domain.csproj", "{D2DAD8B3-0D87-4C5F-BF22-8BD921449FA9}"
6 | EndProject
7 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AulaRepositoryPattern.Data", "AulaRepositoryPattern.Data\AulaRepositoryPattern.Data.csproj", "{B40CBC66-FC17-42F9-8ED6-EBACADC8F856}"
8 | EndProject
9 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AulaRepositoryPattern.Core", "AulaRepositoryPattern.Core\AulaRepositoryPattern.Core.csproj", "{8580C642-65CC-40FA-8BAC-FC432E21361C}"
10 | EndProject
11 | Global
12 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
13 | Debug|Any CPU = Debug|Any CPU
14 | Release|Any CPU = Release|Any CPU
15 | EndGlobalSection
16 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
17 | {80CDE2FE-29A0-4EA7-8ED7-6EE91D8EFF46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18 | {80CDE2FE-29A0-4EA7-8ED7-6EE91D8EFF46}.Debug|Any CPU.Build.0 = Debug|Any CPU
19 | {80CDE2FE-29A0-4EA7-8ED7-6EE91D8EFF46}.Release|Any CPU.ActiveCfg = Release|Any CPU
20 | {80CDE2FE-29A0-4EA7-8ED7-6EE91D8EFF46}.Release|Any CPU.Build.0 = Release|Any CPU
21 | {D2DAD8B3-0D87-4C5F-BF22-8BD921449FA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22 | {D2DAD8B3-0D87-4C5F-BF22-8BD921449FA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
23 | {D2DAD8B3-0D87-4C5F-BF22-8BD921449FA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
24 | {D2DAD8B3-0D87-4C5F-BF22-8BD921449FA9}.Release|Any CPU.Build.0 = Release|Any CPU
25 | {B40CBC66-FC17-42F9-8ED6-EBACADC8F856}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26 | {B40CBC66-FC17-42F9-8ED6-EBACADC8F856}.Debug|Any CPU.Build.0 = Debug|Any CPU
27 | {B40CBC66-FC17-42F9-8ED6-EBACADC8F856}.Release|Any CPU.ActiveCfg = Release|Any CPU
28 | {B40CBC66-FC17-42F9-8ED6-EBACADC8F856}.Release|Any CPU.Build.0 = Release|Any CPU
29 | {8580C642-65CC-40FA-8BAC-FC432E21361C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30 | {8580C642-65CC-40FA-8BAC-FC432E21361C}.Debug|Any CPU.Build.0 = Debug|Any CPU
31 | {8580C642-65CC-40FA-8BAC-FC432E21361C}.Release|Any CPU.ActiveCfg = Release|Any CPU
32 | {8580C642-65CC-40FA-8BAC-FC432E21361C}.Release|Any CPU.Build.0 = Release|Any CPU
33 | EndGlobalSection
34 | EndGlobal
35 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern.sln.DotSettings:
--------------------------------------------------------------------------------
1 |
2 | True
3 | True
4 | True
5 | True
6 | True
--------------------------------------------------------------------------------
/AulaRepositoryPattern/AulaRepositoryPattern.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 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern/Controllers/CarroController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using AulaRepositoryPattern.Data.Repositories.Abstractions;
4 | using AulaRepositoryPattern.Domain;
5 | using Microsoft.AspNetCore.Mvc;
6 |
7 | namespace AulaRepositoryPattern.Controllers
8 | {
9 | [ApiController]
10 | [Route("[controller]")]
11 | public class CarroController : ControllerBase
12 | {
13 | private readonly ICarroRepository _carroRepository;
14 |
15 | public CarroController(ICarroRepository carroRepository)
16 | {
17 | _carroRepository = carroRepository;
18 | }
19 |
20 | [HttpPost]
21 | public async Task AdicionarCarro()
22 | {
23 | var carro = new Carro(Guid.NewGuid(), "Carro", 400);
24 | await _carroRepository.AddAsync(carro);
25 | return Ok(carro);
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern/Controllers/UsuarioController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using AulaRepositoryPattern.Data.Repositories.Abstractions;
4 | using AulaRepositoryPattern.Domain;
5 | using Microsoft.AspNetCore.Mvc;
6 |
7 | namespace AulaRepositoryPattern.Controllers
8 | {
9 | [ApiController]
10 | [Route("[controller]")]
11 | public class UsuarioController : ControllerBase
12 | {
13 | private readonly IUsuarioRepository _usuarioRepository;
14 |
15 | public UsuarioController(IUsuarioRepository usuarioRepository)
16 | {
17 | _usuarioRepository = usuarioRepository;
18 | }
19 |
20 | [HttpPost]
21 | public async Task AddUsuario()
22 | {
23 | var usuario = new Usuario(Guid.NewGuid(), "Cristian", "Email@Email.com", 25);
24 | await _usuarioRepository.AddAsync(usuario);
25 | return Ok(usuario);
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern/Controllers/WeatherForecastController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Mvc;
6 | using Microsoft.Extensions.Logging;
7 |
8 | namespace AulaRepositoryPattern.Controllers
9 | {
10 | [ApiController]
11 | [Route("[controller]")]
12 | public class WeatherForecastController : ControllerBase
13 | {
14 | private static readonly string[] Summaries = new[]
15 | {
16 | "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
17 | };
18 |
19 | private readonly ILogger _logger;
20 |
21 | public WeatherForecastController(ILogger logger)
22 | {
23 | _logger = logger;
24 | }
25 |
26 | [HttpGet]
27 | public IEnumerable Get()
28 | {
29 | var rng = new Random();
30 | return Enumerable.Range(1, 5).Select(index => new WeatherForecast
31 | {
32 | Date = DateTime.Now.AddDays(index),
33 | TemperatureC = rng.Next(-20, 55),
34 | Summary = Summaries[rng.Next(Summaries.Length)]
35 | })
36 | .ToArray();
37 | }
38 | }
39 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Hosting;
6 | using Microsoft.Extensions.Configuration;
7 | using Microsoft.Extensions.Hosting;
8 | using Microsoft.Extensions.Logging;
9 |
10 | namespace AulaRepositoryPattern
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 => { webBuilder.UseStartup(); });
22 | }
23 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern/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:15837",
8 | "sslPort": 44314
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 | "AulaRepositoryPattern": {
21 | "commandName": "Project",
22 | "dotnetRunMessages": "true",
23 | "launchBrowser": true,
24 | "launchUrl": "swagger",
25 | "applicationUrl": "https://localhost:5001;http://localhost:5000",
26 | "environmentVariables": {
27 | "ASPNETCORE_ENVIRONMENT": "Development"
28 | }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern/Startup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using AulaRepositoryPattern.Data.Extensions;
6 | using Microsoft.AspNetCore.Builder;
7 | using Microsoft.AspNetCore.Hosting;
8 | using Microsoft.AspNetCore.HttpsPolicy;
9 | using Microsoft.AspNetCore.Mvc;
10 | using Microsoft.Extensions.Configuration;
11 | using Microsoft.Extensions.DependencyInjection;
12 | using Microsoft.Extensions.Hosting;
13 | using Microsoft.Extensions.Logging;
14 | using Microsoft.OpenApi.Models;
15 |
16 | namespace AulaRepositoryPattern
17 | {
18 | public class Startup
19 | {
20 | public Startup(IConfiguration configuration)
21 | {
22 | Configuration = configuration;
23 | }
24 |
25 | public IConfiguration Configuration { get; }
26 |
27 | // This method gets called by the runtime. Use this method to add services to the container.
28 | public void ConfigureServices(IServiceCollection services)
29 | {
30 | services.AddControllers();
31 | services.AddSwaggerGen(c =>
32 | {
33 | c.SwaggerDoc("v1", new OpenApiInfo {Title = "AulaRepositoryPattern", Version = "v1"});
34 | });
35 |
36 | services.AddEntityFramework(Configuration);
37 | services.AddRepositories();
38 | }
39 |
40 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
41 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
42 | {
43 | if (env.IsDevelopment())
44 | {
45 | app.UseDeveloperExceptionPage();
46 | app.UseSwagger();
47 | app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AulaRepositoryPattern v1"));
48 | }
49 |
50 | app.UseHttpsRedirection();
51 |
52 | app.UseRouting();
53 |
54 | app.UseAuthorization();
55 |
56 | app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
57 | }
58 | }
59 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern/WeatherForecast.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace AulaRepositoryPattern
4 | {
5 | public class WeatherForecast
6 | {
7 | public DateTime Date { get; set; }
8 |
9 | public int TemperatureC { get; set; }
10 |
11 | public int TemperatureF => 32 + (int) (TemperatureC / 0.5556);
12 |
13 | public string Summary { get; set; }
14 | }
15 | }
--------------------------------------------------------------------------------
/AulaRepositoryPattern/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/AulaRepositoryPattern/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "ConnectionStrings": {
10 | "Default": "Server=localhost,1433;Database=AulaDb;User Id=SA;Password=MyPass@word;"
11 | },
12 | "AllowedHosts": "*"
13 | }
14 |
--------------------------------------------------------------------------------