Skip to content

Commit fc7177c

Browse files
committed
#15 add protobuf for the message contract and refactor code
1 parent ca0e326 commit fc7177c

Some content is hidden

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

46 files changed

+624
-347
lines changed

build.cake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var libs = new List<string>{
1919
"./src/NetCoreKit.Infrastructure.EfCore.SqlServer/NetCoreKit.Infrastructure.EfCore.SqlServer.csproj",
2020
"./src/NetCoreKit.Infrastructure.EfCore.MySql/NetCoreKit.Infrastructure.EfCore.MySql.csproj",
2121
"./src/NetCoreKit.Infrastructure.Bus/NetCoreKit.Infrastructure.Bus.csproj",
22+
"./src/NetCoreKit.Infrastructure.Bus.InProcessBus/NetCoreKit.Infrastructure.Bus.InProcessBus.csproj",
2223
"./src/NetCoreKit.Infrastructure.Bus.Kafka/NetCoreKit.Infrastructure.Bus.Kafka.csproj"
2324
};
2425

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<ProjectReference Include="..\..\src\NetCoreKit.Domain\NetCoreKit.Domain.csproj" />
11+
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
1212
</ItemGroup>
1313

1414
</Project>

samples/Contracts/Project.cs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// <auto-generated>
2+
// Generated by the protocol buffer compiler. DO NOT EDIT!
3+
// source: protos/project.proto
4+
// </auto-generated>
5+
#pragma warning disable 1591, 0612, 3021
6+
#region Designer generated code
7+
8+
using pb = global::Google.Protobuf;
9+
using pbc = global::Google.Protobuf.Collections;
10+
using pbr = global::Google.Protobuf.Reflection;
11+
using scg = global::System.Collections.Generic;
12+
namespace Project.Proto {
13+
14+
/// <summary>Holder for reflection information generated from protos/project.proto</summary>
15+
public static partial class ProjectReflection {
16+
17+
#region Descriptor
18+
/// <summary>File descriptor for protos/project.proto</summary>
19+
public static pbr::FileDescriptor Descriptor {
20+
get { return descriptor; }
21+
}
22+
private static pbr::FileDescriptor descriptor;
23+
24+
static ProjectReflection() {
25+
byte[] descriptorData = global::System.Convert.FromBase64String(
26+
string.Concat(
27+
"ChRwcm90b3MvcHJvamVjdC5wcm90bxINcHJvamVjdC5wcm90byIhChFQcm9q",
28+
"ZWN0Q3JlYXRlZE1zZxIMCgROYW1lGAEgASgJYgZwcm90bzM="));
29+
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
30+
new pbr::FileDescriptor[] { },
31+
new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] {
32+
new pbr::GeneratedClrTypeInfo(typeof(global::Project.Proto.ProjectCreatedMsg), global::Project.Proto.ProjectCreatedMsg.Parser, new[]{ "Name" }, null, null, null)
33+
}));
34+
}
35+
#endregion
36+
37+
}
38+
#region Messages
39+
public sealed partial class ProjectCreatedMsg : pb::IMessage<ProjectCreatedMsg> {
40+
private static readonly pb::MessageParser<ProjectCreatedMsg> _parser = new pb::MessageParser<ProjectCreatedMsg>(() => new ProjectCreatedMsg());
41+
private pb::UnknownFieldSet _unknownFields;
42+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
43+
public static pb::MessageParser<ProjectCreatedMsg> Parser { get { return _parser; } }
44+
45+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
46+
public static pbr::MessageDescriptor Descriptor {
47+
get { return global::Project.Proto.ProjectReflection.Descriptor.MessageTypes[0]; }
48+
}
49+
50+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
51+
pbr::MessageDescriptor pb::IMessage.Descriptor {
52+
get { return Descriptor; }
53+
}
54+
55+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
56+
public ProjectCreatedMsg() {
57+
OnConstruction();
58+
}
59+
60+
partial void OnConstruction();
61+
62+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
63+
public ProjectCreatedMsg(ProjectCreatedMsg other) : this() {
64+
name_ = other.name_;
65+
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
66+
}
67+
68+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
69+
public ProjectCreatedMsg Clone() {
70+
return new ProjectCreatedMsg(this);
71+
}
72+
73+
/// <summary>Field number for the "Name" field.</summary>
74+
public const int NameFieldNumber = 1;
75+
private string name_ = "";
76+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
77+
public string Name {
78+
get { return name_; }
79+
set {
80+
name_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
81+
}
82+
}
83+
84+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
85+
public override bool Equals(object other) {
86+
return Equals(other as ProjectCreatedMsg);
87+
}
88+
89+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
90+
public bool Equals(ProjectCreatedMsg other) {
91+
if (ReferenceEquals(other, null)) {
92+
return false;
93+
}
94+
if (ReferenceEquals(other, this)) {
95+
return true;
96+
}
97+
if (Name != other.Name) return false;
98+
return Equals(_unknownFields, other._unknownFields);
99+
}
100+
101+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
102+
public override int GetHashCode() {
103+
int hash = 1;
104+
if (Name.Length != 0) hash ^= Name.GetHashCode();
105+
if (_unknownFields != null) {
106+
hash ^= _unknownFields.GetHashCode();
107+
}
108+
return hash;
109+
}
110+
111+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
112+
public override string ToString() {
113+
return pb::JsonFormatter.ToDiagnosticString(this);
114+
}
115+
116+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
117+
public void WriteTo(pb::CodedOutputStream output) {
118+
if (Name.Length != 0) {
119+
output.WriteRawTag(10);
120+
output.WriteString(Name);
121+
}
122+
if (_unknownFields != null) {
123+
_unknownFields.WriteTo(output);
124+
}
125+
}
126+
127+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
128+
public int CalculateSize() {
129+
int size = 0;
130+
if (Name.Length != 0) {
131+
size += 1 + pb::CodedOutputStream.ComputeStringSize(Name);
132+
}
133+
if (_unknownFields != null) {
134+
size += _unknownFields.CalculateSize();
135+
}
136+
return size;
137+
}
138+
139+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
140+
public void MergeFrom(ProjectCreatedMsg other) {
141+
if (other == null) {
142+
return;
143+
}
144+
if (other.Name.Length != 0) {
145+
Name = other.Name;
146+
}
147+
_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
148+
}
149+
150+
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
151+
public void MergeFrom(pb::CodedInputStream input) {
152+
uint tag;
153+
while ((tag = input.ReadTag()) != 0) {
154+
switch(tag) {
155+
default:
156+
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
157+
break;
158+
case 10: {
159+
Name = input.ReadString();
160+
break;
161+
}
162+
}
163+
}
164+
}
165+
166+
}
167+
168+
#endregion
169+
170+
}
171+
172+
#endregion Designer generated code
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package project.proto;
4+
5+
message ProjectCreatedMsg {
6+
string Name = 1;
7+
}

samples/Notifier/EventsHostedService.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
4+
using AutoMapper;
45
using MediatR;
56
using Microsoft.Extensions.Hosting;
67
using Microsoft.Extensions.Logging;
7-
using NetCoreKit.Domain;
8-
using NetCoreKit.Samples.Contracts.Events;
8+
using NetCoreKit.Infrastructure.Bus.Kafka;
9+
using NetCoreKit.Infrastructure.Mappers;
10+
using Project.Proto;
911

1012
namespace NetCoreKit.Samples.Notifier
1113
{
1214
internal class EventsHostedService : IHostedService
1315
{
1416
private readonly ILogger _logger;
1517
private readonly IApplicationLifetime _appLifetime;
16-
private readonly IEventBus _eventBus;
18+
private readonly IDispatchedEventBus _eventBus;
1719

1820
public EventsHostedService(
1921
ILogger<EventsHostedService> logger,
2022
IApplicationLifetime appLifetime,
21-
IEventBus eventBus)
23+
IDispatchedEventBus eventBus)
2224
{
2325
_logger = logger;
2426
_appLifetime = appLifetime;
@@ -42,7 +44,7 @@ public Task StopAsync(CancellationToken cancellationToken)
4244
private void OnStarted()
4345
{
4446
_logger.LogInformation("OnStarted has been called.");
45-
_eventBus.Subscribe("project").Wait();
47+
_eventBus.Subscribe<ProjectCreatedMsg>("project").Wait();
4648
}
4749

4850
private void OnStopping()
@@ -59,7 +61,23 @@ private void OnStopped()
5961
}
6062
}
6163

62-
/*public class ProjectCreatedSubscriber : INotificationHandler<ProjectCreated>
64+
public class ProjectProfile : Profile
65+
{
66+
public ProjectProfile()
67+
{
68+
this.MapToNotification<ProjectCreatedMsg, ProjectCreated>();
69+
//this.MapToNotification<TaskCreated, Notifications.TaskCreated>();
70+
}
71+
}
72+
73+
public class ProjectCreated : INotification
74+
{
75+
public string Name { get; set; }
76+
public DateTime OccurredOn { get; set; }
77+
public int EventVersion { get; set; }
78+
}
79+
80+
public class ProjectCreatedSubscriber : INotificationHandler<ProjectCreated>
6381
{
6482
private readonly ILogger _logger;
6583

@@ -73,5 +91,5 @@ public Task Handle(ProjectCreated @event, CancellationToken cancellationToken)
7391
_logger.LogInformation($"@ Project Created Event -{@event.OccurredOn}-{@event.EventVersion}. Now I will do something cool in this worker...");
7492
return Task.FromResult(@event);
7593
}
76-
}*/
94+
}
7795
}

samples/Notifier/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public static async Task Main(string[] args)
3333
})
3434
.ConfigureServices((hostContext, services) =>
3535
{
36+
Mapper.Initialize(cfg => cfg.AddProfiles(typeof(Program)));
3637
services.AddMediatR();
3738
services.AddKafkaEventBus();
38-
Mapper.Initialize(cfg => cfg.AddProfiles(typeof(Program)));
3939

4040
var config = services.BuildServiceProvider().GetService<IConfiguration>();
4141

samples/SignalRNotifier/Services/Hubs/ProjectHub.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using System.Threading.Tasks;
33
using MediatR;
44
using Microsoft.AspNetCore.SignalR;
5-
using NetCoreKit.Domain;
5+
using NetCoreKit.Infrastructure.Bus.Kafka;
6+
using Project.Proto;
67

78
namespace NetCoreKit.Samples.SignalRNotifier.Services.Hubs
89
{
@@ -14,9 +15,9 @@ public class ProjectHostService : HostedService,
1415
INotificationHandler<Notifications.ProjectCreated>,
1516
INotificationHandler<Notifications.TaskCreated>
1617
{
17-
private readonly IEventBus _eventBus;
18+
private readonly IDispatchedEventBus _eventBus;
1819

19-
public ProjectHostService(IHubContext<ProjectHub> context, IEventBus eventBus)
20+
public ProjectHostService(IHubContext<ProjectHub> context, IDispatchedEventBus eventBus)
2021
{
2122
_eventBus = eventBus;
2223
Clients = context.Clients;
@@ -36,7 +37,7 @@ public async Task Handle(Notifications.TaskCreated notification, CancellationTok
3637

3738
protected override Task ExecuteAsync(CancellationToken cancellationToken)
3839
{
39-
_eventBus.Subscribe("project").Wait(cancellationToken);
40+
_eventBus.Subscribe<ProjectCreatedMsg>("project").Wait(cancellationToken);
4041
return Task.CompletedTask;
4142
}
4243
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using AutoMapper;
22
using NetCoreKit.Infrastructure.Mappers;
3-
using NetCoreKit.Samples.Contracts.Events;
3+
using Project.Proto;
44

55
namespace NetCoreKit.Samples.SignalRNotifier.Services.Profiles
66
{
77
public class ProjectProfile : Profile
88
{
99
public ProjectProfile()
1010
{
11-
this.MapToNotification<ProjectCreated, Notifications.ProjectCreated>();
12-
this.MapToNotification<TaskCreated, Notifications.TaskCreated>();
11+
this.MapToNotification<ProjectCreatedMsg, Notifications.ProjectCreated>();
12+
//this.MapToNotification<TaskCreated, Notifications.TaskCreated>();
1313
}
1414
}
1515
}

samples/SignalRNotifier/Startup.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Hosting;
9+
using NetCoreKit.Infrastructure.Bus;
910
using NetCoreKit.Infrastructure.Bus.Kafka;
10-
using NetCoreKit.Samples.Contracts.Events;
1111
using NetCoreKit.Samples.SignalRNotifier.Services.Hubs;
1212
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
1313

@@ -19,8 +19,8 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
1919
{
2020
services.AddMediatR(
2121
typeof(Startup),
22-
typeof(EventBus),
23-
typeof(ProjectCreated));
22+
typeof(DomainDomainEventBus)
23+
/*typeof(ProjectCreated)*/);
2424

2525
Mapper.Initialize(cfg => cfg.AddProfiles(typeof(Startup)));
2626

samples/TodoApi/Domain/Project.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using NetCoreKit.Domain;
5-
using NetCoreKit.Samples.Contracts.Events;
65

76
namespace NetCoreKit.Samples.TodoAPI.Domain
87
{

0 commit comments

Comments
 (0)