Skip to content

Commit 6d54363

Browse files
committed
Added SQL Server data store option
1 parent ab60c7e commit 6d54363

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

.template.config/template.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"**/*.filelist",
6363
"**/*.user",
6464
"**/*.lock.json",
65-
".vscode/**/*"
65+
".vscode/**/*",
66+
"nuget.csproj"
6667
],
6768
"modifiers": [
6869
{

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ The solution is a set of .NET 5 C# projects. Each project has nullable type chec
1616
- _CleanArchTemplate.Infrastructure_ - This project contains the implemenation of any logic that needs to communicate with outside entities, such as a database, the file system, other HTTP API's, and so forth.
1717
- _CleanArchTemplate.Api_ - This is the front-end of the application, and provides the start-up code and the API endpoint entry points. It is an ASP<nowiki/>.NET Web API project.
1818

19+
### Data Services
20+
21+
The architecture utilizes data services to manage access to the data store. Each data service is meant to isolate a single domain action within the service; retrieving a record and its related entities, or persisting them. For instance, the data store operation to get all of the people in the data store (as implemented by the sample `GetPeopleQuery.Handler.cs` file) is self contained in the data service `IGetPeopleDataService`.
22+
23+
The `AddInfrastructure` method will scan the `CleanArchTemplate.Application` project for all interfaces that start with `I`, and end with `DataSevice`, and will pair them with any classes in the `CleanArchTemplate.Infrastructure` project that implement the interfaces. So, if you have a data source that stores pets, you can create an interface called `IGetPetsDataService`, create an implementation class for it, and the `AddInfrastructure` method will pick it up and add it to the dependency injection container.
24+
25+
It is important to note that you do not need to register these data services directly (with Scoped lifetime). The `AddInfrastructure` method uses the following reflection code to automatically do this:
26+
27+
```C#
28+
private static readonly Regex InterfacePattern = new Regex("I(?:.+)DataService", RegexOptions.Compiled);
29+
...
30+
(from c in typeof(Application.DependencyInjection).Assembly.GetTypes()
31+
where c.IsInterface && InterfacePattern.IsMatch(c.Name)
32+
from i in typeof(Infrastructure.DependencyInjection).Assembly.GetTypes()
33+
where c.IsAssignableFrom(i)
34+
select new
35+
{
36+
Contract = c,
37+
Implementation = i
38+
}).ToList()
39+
.ForEach(x => services.AddScoped(x.Contract, x.Implementation));
40+
```
41+
1942
### Endpoints
2043

2144
Endpoints work with a class called `EndpointResult`. This class is separate from the `ActionResults` used by ASP<nowiki/>.NET, and provides for a separation of concerns from the API itself. When the call to `IMediator.Send` returns, it is cast to an `ActionResult` via the `ToActionResult` extension method.

src/CleanArchTemplate.Infrastructure/CleanArchTemplate.Infrastructure.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<!-- #if (includePostgres) -->
1717
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.10" />
1818
<PackageReference Include="EFCore.NamingConventions" Version="5.0.2" />
19+
<!-- #endif -->
20+
<!-- #if (includeSqlServer) -->
21+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.12" />
1922
<!-- #endif -->
2023
</ItemGroup>
2124

src/CleanArchTemplate.Infrastructure/DependencyInjection.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
3131
).UseSnakeCaseNamingConvention();
3232
});
3333
#endif
34+
#if (includeSqlServer)
35+
services.AddDbContext<CleanArchTemplateDbContext>(options =>
36+
{
37+
options.UseSqlServer(
38+
configuration.GetConnectionString("DefaultConnection"),
39+
x => x.MigrationsHistoryTable("__EFMigrationsHistory")
40+
);
41+
});
42+
#endif
3443
#if (includeDB)
3544
services.AddScoped<ICleanArchTemplateDbContext, CleanArchTemplateDbContext>();
3645
#endif

0 commit comments

Comments
 (0)