Any one know of a less repetitive way to write to an event outbox using Avro objects for Kafka?

public async Task<int> CreateResource(int resourceName, DateTime timestamp, IDbTransaction transaction) {
string sql = //some create sql for some table dbo.Resource
var parametrs = new {...};
int someRetVal await connection.ExecuteAsync(sql, parameters, transaction);
//create the event by fetching from db
string eventSql = //some sql that fetches a "Resource" event which is most/all columns from Resource Table along with some other metadata for the event. Sometimes resource may have a field that needs to use link tables to join and reach the data for that field. This "ResourceEvent" is our Avro object, though it gets serialized into a generic JSON object into the Eventlogging table.
ResourceEvent event = await connection.QuerySingleAsync(eventsql, new {timestamp}, transaction);

// somet events have nested event objects because of some hierarchal structure, they have less fields, but still need to fetched with sql
FetchedSubEvent subEvent = //some FetchMethod
event.SubEvent.Add(subEvent);
await logger.WriteToOutbox(event);

return someRetVal
}
public async Task<int> CreateResource(int resourceName, DateTime timestamp, IDbTransaction transaction) {
string sql = //some create sql for some table dbo.Resource
var parametrs = new {...};
int someRetVal await connection.ExecuteAsync(sql, parameters, transaction);
//create the event by fetching from db
string eventSql = //some sql that fetches a "Resource" event which is most/all columns from Resource Table along with some other metadata for the event. Sometimes resource may have a field that needs to use link tables to join and reach the data for that field. This "ResourceEvent" is our Avro object, though it gets serialized into a generic JSON object into the Eventlogging table.
ResourceEvent event = await connection.QuerySingleAsync(eventsql, new {timestamp}, transaction);

// somet events have nested event objects because of some hierarchal structure, they have less fields, but still need to fetched with sql
FetchedSubEvent subEvent = //some FetchMethod
event.SubEvent.Add(subEvent);
await logger.WriteToOutbox(event);

return someRetVal
}
Basically gets repetitive when there is varying event types, with varying sub events. We don't use Entity Framework queries, which to me seems like would be a lot faster coding wise to build these event objects. We use dapper so most of the time, I'm writing out the queries. But in ways the queries are all very similar except, different tables and columns and joins etc. But they are just searching for data. Originally I had a logger and a fetcher class that just had all the fetch methods, but having a chance to rewrite I didn't see any benefit in that because all the code that accesses those methods are in different data base access classes. So the fetching class would only ever have a subset of its methods called because UserCRUDMethods may never need to call unrelated FetchCreateApplesEvent. So I'm keeping the code close to the crud database access classes. Its still feels repetitive. I think maybe SQL generation but I think that would also be a lot too.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?