Skip to content

Commit 69bd599

Browse files
authored
Add tye to work with Dapr (#12)
1 parent e0d7e06 commit 69bd599

58 files changed

Lines changed: 979 additions & 361 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.editorconfig

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
# C# files
12+
[*.{cs, cshtml}]
13+
indent_size = 4
14+
# New line preferences
15+
csharp_new_line_before_open_brace = all
16+
csharp_new_line_before_else = true
17+
csharp_new_line_before_catch = true
18+
csharp_new_line_before_finally = true
19+
csharp_new_line_before_members_in_object_initializers = true
20+
csharp_new_line_before_members_in_anonymous_types = true
21+
csharp_new_line_within_query_expression_clauses = true
22+
23+
# Indentation preferences
24+
csharp_indent_block_contents = true
25+
csharp_indent_braces = false
26+
csharp_indent_case_contents = true
27+
csharp_indent_switch_labels = true
28+
csharp_indent_labels = one_less_than_current
29+
30+
# avoid this. unless absolutely necessary
31+
dotnet_style_qualification_for_field = false:suggestion
32+
dotnet_style_qualification_for_property = false:suggestion
33+
dotnet_style_qualification_for_method = false:suggestion
34+
dotnet_style_qualification_for_event = false:suggestion
35+
36+
# only use var when it's obvious what the variable type is
37+
# csharp_style_var_for_built_in_types = false:none
38+
# csharp_style_var_when_type_is_apparent = false:none
39+
# csharp_style_var_elsewhere = false:suggestion
40+
41+
# use language keywords instead of BCL types
42+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
43+
dotnet_style_predefined_type_for_member_access = true:suggestion
44+
45+
# name all constant fields using PascalCase
46+
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
47+
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
48+
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
49+
50+
dotnet_naming_symbols.constant_fields.applicable_kinds = field
51+
dotnet_naming_symbols.constant_fields.required_modifiers = const
52+
53+
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
54+
55+
# static fields should have s_ prefix
56+
dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
57+
dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
58+
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
59+
60+
dotnet_naming_symbols.static_fields.applicable_kinds = field
61+
dotnet_naming_symbols.static_fields.required_modifiers = static
62+
63+
dotnet_naming_style.static_prefix_style.required_prefix = s_
64+
dotnet_naming_style.static_prefix_style.capitalization = camel_case
65+
66+
# internal and private fields should be _camelCase
67+
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
68+
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
69+
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
70+
71+
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
72+
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
73+
74+
dotnet_naming_style.camel_case_underscore_style.required_prefix = _
75+
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
76+
77+
# Code style defaults
78+
dotnet_sort_system_directives_first = true
79+
csharp_preserve_single_line_blocks = true
80+
csharp_preserve_single_line_statements = false
81+
82+
# Expression-level preferences
83+
dotnet_style_object_initializer = true:suggestion
84+
dotnet_style_collection_initializer = true:suggestion
85+
dotnet_style_explicit_tuple_names = true:suggestion
86+
dotnet_style_coalesce_expression = true:suggestion
87+
dotnet_style_null_propagation = true:suggestion
88+
89+
# Expression-bodied members
90+
csharp_style_expression_bodied_methods = false:none
91+
csharp_style_expression_bodied_constructors = false:none
92+
csharp_style_expression_bodied_operators = false:none
93+
csharp_style_expression_bodied_properties = true:none
94+
csharp_style_expression_bodied_indexers = true:none
95+
csharp_style_expression_bodied_accessors = true:none
96+
97+
# Pattern matching
98+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
99+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
100+
csharp_style_inlined_variable_declaration = true:suggestion
101+
102+
# Null checking preferences
103+
csharp_style_throw_expression = true:suggestion
104+
csharp_style_conditional_delegate_call = true:suggestion
105+
106+
# Space preferences
107+
csharp_space_after_cast = false
108+
csharp_space_after_colon_in_inheritance_clause = true
109+
csharp_space_after_comma = true
110+
csharp_space_after_dot = false
111+
csharp_space_after_keywords_in_control_flow_statements = true
112+
csharp_space_after_semicolon_in_for_statement = true
113+
csharp_space_around_binary_operators = before_and_after
114+
csharp_space_around_declaration_statements = do_not_ignore
115+
csharp_space_before_colon_in_inheritance_clause = true
116+
csharp_space_before_comma = false
117+
csharp_space_before_dot = false
118+
csharp_space_before_open_square_brackets = false
119+
csharp_space_before_semicolon_in_for_statement = false
120+
csharp_space_between_empty_square_brackets = false
121+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
122+
csharp_space_between_method_call_name_and_opening_parenthesis = false
123+
csharp_space_between_method_call_parameter_list_parentheses = false
124+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
125+
csharp_space_between_method_declaration_name_and_open_parenthesis = false
126+
csharp_space_between_method_declaration_parameter_list_parentheses = false
127+
csharp_space_between_parentheses = false
128+
csharp_space_between_square_brackets = false
129+
130+
[*.{asm,inc}]
131+
indent_size = 8
132+
133+
# Xml project files
134+
[*.{csproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
135+
indent_size = 2
136+
137+
# Xml config files
138+
[*.{props,targets,config,nuspec}]
139+
indent_size = 2
140+
141+
[CMakeLists.txt]
142+
indent_size = 2
143+
144+
[*.cmd]
145+
indent_size = 2
146+
147+
[*.md]
148+
insert_final_newline = false
149+
trim_trailing_whitespace = false
150+
151+
[*.ts]
152+
indent_size = 2
153+
end_of_line = lf
154+
155+
[*.tsx]
156+
indent_size = 2
157+
end_of_line = lf
158+
159+
[.yml]
160+
indent_size = 2
161+
end_of_line = lf

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,5 @@ MigrationBackup/
348348

349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351+
352+
.tye/

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ If you liked `practical-dapr` project or if it helped you, please give a star :s
2727

2828
- We need Docker for Desktop installed
2929
- Follow those steps at https://github.com/dapr/cli to install Dapr CLI
30+
- Follow these steps at https://github.com/dotnet/tye/blob/master/docs/getting_started.md to install `tye` CLI
3031

3132
```bash
3233
$ docker-compose -f docker-compose.yml -f docker-compose.override.yml run sqlserver
@@ -49,6 +50,12 @@ $ cd src\Inventory\CoolStore.InventoryApi
4950
$ dapr run --app-id inventory-api --grpc-port 5303 --port 5302 --app-port 5301 dotnet run
5051
```
5152

53+
### Run with tye on local development
54+
55+
```bash
56+
$ type run
57+
```
58+
5259
## Test it
5360

5461
- Go to http://localhost:5102

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ services:
8989

9090
networks:
9191
coolstore:
92-
name: coolstore-network
92+
name: coolstore-network

practical-dapr.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoolStore.InventoryApi", "s
4040
EndProject
4141
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "N8T.Infrastructure", "src\BuildingBlocks\N8T.Infrastructure\N8T.Infrastructure.csproj", "{5E3CD528-6C2E-4D38-B4A3-C5646853BD88}"
4242
EndProject
43+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoolStore.ShoppingCartApi", "src\ShoppingCart\CoolStore.ShoppingCartApi\CoolStore.ShoppingCartApi.csproj", "{417E233D-4A16-4EA5-A221-A3B879F03E58}"
44+
EndProject
45+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Order", "Order", "{BCEFF6E3-BD86-4268-B0FF-3797F56EB803}"
46+
EndProject
47+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoolStore.OrderApi", "src\Order\CoolStore.OrderApi\CoolStore.OrderApi.csproj", "{1A3DCA3D-6E35-43F8-BD1F-164C9719AC80}"
48+
EndProject
49+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{49AD1C33-D5BC-45C7-B78C-75D11AD86B05}"
50+
ProjectSection(SolutionItems) = preProject
51+
.editorconfig = .editorconfig
52+
EndProjectSection
53+
EndProject
4354
Global
4455
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4556
Debug|Any CPU = Debug|Any CPU
@@ -70,6 +81,14 @@ Global
7081
{5E3CD528-6C2E-4D38-B4A3-C5646853BD88}.Debug|Any CPU.Build.0 = Debug|Any CPU
7182
{5E3CD528-6C2E-4D38-B4A3-C5646853BD88}.Release|Any CPU.ActiveCfg = Release|Any CPU
7283
{5E3CD528-6C2E-4D38-B4A3-C5646853BD88}.Release|Any CPU.Build.0 = Release|Any CPU
84+
{417E233D-4A16-4EA5-A221-A3B879F03E58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
85+
{417E233D-4A16-4EA5-A221-A3B879F03E58}.Debug|Any CPU.Build.0 = Debug|Any CPU
86+
{417E233D-4A16-4EA5-A221-A3B879F03E58}.Release|Any CPU.ActiveCfg = Release|Any CPU
87+
{417E233D-4A16-4EA5-A221-A3B879F03E58}.Release|Any CPU.Build.0 = Release|Any CPU
88+
{1A3DCA3D-6E35-43F8-BD1F-164C9719AC80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
89+
{1A3DCA3D-6E35-43F8-BD1F-164C9719AC80}.Debug|Any CPU.Build.0 = Debug|Any CPU
90+
{1A3DCA3D-6E35-43F8-BD1F-164C9719AC80}.Release|Any CPU.ActiveCfg = Release|Any CPU
91+
{1A3DCA3D-6E35-43F8-BD1F-164C9719AC80}.Release|Any CPU.Build.0 = Release|Any CPU
7392
EndGlobalSection
7493
GlobalSection(SolutionProperties) = preSolution
7594
HideSolutionNode = FALSE
@@ -87,6 +106,9 @@ Global
87106
{D8D8FE55-07AF-49DC-8437-1F794D629187} = {6ED8DE8E-DD79-459E-B83D-A92BEEF82B76}
88107
{00B9F3A1-02A7-4AC3-BB45-76DE05F9C095} = {36019538-F352-459E-A646-115428621E8E}
89108
{5E3CD528-6C2E-4D38-B4A3-C5646853BD88} = {527B152F-BC11-42AE-89CE-E5B26C580F7F}
109+
{417E233D-4A16-4EA5-A221-A3B879F03E58} = {9814F542-3D82-41B7-BE56-943A5E69278A}
110+
{BCEFF6E3-BD86-4268-B0FF-3797F56EB803} = {A4A723DA-686E-4AF3-8CF8-709E1E75C99C}
111+
{1A3DCA3D-6E35-43F8-BD1F-164C9719AC80} = {BCEFF6E3-BD86-4268-B0FF-3797F56EB803}
90112
EndGlobalSection
91113
GlobalSection(ExtensibilityGlobals) = postSolution
92114
SolutionGuid = {765FF0DA-E88E-4254-9C2B-434D0AFF69C0}

practical-dapr.sln.DotSettings

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2-
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=QL/@EntryIndexedValue">QL</s:String>
3-
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=dapr/@EntryIndexedValue">True</s:Boolean>
43
<s:Boolean x:Key="/Default/UserDictionary/Words/=Grpc/@EntryIndexedValue">True</s:Boolean>
5-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mediat/@EntryIndexedValue">True</s:Boolean>
6-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Migrator/@EntryIndexedValue">True</s:Boolean>
7-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Protobuf/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
4+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Migrator/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Text.Json;
3+
using Dapr.Client;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace N8T.Infrastructure.Dapr
8+
{
9+
public static class Extensions
10+
{
11+
public static DaprClient GetDaprClient(
12+
this IConfiguration config,
13+
string appId,
14+
bool isHttps = false,
15+
ILogger logger = null)
16+
{
17+
var url = GetDaprClientUrl(config, appId, isHttps);
18+
logger?.LogInformation($"Dapr Client Url: {url}");
19+
20+
var options = new JsonSerializerOptions
21+
{
22+
PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase
23+
};
24+
25+
var client = new DaprClientBuilder()
26+
.UseEndpoint(url)
27+
.UseJsonSerializationOptions(options)
28+
.Build();
29+
30+
return client;
31+
}
32+
33+
public static string GetDaprClientUrl(this IConfiguration config, string appId, bool isHttps = false)
34+
{
35+
string host, port, url;
36+
if (!isHttps)
37+
{
38+
host = config[$"service:{appId}:host"];
39+
port = config[$"service:{appId}:port"];
40+
url = $"http://{host}:{port}";
41+
}
42+
else
43+
{
44+
var protocol = Environment.GetEnvironmentVariable($"{appId.ToUpper()}_HTTPS_SERVICE_PROTOCOL");
45+
port = Environment.GetEnvironmentVariable($"{appId.ToUpper()}_HTTPS_SERVICE_PORT");
46+
host = config[$"service:{appId}:host"];
47+
url = $"{protocol}://{host}:{port}";
48+
}
49+
50+
return url;
51+
}
52+
}
53+
}

src/BuildingBlocks/N8T.Infrastructure/Data/DbContextMigratorHostedService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.EntityFrameworkCore;
1+
using Microsoft.EntityFrameworkCore;
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Hosting;
44
using System;
@@ -23,4 +23,4 @@ public async Task StartAsync(CancellationToken cancellationToken)
2323

2424
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
2525
}
26-
}
26+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Reflection;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using N8T.Domain;
7+
8+
namespace N8T.Infrastructure.Data
9+
{
10+
public static class Extensions
11+
{
12+
public static IServiceCollection AddCustomDbContext<TDbContext>(this IServiceCollection services, Assembly anchorAssembly, string connString)
13+
where TDbContext : DbContext, IDbFacadeResolver, IDomainEventContext
14+
{
15+
services
16+
.AddDbContext<TDbContext>(options =>
17+
{
18+
options.UseSqlServer(connString, sqlOptions =>
19+
{
20+
sqlOptions.MigrationsAssembly(anchorAssembly.GetName().Name);
21+
sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, TimeSpan.FromSeconds(30), null);
22+
});
23+
});
24+
25+
services.AddScoped<IDbFacadeResolver>(provider => provider.GetService<TDbContext>());
26+
services.AddScoped<IDomainEventContext>(provider => provider.GetService<TDbContext>());
27+
services.AddHostedService<DbContextMigratorHostedService>();
28+
29+
return services;
30+
}
31+
32+
public static IServiceCollection AddCustomDbContext<TDbContext>(this IServiceCollection services, Assembly anchorAssembly, IConfiguration config)
33+
where TDbContext : DbContext, IDbFacadeResolver, IDomainEventContext
34+
{
35+
services
36+
.AddDbContext<TDbContext>(options =>
37+
{
38+
options.UseSqlServer(config.GetConnectionString("MainDb"), sqlOptions =>
39+
{
40+
sqlOptions.MigrationsAssembly(anchorAssembly.GetName().Name);
41+
sqlOptions.EnableRetryOnFailure(maxRetryCount: 3);
42+
});
43+
});
44+
45+
services.AddScoped<IDbFacadeResolver>(provider => provider.GetService<TDbContext>());
46+
services.AddScoped<IDomainEventContext>(provider => provider.GetService<TDbContext>());
47+
services.AddHostedService<DbContextMigratorHostedService>();
48+
49+
return services;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)