Skip to content

Commit

Permalink
Added template for events
Browse files Browse the repository at this point in the history
  • Loading branch information
masesdevelopers committed Oct 20, 2023
1 parent 68ea906 commit a10051d
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "MASES s.r.l.",
"classifications": [ "Common", "Library" ],
"identity": "MASES.EntityFrameworkCore.KNet.Templates.AppWithEvents",
"name": "Executable template: Simple Console with events for EntityFrameworkCore provider for Apache Kafka project",
"shortName": "kefcoreAppWithEvents",
"tags": {
"language": "C#",
"type": "project"
}
}
124 changes: 124 additions & 0 deletions src/net/templates/templates/kefcoreAppWithEvents/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using MASES.EntityFrameworkCore.KNet.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MASES.EntityFrameworkCore.KNet.Templates
{
partial class Program
{
static void OnEvent(IEntityType entity, bool state, object key)
{
Console.WriteLine($"Entity {entity.Name} has {(state ? "removed" : "added/updated")} the key {key}");
}

static void Main(string[] args)
{
BloggingContext context = null;
try
{
context = new BloggingContext()
{
BootstrapServers = "KAFKA-BROKER:9092",
ApplicationId = "MyApplicationId",
DbName = "MyDB",
OnChangeEvent = OnEvent
};
// cleanup topics on Broker
context.Database.EnsureDeleted();
context.Database.EnsureCreated();

// prefill data
for (int i = 0; i < 1000; i++)
{
context.Add(new Blog
{
Url = "http://blogs.msdn.com/adonet" + i.ToString(),
Posts = new List<Post>()
{
new Post()
{
Title = "title",
Content = i.ToString()
}
},
Rating = i,
});
}
// save data
context.SaveChanges();

// make some queries
var selector = (from op in context.Blogs
join pg in context.Posts on op.BlogId equals pg.BlogId
where pg.BlogId == op.BlogId
select new { pg, op });
var pageObject = selector.FirstOrDefault();

var post = context.Posts.Single(b => b.BlogId == 2);

post = context.Posts.Single(b => b.BlogId == 1);

var all = context.Posts.All((o) => true);

var value = context.Blogs.AsQueryable().ToQueryString();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
context?.Dispose();
}
}
}

public class BloggingContext : KafkaDbContext
{
// uncomment for persistent storage
// public override bool UsePersistentStorage { get; set; } = true;

// uncomment to disable compacted replicator
//public override bool UseCompactedReplicator { get; set; } = false;

public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }

/// uncomment for model builder
//protected override void OnModelCreating(ModelBuilder modelBuilder)
//{
// modelBuilder.Entity<Blog>().HasKey(c => new { c.BlogId, c.Rating });
//}
}

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
public List<Post> Posts { get; set; }

public override string ToString()
{
return $"BlogId: {BlogId} Url: {Url} Rating: {Rating}";
}
}

public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }

public int BlogId { get; set; }
public Blog Blog { get; set; }

public override string ToString()
{
return $"PostId: {PostId} Title: {Title} Content: {Content} BlogId: {BlogId}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>latest</LangVersion>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="Exists('..\..\..\KEFCore\KEFCore.csproj')">
<!--Within GitHub repo: used for test purpose-->
<ProjectReference Include="..\..\..\KEFCore\KEFCore.csproj" />
</ItemGroup>
<ItemGroup Condition="!Exists('..\..\..\KEFCore\KEFCore.csproj')">
<!--Outside GitHub repo-->
<PackageReference Include="MASES.EntityFrameworkCore.KNet" Version="0.10.1" IncludeAssets="All" PrivateAssets="None" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions test/KEFCore.Test.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Templates", "Templates", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "kefcoreApp", "..\src\net\templates\templates\kefcoreApp\kefcoreApp.csproj", "{ECB20F1B-A86D-4CB1-953B-B7C42E9FC1CD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "kefcoreAppWithEvents", "..\src\net\templates\templates\kefcoreAppWithEvents\kefcoreAppWithEvents.csproj", "{EEF483DF-8DDB-474D-8135-2762014F9438}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -75,6 +77,10 @@ Global
{ECB20F1B-A86D-4CB1-953B-B7C42E9FC1CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ECB20F1B-A86D-4CB1-953B-B7C42E9FC1CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ECB20F1B-A86D-4CB1-953B-B7C42E9FC1CD}.Release|Any CPU.Build.0 = Release|Any CPU
{EEF483DF-8DDB-474D-8135-2762014F9438}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEF483DF-8DDB-474D-8135-2762014F9438}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEF483DF-8DDB-474D-8135-2762014F9438}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEF483DF-8DDB-474D-8135-2762014F9438}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -90,6 +96,7 @@ Global
{BED3DF6D-B60E-486D-96E9-AD069D0600D2} = {B35B16BB-890F-4385-AB20-7AA4DD6E9C01}
{E1BE56EB-4701-47E7-876A-D2146619C211} = {4A0AD520-9BC4-4F92-893B-6F92BBC35BFA}
{ECB20F1B-A86D-4CB1-953B-B7C42E9FC1CD} = {68A75218-B566-4DF6-8BFF-586B7C3E7ED6}
{EEF483DF-8DDB-474D-8135-2762014F9438} = {68A75218-B566-4DF6-8BFF-586B7C3E7ED6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {36C294ED-9ECE-42AA-8273-31E008749AF3}
Expand Down

0 comments on commit a10051d

Please sign in to comment.