├── .gitignore
├── KodotiSells-Script.sql
├── KodotiSells.bak
├── KodotiSells.sln
├── README.md
├── clear.bat
└── src
├── AspNetClient
├── AspNetClient.csproj
├── AspNetClient.csproj.user
├── Controllers
│ └── ValuesController.cs
├── Program.cs
├── Startup.cs
├── appsettings.Development.json
└── appsettings.json
├── Common
├── Common.csproj
└── Parameters.cs
├── ConsoleClient
├── ConsoleClient.csproj
└── Program.cs
├── Models
├── Client.cs
├── Invoice.cs
├── InvoiceDetail.cs
├── Models.csproj
└── Product.cs
├── Repository.Interfaces
├── Actions
│ ├── ICreateRepository.cs
│ ├── IReadRepository.cs
│ ├── IRemoveRepository.cs
│ └── IUpdateRepository.cs
├── IClientRepository.cs
├── IInvoiceDetailRepository.cs
├── IInvoiceRepository.cs
├── IProductRepository.cs
└── Repository.Interfaces.csproj
├── Repository.SqlServer
├── ClientRepository.cs
├── InvoiceDetailRepository.cs
├── InvoiceRepository.cs
├── ProductRepository.cs
├── Repository.SqlServer.csproj
└── Repository.cs
├── Services
├── InvoiceService.cs
└── Services.csproj
├── UnitOfWork.Interfaces
├── IUnitOfWork.cs
├── IUnitOfWorkAdapter.cs
├── IUnitOfWorkRepository.cs
└── UnitOfWork.Interfaces.csproj
└── UnitOfWork.SqlServer
├── UnitOfWork.SqlServer.csproj
├── UnitOfWorkSqlServer.cs
├── UnitOfWorkSqlServerAdapter.cs
└── UnitOfWorkSqlServerRepository.cs
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 |
3 | # User-specific files (MonoDevelop/Xamarin Studio)
4 | *.userprefs
5 |
6 | # Build results
7 | [Dd]ebug/
8 | [Dd]ebugPublic/
9 | [Rr]elease/
10 | [Rr]eleases/
11 | x64/
12 | x86/
13 | bld/
14 | [Bb]in/
15 | [Oo]bj/
16 | [Ll]og/
17 |
18 | # Visual Studio 2015 cache/options directory
19 | .vs/
20 | # Uncomment if you have tasks that create the project's static files in wwwroot
21 | #wwwroot/
22 |
23 | # MSTest test Results
24 | [Tt]est[Rr]esult*/
25 | [Bb]uild[Ll]og.*
26 |
27 | # NUNIT
28 | *.VisualState.xml
29 | TestResult.xml
30 |
31 | # Build Results of an ATL Project
32 | [Dd]ebugPS/
33 | [Rr]eleasePS/
34 | dlldata.c
35 |
36 | # .NET Core
37 | project.lock.json
38 | project.fragment.lock.json
39 | artifacts/
40 | **/Properties/launchSettings.json
41 |
42 | *_i.c
43 | *_p.c
44 | *_i.h
45 | *.ilk
46 | *.meta
47 | *.obj
48 | *.pch
49 | *.pdb
50 | *.pgc
51 | *.pgd
52 | *.rsp
53 | *.sbr
54 | *.tlb
55 | *.tli
56 | *.tlh
57 | *.tmp
58 | *.tmp_proj
59 | *.log
60 | *.vspscc
61 | *.vssscc
62 | .builds
63 | *.pidb
64 | *.svclog
65 | *.scc
66 |
67 | # Chutzpah Test files
68 | _Chutzpah*
69 |
70 | # Visual C++ cache files
71 | ipch/
72 | *.aps
73 | *.ncb
74 | *.opendb
75 | *.opensdf
76 | *.sdf
77 | *.cachefile
78 | *.VC.db
79 | *.VC.VC.opendb
80 |
81 | # Visual Studio profiler
82 | *.psess
83 | *.vsp
84 | *.vspx
85 | *.sap
86 |
87 | # TFS 2012 Local Workspace
88 | $tf/
89 |
90 | # Guidance Automation Toolkit
91 | *.gpState
92 |
93 | # ReSharper is a .NET coding add-in
94 | _ReSharper*/
95 | *.[Rr]e[Ss]harper
96 | *.DotSettings.user
97 |
98 | # JustCode is a .NET coding add-in
99 | .JustCode
100 |
101 | # TeamCity is a build add-in
102 | _TeamCity*
103 |
104 | # DotCover is a Code Coverage Tool
105 | *.dotCover
106 |
107 | # Visual Studio code coverage results
108 | *.coverage
109 | *.coveragexml
110 |
111 | # NCrunch
112 | _NCrunch_*
113 | .*crunch*.local.xml
114 | nCrunchTemp_*
115 |
116 | # MightyMoose
117 | *.mm.*
118 | AutoTest.Net/
119 |
120 | # Web workbench (sass)
121 | .sass-cache/
122 |
123 | # Installshield output folder
124 | [Ee]xpress/
125 |
126 | # DocProject is a documentation generator add-in
127 | DocProject/buildhelp/
128 | DocProject/Help/*.HxT
129 | DocProject/Help/*.HxC
130 | DocProject/Help/*.hhc
131 | DocProject/Help/*.hhk
132 | DocProject/Help/*.hhp
133 | DocProject/Help/Html2
134 | DocProject/Help/html
135 |
136 | # Click-Once directory
137 | publish/
138 |
139 | # Publish Web Output
140 | *.[Pp]ublish.xml
141 | *.azurePubxml
142 | # TODO: Comment the next line if you want to checkin your web deploy settings
143 | # but database connection strings (with potential passwords) will be unencrypted
144 | *.pubxml
145 | *.publishproj
146 |
147 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
148 | # checkin your Azure Web App publish settings, but sensitive information contained
149 | # in these scripts will be unencrypted
150 | PublishScripts/
151 |
152 | # NuGet Packages
153 | *.nupkg
154 | # The packages folder can be ignored because of Package Restore
155 | **/packages/*
156 | # except build/, which is used as an MSBuild target.
157 | !**/packages/build/
158 | # Uncomment if necessary however generally it will be regenerated when needed
159 | #!**/packages/repositories.config
160 | # NuGet v3's project.json files produces more ignoreable files
161 | *.nuget.props
162 | *.nuget.targets
163 |
164 | FrontEnd/App_Data/Logs/*
165 |
166 | # Microsoft Azure Build Output
167 | csx/
168 | *.build.csdef
169 |
170 | # Microsoft Azure Emulator
171 | ecf/
172 | rcf/
173 |
174 | # Windows Store app package directories and files
175 | AppPackages/
176 | BundleArtifacts/
177 | Package.StoreAssociation.xml
178 | _pkginfo.txt
179 |
180 | # Visual Studio cache files
181 | # files ending in .cache can be ignored
182 | *.[Cc]ache
183 | # but keep track of directories ending in .cache
184 | !*.[Cc]ache/
185 |
186 | # Others
187 | ClientBin/
188 | ~$*
189 | *~
190 | *.dbmdl
191 | *.dbproj.schemaview
192 | *.jfm
193 | *.pfx
194 | *.publishsettings
195 | node_modules/
196 | orleans.codegen.cs
197 |
198 | # Since there are multiple workflows, uncomment next line to ignore bower_components
199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
200 | #bower_components/
201 |
202 | # RIA/Silverlight projects
203 | Generated_Code/
204 |
205 | # Backup & report files from converting an old project file
206 | # to a newer Visual Studio version. Backup files are not needed,
207 | # because we have git ;-)
208 | _UpgradeReport_Files/
209 | Backup*/
210 | UpgradeLog*.XML
211 | UpgradeLog*.htm
212 |
213 | # SQL Server files
214 | *.mdf
215 | *.ldf
216 |
217 | # Business Intelligence projects
218 | *.rdl.data
219 | *.bim.layout
220 | *.bim_*.settings
221 |
222 | # Microsoft Fakes
223 | FakesAssemblies/
224 |
225 | # GhostDoc plugin setting file
226 | *.GhostDoc.xml
227 |
228 | # Node.js Tools for Visual Studio
229 | .ntvs_analysis.dat
230 |
231 | # Visual Studio 6 build log
232 | *.plg
233 |
234 | # Visual Studio 6 workspace options file
235 | *.opt
236 |
237 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
238 | *.vbw
239 |
240 | # Visual Studio LightSwitch build output
241 | **/*.HTMLClient/GeneratedArtifacts
242 | **/*.DesktopClient/GeneratedArtifacts
243 | **/*.DesktopClient/ModelManifest.xml
244 | **/*.Server/GeneratedArtifacts
245 | **/*.Server/ModelManifest.xml
246 | _Pvt_Extensions
247 |
248 | # Paket dependency manager
249 | .paket/paket.exe
250 | paket-files/
251 |
252 | # FAKE - F# Make
253 | .fake/
254 |
255 | # JetBrains Rider
256 | .idea/
257 | *.sln.iml
258 |
259 | # CodeRush
260 | .cr/
261 |
262 | # Python Tools for Visual Studio (PTVS)
263 | __pycache__/
264 | *.pyc
265 |
266 | # Cake - Uncomment if you are using it
267 | # tools/**
268 | # !tools/packages.config
--------------------------------------------------------------------------------
/KodotiSells-Script.sql:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Anexsoft/ADO.NET-Unit-Of-Work-Repository-Pattern-con-.NET-Core/c506bd2f6b4926905f96f0d7fa31b0792316dbd6/KodotiSells-Script.sql
--------------------------------------------------------------------------------
/KodotiSells.bak:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Anexsoft/ADO.NET-Unit-Of-Work-Repository-Pattern-con-.NET-Core/c506bd2f6b4926905f96f0d7fa31b0792316dbd6/KodotiSells.bak
--------------------------------------------------------------------------------
/KodotiSells.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29306.81
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "01. Clients", "01. Clients", "{18D5AE78-17AB-4756-B139-EAF989F3AECC}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "02. Models", "02. Models", "{AEA4C4A5-1410-4B10-8989-72649FAEF721}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleClient", "src\ConsoleClient\ConsoleClient.csproj", "{7004EABE-B003-447C-B0CF-EC8FD44BDA67}"
11 | EndProject
12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Models", "src\Models\Models.csproj", "{12A6B965-6EC4-419A-B584-50E5EA7AACB9}"
13 | EndProject
14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "03. Services", "03. Services", "{61CC4FF6-F7A0-408F-B42E-FFBAB7C60A36}"
15 | EndProject
16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Services", "src\Services\Services.csproj", "{2DE4338D-4348-4D51-AB60-87B4488908D0}"
17 | EndProject
18 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "06. Common", "06. Common", "{0DF639F8-49CE-4E47-A507-D395DD5A915A}"
19 | EndProject
20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common", "src\Common\Common.csproj", "{3E987B4B-B8EE-494D-8F21-830C97E1C2C9}"
21 | EndProject
22 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04. UnitOfWork", "04. UnitOfWork", "{C7EFF3D4-3BF7-4D0A-804D-C11A181A21BA}"
23 | EndProject
24 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "05. Repository", "05. Repository", "{B6562BED-C211-4B9E-8B5F-3E87C01C9963}"
25 | EndProject
26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitOfWork.Interfaces", "src\UnitOfWork.Interfaces\UnitOfWork.Interfaces.csproj", "{89D39144-2BD4-451B-9407-69ED076C856E}"
27 | EndProject
28 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitOfWork.SqlServer", "src\UnitOfWork.SqlServer\UnitOfWork.SqlServer.csproj", "{7B1A5658-7890-4C04-B844-D028BE5E5E4A}"
29 | EndProject
30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Repository.Interfaces", "src\Repository.Interfaces\Repository.Interfaces.csproj", "{D67FB62C-F377-4490-A352-0FC09F029C9A}"
31 | EndProject
32 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Repository.SqlServer", "src\Repository.SqlServer\Repository.SqlServer.csproj", "{5B668A86-F27D-4631-A222-EC4C136A1106}"
33 | EndProject
34 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetClient", "src\AspNetClient\AspNetClient.csproj", "{165EC0F4-DCA6-4C2A-B001-F3887E251AA5}"
35 | EndProject
36 | Global
37 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
38 | Debug|Any CPU = Debug|Any CPU
39 | Release|Any CPU = Release|Any CPU
40 | EndGlobalSection
41 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
42 | {7004EABE-B003-447C-B0CF-EC8FD44BDA67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43 | {7004EABE-B003-447C-B0CF-EC8FD44BDA67}.Debug|Any CPU.Build.0 = Debug|Any CPU
44 | {7004EABE-B003-447C-B0CF-EC8FD44BDA67}.Release|Any CPU.ActiveCfg = Release|Any CPU
45 | {7004EABE-B003-447C-B0CF-EC8FD44BDA67}.Release|Any CPU.Build.0 = Release|Any CPU
46 | {12A6B965-6EC4-419A-B584-50E5EA7AACB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47 | {12A6B965-6EC4-419A-B584-50E5EA7AACB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
48 | {12A6B965-6EC4-419A-B584-50E5EA7AACB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
49 | {12A6B965-6EC4-419A-B584-50E5EA7AACB9}.Release|Any CPU.Build.0 = Release|Any CPU
50 | {2DE4338D-4348-4D51-AB60-87B4488908D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51 | {2DE4338D-4348-4D51-AB60-87B4488908D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
52 | {2DE4338D-4348-4D51-AB60-87B4488908D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
53 | {2DE4338D-4348-4D51-AB60-87B4488908D0}.Release|Any CPU.Build.0 = Release|Any CPU
54 | {3E987B4B-B8EE-494D-8F21-830C97E1C2C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55 | {3E987B4B-B8EE-494D-8F21-830C97E1C2C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
56 | {3E987B4B-B8EE-494D-8F21-830C97E1C2C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
57 | {3E987B4B-B8EE-494D-8F21-830C97E1C2C9}.Release|Any CPU.Build.0 = Release|Any CPU
58 | {89D39144-2BD4-451B-9407-69ED076C856E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59 | {89D39144-2BD4-451B-9407-69ED076C856E}.Debug|Any CPU.Build.0 = Debug|Any CPU
60 | {89D39144-2BD4-451B-9407-69ED076C856E}.Release|Any CPU.ActiveCfg = Release|Any CPU
61 | {89D39144-2BD4-451B-9407-69ED076C856E}.Release|Any CPU.Build.0 = Release|Any CPU
62 | {7B1A5658-7890-4C04-B844-D028BE5E5E4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63 | {7B1A5658-7890-4C04-B844-D028BE5E5E4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
64 | {7B1A5658-7890-4C04-B844-D028BE5E5E4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
65 | {7B1A5658-7890-4C04-B844-D028BE5E5E4A}.Release|Any CPU.Build.0 = Release|Any CPU
66 | {D67FB62C-F377-4490-A352-0FC09F029C9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67 | {D67FB62C-F377-4490-A352-0FC09F029C9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
68 | {D67FB62C-F377-4490-A352-0FC09F029C9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
69 | {D67FB62C-F377-4490-A352-0FC09F029C9A}.Release|Any CPU.Build.0 = Release|Any CPU
70 | {5B668A86-F27D-4631-A222-EC4C136A1106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71 | {5B668A86-F27D-4631-A222-EC4C136A1106}.Debug|Any CPU.Build.0 = Debug|Any CPU
72 | {5B668A86-F27D-4631-A222-EC4C136A1106}.Release|Any CPU.ActiveCfg = Release|Any CPU
73 | {5B668A86-F27D-4631-A222-EC4C136A1106}.Release|Any CPU.Build.0 = Release|Any CPU
74 | {165EC0F4-DCA6-4C2A-B001-F3887E251AA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75 | {165EC0F4-DCA6-4C2A-B001-F3887E251AA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
76 | {165EC0F4-DCA6-4C2A-B001-F3887E251AA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
77 | {165EC0F4-DCA6-4C2A-B001-F3887E251AA5}.Release|Any CPU.Build.0 = Release|Any CPU
78 | EndGlobalSection
79 | GlobalSection(SolutionProperties) = preSolution
80 | HideSolutionNode = FALSE
81 | EndGlobalSection
82 | GlobalSection(NestedProjects) = preSolution
83 | {7004EABE-B003-447C-B0CF-EC8FD44BDA67} = {18D5AE78-17AB-4756-B139-EAF989F3AECC}
84 | {12A6B965-6EC4-419A-B584-50E5EA7AACB9} = {AEA4C4A5-1410-4B10-8989-72649FAEF721}
85 | {2DE4338D-4348-4D51-AB60-87B4488908D0} = {61CC4FF6-F7A0-408F-B42E-FFBAB7C60A36}
86 | {3E987B4B-B8EE-494D-8F21-830C97E1C2C9} = {0DF639F8-49CE-4E47-A507-D395DD5A915A}
87 | {89D39144-2BD4-451B-9407-69ED076C856E} = {C7EFF3D4-3BF7-4D0A-804D-C11A181A21BA}
88 | {7B1A5658-7890-4C04-B844-D028BE5E5E4A} = {C7EFF3D4-3BF7-4D0A-804D-C11A181A21BA}
89 | {D67FB62C-F377-4490-A352-0FC09F029C9A} = {B6562BED-C211-4B9E-8B5F-3E87C01C9963}
90 | {5B668A86-F27D-4631-A222-EC4C136A1106} = {B6562BED-C211-4B9E-8B5F-3E87C01C9963}
91 | {165EC0F4-DCA6-4C2A-B001-F3887E251AA5} = {18D5AE78-17AB-4756-B139-EAF989F3AECC}
92 | EndGlobalSection
93 | GlobalSection(ExtensibilityGlobals) = postSolution
94 | SolutionGuid = {0A548895-5CAB-4DB4-B115-E740587AC4FE}
95 | EndGlobalSection
96 | EndGlobal
97 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ADO.NET-Unit-Of-Work-Repository-Pattern-con-.NET-Core
2 | Proyecto que usa una arquitectura ADO.NET mediante el patrón Unit Of Work y Repository para proyectos .NET Core.
3 |
4 | ## ¿Por qué usar esta arquitectura?
5 | Algunos programadores se sienten más cómodo con ADO.NET en vez de usar Entity Framework, por eso se creo este proyecto.
6 |
7 | ## ¿Dónde puede entender mejor el uso de esta?
8 | Tenemos un curso completo sobre esta, dale click al siguiente enlace para entender como funciona.
9 |
10 | https://kodoti.com/cursos/adonet-uow-repository-pattern
--------------------------------------------------------------------------------
/clear.bat:
--------------------------------------------------------------------------------
1 | FOR /F "tokens=*" %%G IN ('DIR /B /AD /S bin') DO RMDIR /S /Q "%%G"
2 | FOR /F "tokens=*" %%G IN ('DIR /B /AD /S obj') DO RMDIR /S /Q "%%G"
--------------------------------------------------------------------------------
/src/AspNetClient/AspNetClient.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.2
5 | InProcess
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/src/AspNetClient/AspNetClient.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | IIS Express
5 |
6 |
--------------------------------------------------------------------------------
/src/AspNetClient/Controllers/ValuesController.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using Microsoft.AspNetCore.Mvc;
3 | using Microsoft.Extensions.Configuration;
4 | using Services;
5 |
6 | namespace AspNetClient.Controllers
7 | {
8 | [Route("api/[controller]")]
9 | [ApiController]
10 | public class ValuesController : ControllerBase
11 | {
12 | private readonly IInvoiceService _invoiceService;
13 |
14 | public ValuesController(IInvoiceService invoiceService)
15 | {
16 | _invoiceService = invoiceService;
17 | }
18 |
19 | // GET api/values
20 | [HttpGet]
21 | public ActionResult> Get()
22 | {
23 | return Ok(
24 | _invoiceService.GetAll()
25 | );
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/AspNetClient/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 AspNetClient
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 |
--------------------------------------------------------------------------------
/src/AspNetClient/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.Mvc;
8 | using Microsoft.Extensions.Configuration;
9 | using Microsoft.Extensions.DependencyInjection;
10 | using Microsoft.Extensions.Logging;
11 | using Microsoft.Extensions.Options;
12 | using Services;
13 | using UnitOfWork.Interfaces;
14 | using UnitOfWork.SqlServer;
15 |
16 | namespace AspNetClient
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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
31 |
32 | services.AddTransient();
33 | services.AddTransient();
34 | }
35 |
36 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
37 | public void Configure(IApplicationBuilder app, IHostingEnvironment env)
38 | {
39 | if (env.IsDevelopment())
40 | {
41 | app.UseDeveloperExceptionPage();
42 | }
43 |
44 | app.UseMvc();
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/AspNetClient/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "SqlConnectionString": "Server=LALO-PC\\SQLEXPRESS;Initial Catalog=KodotiSells;User Id=sa;Password=123456;",
3 | "Logging": {
4 | "LogLevel": {
5 | "Default": "Debug",
6 | "System": "Information",
7 | "Microsoft": "Information"
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/AspNetClient/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Warning"
5 | }
6 | },
7 | "AllowedHosts": "*"
8 | }
9 |
--------------------------------------------------------------------------------
/src/Common/Common.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.2
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/src/Common/Parameters.cs:
--------------------------------------------------------------------------------
1 | namespace Common
2 | {
3 | public class Parameters
4 | {
5 | public const string ConnectionString = "Server=DESKTOP-M9KD390\\SQLEXPRESS;Initial Catalog=KodotiSells;Integrated Security=true;";
6 | public const decimal IvaRate = 0.18m;
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/ConsoleClient/ConsoleClient.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.2
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/ConsoleClient/Program.cs:
--------------------------------------------------------------------------------
1 | using Models;
2 | using Services;
3 | using System;
4 | using System.Collections.Generic;
5 | using UnitOfWork.SqlServer;
6 |
7 | namespace ConsoleClient
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | var unitOfWork = new UnitOfWorkSqlServer();
14 |
15 | var invoiceService = new InvoiceService(unitOfWork);
16 |
17 | var result = invoiceService.GetAll();
18 |
19 | var invoice = new Invoice
20 | {
21 | ClientId = 1,
22 | Detail = new List
23 | {
24 | new InvoiceDetail {
25 | ProductId = 1,
26 | Quantity = 5,
27 | Price = 1500
28 | },
29 | new InvoiceDetail {
30 | ProductId = 8,
31 | Quantity = 15,
32 | Price = 125
33 | }
34 | }
35 | };
36 |
37 | invoiceService.Create(invoice);
38 | //invoiceService.Create(invoice);
39 |
40 | //var invoice = new Invoice
41 | //{
42 | // Id = 33,
43 | // ClientId = 1,
44 | // Detail = new List
45 | // {
46 | // new InvoiceDetail {
47 | // ProductId = 1,
48 | // Quantity = 5,
49 | // Price = 1500
50 | // },
51 | // new InvoiceDetail {
52 | // ProductId = 8,
53 | // Quantity = 30,
54 | // Price = 125
55 | // }
56 | // }
57 | //};
58 |
59 | //invoiceService.Update(invoice);
60 |
61 | //invoiceService.Delete(33);
62 |
63 | Console.Read();
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/Models/Client.cs:
--------------------------------------------------------------------------------
1 | namespace Models
2 | {
3 | public class Client
4 | {
5 | public int Id { get; set; }
6 | public string Name { get; set; }
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/Models/Invoice.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Models
4 | {
5 | public class Invoice
6 | {
7 | public int Id { get; set; }
8 |
9 | public int ClientId { get; set; }
10 | public Client Client { get; set; }
11 |
12 | public IEnumerable Detail { get; set; }
13 |
14 | public decimal Iva { get; set; }
15 | public decimal SubTotal { get; set; }
16 | public decimal Total { get; set; }
17 |
18 | public Invoice()
19 | {
20 | Detail = new List();
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Models/InvoiceDetail.cs:
--------------------------------------------------------------------------------
1 | namespace Models
2 | {
3 | public class InvoiceDetail
4 | {
5 | public int Id { get; set; }
6 |
7 | public int ProductId { get; set; }
8 | public Product Product { get; set; }
9 |
10 | public int InvoiceId { get; set; }
11 | public Invoice Invoice { get; set; }
12 |
13 | public int Quantity { get; set; }
14 | public decimal Price { get; set; }
15 | public decimal Iva { get; set; }
16 | public decimal SubTotal { get; set; }
17 | public decimal Total { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Models/Models.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.2
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/src/Models/Product.cs:
--------------------------------------------------------------------------------
1 | namespace Models
2 | {
3 | public class Product
4 | {
5 | public int Id { get; set; }
6 | public string Name { get; set; }
7 | public decimal Price { get; set; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/Repository.Interfaces/Actions/ICreateRepository.cs:
--------------------------------------------------------------------------------
1 | namespace Repository.Interfaces.Actions
2 | {
3 | public interface ICreateRepository where T : class
4 | {
5 | void Create(T t);
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/Repository.Interfaces/Actions/IReadRepository.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Repository.Interfaces.Actions
4 | {
5 | public interface IReadRepository where T : class
6 | {
7 | IEnumerable GetAll();
8 | T Get(Y id);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Repository.Interfaces/Actions/IRemoveRepository.cs:
--------------------------------------------------------------------------------
1 | namespace Repository.Interfaces.Actions
2 | {
3 | public interface IRemoveRepository
4 | {
5 | void Remove(T id);
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/Repository.Interfaces/Actions/IUpdateRepository.cs:
--------------------------------------------------------------------------------
1 | namespace Repository.Interfaces.Actions
2 | {
3 | public interface IUpdateRepository where T : class
4 | {
5 | void Update(T t);
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/Repository.Interfaces/IClientRepository.cs:
--------------------------------------------------------------------------------
1 | using Models;
2 | using Repository.Interfaces.Actions;
3 |
4 | namespace Repository.Interfaces
5 | {
6 | public interface IClientRepository : IReadRepository
7 | {
8 |
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Repository.Interfaces/IInvoiceDetailRepository.cs:
--------------------------------------------------------------------------------
1 | using Models;
2 | using System.Collections.Generic;
3 |
4 | namespace Repository.Interfaces
5 | {
6 | public interface IInvoiceDetailRepository
7 | {
8 | void Create(IEnumerable model, int invoiceId);
9 | IEnumerable GetAllByInvoiceId(int invoiceId);
10 | void RemoveByInvoiceId(int invoiceId);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/Repository.Interfaces/IInvoiceRepository.cs:
--------------------------------------------------------------------------------
1 | using Models;
2 | using Repository.Interfaces.Actions;
3 |
4 | namespace Repository.Interfaces
5 | {
6 | public interface IInvoiceRepository : IReadRepository, ICreateRepository, IUpdateRepository, IRemoveRepository
7 | {
8 |
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Repository.Interfaces/IProductRepository.cs:
--------------------------------------------------------------------------------
1 | using Models;
2 | using Repository.Interfaces.Actions;
3 |
4 | namespace Repository.Interfaces
5 | {
6 | public interface IProductRepository : IReadRepository
7 | {
8 |
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Repository.Interfaces/Repository.Interfaces.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.2
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/Repository.SqlServer/ClientRepository.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Data.SqlClient;
4 | using Models;
5 | using Repository.Interfaces;
6 |
7 | namespace Repository.SqlServer
8 | {
9 | public class ClientRepository : Repository, IClientRepository
10 | {
11 | public ClientRepository(SqlConnection context, SqlTransaction transaction)
12 | {
13 | this._context = context;
14 | this._transaction = transaction;
15 | }
16 |
17 | public Client Get(int id)
18 | {
19 | var command = CreateCommand("SELECT * FROM clients WITH(NOLOCK) WHERE id = @clientId");
20 | command.Parameters.AddWithValue("@clientId", id);
21 |
22 | using (var reader = command.ExecuteReader())
23 | {
24 | reader.Read();
25 |
26 | return new Client
27 | {
28 | Id = Convert.ToInt32(reader["id"]),
29 | Name = reader["name"].ToString()
30 | };
31 | }
32 | }
33 |
34 | public IEnumerable GetAll()
35 | {
36 | throw new System.NotImplementedException();
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/Repository.SqlServer/InvoiceDetailRepository.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Data.SqlClient;
4 | using Models;
5 | using Repository.Interfaces;
6 |
7 | namespace Repository.SqlServer
8 | {
9 | public class InvoiceDetailRepository : Repository, IInvoiceDetailRepository
10 | {
11 | public InvoiceDetailRepository(SqlConnection context, SqlTransaction transaction)
12 | {
13 | this._context = context;
14 | this._transaction = transaction;
15 | }
16 |
17 | public void Create(IEnumerable model, int invoiceId)
18 | {
19 | foreach (var detail in model)
20 | {
21 | var query = "insert into invoicedetail(invoiceId, productId, quantity, price, iva, subTotal, total) values (@invoiceId, @productId, @quantity, @price, @iva, @subTotal, @total)";
22 | var command = CreateCommand(query);
23 |
24 | command.Parameters.AddWithValue("@invoiceId", invoiceId);
25 | command.Parameters.AddWithValue("@productId", detail.ProductId);
26 | command.Parameters.AddWithValue("@quantity", detail.Quantity);
27 | command.Parameters.AddWithValue("@price", detail.Price);
28 | command.Parameters.AddWithValue("@iva", detail.Iva);
29 | command.Parameters.AddWithValue("@subTotal", detail.SubTotal);
30 | command.Parameters.AddWithValue("@total", detail.Total);
31 |
32 | command.ExecuteNonQuery();
33 | }
34 | }
35 |
36 | public void RemoveByInvoiceId(int invoiceId)
37 | {
38 | var command = CreateCommand("DELETE FROM invoicedetail WHERE invoiceId = @invoiceId");
39 | command.Parameters.AddWithValue("@invoiceId", invoiceId);
40 |
41 | command.ExecuteNonQuery();
42 | }
43 |
44 | public IEnumerable GetAllByInvoiceId(int invoiceId)
45 | {
46 | var result = new List();
47 |
48 | var command = CreateCommand("SELECT * FROM invoicedetail WITH(NOLOCK) WHERE invoiceId = @invoiceId");
49 | command.Parameters.AddWithValue("@invoiceId", invoiceId);
50 |
51 | using (var reader = command.ExecuteReader())
52 | {
53 | while (reader.Read())
54 | {
55 | result.Add(new InvoiceDetail
56 | {
57 | Id = Convert.ToInt32(reader["id"]),
58 | ProductId = Convert.ToInt32(reader["productId"]),
59 | Quantity = Convert.ToInt32(reader["quantity"]),
60 | Iva = Convert.ToDecimal(reader["iva"]),
61 | SubTotal = Convert.ToDecimal(reader["subtotal"]),
62 | Total = Convert.ToDecimal(reader["total"])
63 | });
64 | }
65 | }
66 |
67 | return result;
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/Repository.SqlServer/InvoiceRepository.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Data.SqlClient;
4 | using Models;
5 | using Repository.Interfaces;
6 |
7 | namespace Repository.SqlServer
8 | {
9 | public class InvoiceRepository : Repository, IInvoiceRepository
10 | {
11 | public InvoiceRepository(SqlConnection context, SqlTransaction transaction)
12 | {
13 | this._context = context;
14 | this._transaction = transaction;
15 | }
16 |
17 | public void Create(Invoice model)
18 | {
19 | var query = "insert into invoices(clientId, Iva, SubTotal, Total) output INSERTED.ID values (@clientId, @iva, @subTotal, @total)";
20 | var command = CreateCommand(query);
21 |
22 | command.Parameters.AddWithValue("@clientId", model.ClientId);
23 | command.Parameters.AddWithValue("@iva", model.Iva);
24 | command.Parameters.AddWithValue("@subTotal", model.SubTotal);
25 | command.Parameters.AddWithValue("@total", model.Total);
26 |
27 | model.Id = Convert.ToInt32(command.ExecuteScalar());
28 | }
29 |
30 | public void Remove(int id)
31 | {
32 | var command = CreateCommand("DELETE FROM invoices WHERE id = @id");
33 | command.Parameters.AddWithValue("@id", id);
34 |
35 | command.ExecuteNonQuery();
36 | }
37 |
38 | public Invoice Get(int id)
39 | {
40 | var command = CreateCommand("SELECT * FROM invoices WITH(NOLOCK) WHERE id = @id");
41 | command.Parameters.AddWithValue("@id", id);
42 |
43 | using (var reader = command.ExecuteReader())
44 | {
45 | reader.Read();
46 |
47 | return new Invoice
48 | {
49 | Id = Convert.ToInt32(reader["id"]),
50 | Iva = Convert.ToDecimal(reader["iva"]),
51 | SubTotal = Convert.ToDecimal(reader["subtotal"]),
52 | Total = Convert.ToDecimal(reader["total"]),
53 | ClientId = Convert.ToInt32(reader["clientId"])
54 | };
55 | };
56 | }
57 |
58 | public IEnumerable GetAll()
59 | {
60 | var result = new List();
61 |
62 | var command = CreateCommand("SELECT * FROM invoices WITH(NOLOCK)");
63 |
64 | using (var reader = command.ExecuteReader())
65 | {
66 | while (reader.Read())
67 | {
68 | result.Add(new Invoice
69 | {
70 | Id = Convert.ToInt32(reader["id"]),
71 | Iva = Convert.ToDecimal(reader["iva"]),
72 | SubTotal = Convert.ToDecimal(reader["subtotal"]),
73 | Total = Convert.ToDecimal(reader["total"]),
74 | ClientId = Convert.ToInt32(reader["clientId"])
75 | });
76 | }
77 | }
78 |
79 | return result;
80 | }
81 |
82 | public void Update(Invoice model)
83 | {
84 | var query = "update invoices set clientId = @clientId, iva = @iva, subTotal = @subTotal, total = @total WHERE id = @id";
85 | var command = CreateCommand(query);
86 |
87 | command.Parameters.AddWithValue("@clientId", model.ClientId);
88 | command.Parameters.AddWithValue("@iva", model.Iva);
89 | command.Parameters.AddWithValue("@subTotal", model.SubTotal);
90 | command.Parameters.AddWithValue("@total", model.Total);
91 | command.Parameters.AddWithValue("@id", model.Id);
92 |
93 | command.ExecuteNonQuery();
94 | }
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/src/Repository.SqlServer/ProductRepository.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Data.SqlClient;
4 | using Models;
5 | using Repository.Interfaces;
6 |
7 | namespace Repository.SqlServer
8 | {
9 | public class ProductRepository : Repository, IProductRepository
10 | {
11 | public ProductRepository(SqlConnection context, SqlTransaction transaction)
12 | {
13 | this._context = context;
14 | this._transaction = transaction;
15 | }
16 |
17 | public Product Get(int id)
18 | {
19 | var command = CreateCommand("SELECT * FROM products WITH(NOLOCK) WHERE id = @productId");
20 |
21 | command.Parameters.AddWithValue("@productId", id);
22 |
23 | using (var reader = command.ExecuteReader())
24 | {
25 | reader.Read();
26 |
27 | return new Product
28 | {
29 | Id = Convert.ToInt32(reader["id"]),
30 | Price = Convert.ToDecimal(reader["price"]),
31 | Name = reader["name"].ToString()
32 | };
33 | }
34 | }
35 |
36 | public IEnumerable GetAll()
37 | {
38 | throw new System.NotImplementedException();
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/Repository.SqlServer/Repository.SqlServer.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.2
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/Repository.SqlServer/Repository.cs:
--------------------------------------------------------------------------------
1 | using System.Data.SqlClient;
2 |
3 | namespace Repository.SqlServer
4 | {
5 | public abstract class Repository
6 | {
7 | protected SqlConnection _context;
8 | protected SqlTransaction _transaction;
9 |
10 | protected SqlCommand CreateCommand(string query)
11 | {
12 | return new SqlCommand(query, _context, _transaction);
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Services/InvoiceService.cs:
--------------------------------------------------------------------------------
1 | using Common;
2 | using Models;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using UnitOfWork.Interfaces;
6 |
7 | namespace Services
8 | {
9 | public interface IInvoiceService
10 | {
11 | IEnumerable GetAll();
12 | Invoice Get(int id);
13 | void Create(Invoice model);
14 | void Update(Invoice model);
15 | void Delete(int id);
16 | }
17 |
18 | public class InvoiceService : IInvoiceService
19 | {
20 | private IUnitOfWork _unitOfWork;
21 |
22 | public InvoiceService(IUnitOfWork unitOfWork)
23 | {
24 | _unitOfWork = unitOfWork;
25 | }
26 |
27 | public IEnumerable GetAll()
28 | {
29 | using (var context = _unitOfWork.Create())
30 | {
31 | var records = context.Repositories.InvoiceRepository.GetAll();
32 |
33 | foreach (var record in records)
34 | {
35 | record.Client = context.Repositories.ClientRepository.Get(record.ClientId);
36 | record.Detail = context.Repositories.InvoiceDetailRepository.GetAllByInvoiceId(record.Id);
37 |
38 | foreach (var item in record.Detail)
39 | {
40 | item.Product = context.Repositories.ProductRepository.Get(item.ProductId);
41 | }
42 | }
43 |
44 | return records;
45 | }
46 | }
47 |
48 | public Invoice Get(int id)
49 | {
50 | using (var context = _unitOfWork.Create())
51 | {
52 | var result = context.Repositories.InvoiceRepository.Get(id);
53 |
54 | result.Client = context.Repositories.ClientRepository.Get(result.ClientId);
55 | result.Detail = context.Repositories.InvoiceDetailRepository.GetAllByInvoiceId(result.Id);
56 |
57 | foreach (var item in result.Detail)
58 | {
59 | item.Product = context.Repositories.ProductRepository.Get(item.ProductId);
60 | }
61 |
62 | return result;
63 | }
64 | }
65 |
66 | public void Create(Invoice model)
67 | {
68 | PrepareOrder(model);
69 |
70 | using (var context = _unitOfWork.Create())
71 | {
72 | // Header
73 | context.Repositories.InvoiceRepository.Create(model);
74 |
75 | // Detail
76 | context.Repositories.InvoiceDetailRepository.Create(model.Detail, model.Id);
77 |
78 | // Confirm changes
79 | context.SaveChanges();
80 | }
81 | }
82 |
83 | public void Update(Invoice model)
84 | {
85 | PrepareOrder(model);
86 |
87 | using (var context = _unitOfWork.Create())
88 | {
89 | // Header
90 | context.Repositories.InvoiceRepository.Update(model);
91 |
92 | // Detail
93 | context.Repositories.InvoiceDetailRepository.RemoveByInvoiceId(model.Id);
94 | context.Repositories.InvoiceDetailRepository.Create(model.Detail, model.Id);
95 |
96 | // Confirm changes
97 | context.SaveChanges();
98 | }
99 | }
100 |
101 | public void Delete(int id)
102 | {
103 | using (var context = _unitOfWork.Create())
104 | {
105 | context.Repositories.InvoiceRepository.Remove(id);
106 |
107 | // Confirm changes
108 | context.SaveChanges();
109 | }
110 | }
111 |
112 | private void PrepareOrder(Invoice model)
113 | {
114 | foreach (var detail in model.Detail)
115 | {
116 | detail.Total = detail.Quantity * detail.Price;
117 | detail.SubTotal = detail.Total / (1 + Parameters.IvaRate);
118 | detail.Iva = detail.Total - detail.SubTotal;
119 | }
120 |
121 | model.Total = model.Detail.Sum(x => x.Total);
122 | model.Iva = model.Detail.Sum(x => x.Iva);
123 | model.SubTotal = model.Detail.Sum(x => x.SubTotal);
124 | }
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/src/Services/Services.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.2
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/UnitOfWork.Interfaces/IUnitOfWork.cs:
--------------------------------------------------------------------------------
1 | namespace UnitOfWork.Interfaces
2 | {
3 | public interface IUnitOfWork
4 | {
5 | IUnitOfWorkAdapter Create();
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/UnitOfWork.Interfaces/IUnitOfWorkAdapter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace UnitOfWork.Interfaces
4 | {
5 | public interface IUnitOfWorkAdapter : IDisposable
6 | {
7 | IUnitOfWorkRepository Repositories { get; }
8 | void SaveChanges();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/UnitOfWork.Interfaces/IUnitOfWorkRepository.cs:
--------------------------------------------------------------------------------
1 | using Repository.Interfaces;
2 |
3 | namespace UnitOfWork.Interfaces
4 | {
5 | public interface IUnitOfWorkRepository
6 | {
7 | IProductRepository ProductRepository { get; }
8 | IClientRepository ClientRepository { get; }
9 | IInvoiceRepository InvoiceRepository { get; }
10 | IInvoiceDetailRepository InvoiceDetailRepository { get; }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/UnitOfWork.Interfaces/UnitOfWork.Interfaces.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.2
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/UnitOfWork.SqlServer/UnitOfWork.SqlServer.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.2
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/UnitOfWork.SqlServer/UnitOfWorkSqlServer.cs:
--------------------------------------------------------------------------------
1 | using Common;
2 | using Microsoft.Extensions.Configuration;
3 | using UnitOfWork.Interfaces;
4 |
5 | namespace UnitOfWork.SqlServer
6 | {
7 | public class UnitOfWorkSqlServer : IUnitOfWork
8 | {
9 | private readonly IConfiguration _configuration;
10 |
11 | public UnitOfWorkSqlServer(IConfiguration configuration = null)
12 | {
13 | _configuration = configuration;
14 | }
15 |
16 | public IUnitOfWorkAdapter Create()
17 | {
18 | var connectionString = _configuration == null
19 | ? Parameters.ConnectionString
20 | : _configuration.GetValue("SqlConnectionString");
21 |
22 | return new UnitOfWorkSqlServerAdapter(connectionString);
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/UnitOfWork.SqlServer/UnitOfWorkSqlServerAdapter.cs:
--------------------------------------------------------------------------------
1 | using Common;
2 | using System.Data.SqlClient;
3 | using UnitOfWork.Interfaces;
4 |
5 | namespace UnitOfWork.SqlServer
6 | {
7 | public class UnitOfWorkSqlServerAdapter : IUnitOfWorkAdapter
8 | {
9 | private SqlConnection _context { get; set; }
10 | private SqlTransaction _transaction { get; set; }
11 | public IUnitOfWorkRepository Repositories { get; set; }
12 |
13 | public UnitOfWorkSqlServerAdapter(string connectionString)
14 | {
15 | _context = new SqlConnection(connectionString);
16 | _context.Open();
17 |
18 | _transaction = _context.BeginTransaction();
19 |
20 | Repositories = new UnitOfWorkSqlServerRepository(_context, _transaction);
21 | }
22 |
23 | public void Dispose()
24 | {
25 | if (_transaction != null)
26 | {
27 | _transaction.Dispose();
28 | }
29 |
30 | if (_context != null)
31 | {
32 | _context.Close();
33 | _context.Dispose();
34 | }
35 |
36 | Repositories = null;
37 | }
38 |
39 | public void SaveChanges()
40 | {
41 | _transaction.Commit();
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/UnitOfWork.SqlServer/UnitOfWorkSqlServerRepository.cs:
--------------------------------------------------------------------------------
1 | using Repository.Interfaces;
2 | using Repository.SqlServer;
3 | using System.Data.SqlClient;
4 | using UnitOfWork.Interfaces;
5 |
6 | namespace UnitOfWork.SqlServer
7 | {
8 | public class UnitOfWorkSqlServerRepository : IUnitOfWorkRepository
9 | {
10 | public IProductRepository ProductRepository { get; }
11 | public IClientRepository ClientRepository { get; }
12 | public IInvoiceRepository InvoiceRepository { get; }
13 | public IInvoiceDetailRepository InvoiceDetailRepository { get; }
14 |
15 | public UnitOfWorkSqlServerRepository(SqlConnection context, SqlTransaction transaction)
16 | {
17 | ClientRepository = new ClientRepository(context, transaction);
18 | ProductRepository = new ProductRepository(context, transaction);
19 | InvoiceRepository = new InvoiceRepository(context, transaction);
20 | InvoiceDetailRepository = new InvoiceDetailRepository(context, transaction);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------