Open Closed

How to publish message to kafka when we Add/Edit the SQL table record through stored procedure ? #5320


User avatar
0
abhichan26 created

Ours is a tiered application with Blazor Web Assembly as a UI framework with Entity Framework and SQL Server. • ABP Framework version: v5.3 • UI type: / Blazor Web Assembly • DB provider: EF Core • Tiered (MVC) : Yes • Identity Server Separated : yes

We are publishing message to Kafka using AbpEventBusKafkaModule and we are successfully able to publish message for pre-defined event types EntityCreatedEto<T> is published when an entity of type T was created. EntityUpdatedEto<T> is published when an entity of type T was updated. EntityDeletedEto<T> is published when an entity of type T was deleted.

But there are few places where we are using Stored procedure instead of _myRepository.AddAsync(myDto) or _myRepository.UpdateAsync(myDto), we are making a call to stored procedure as shown below. When we are adding or updating a record through stored procedure , messages are not written to Kafka topic.

public async Task<int> AddXYZ(ABCDto myData, CancellationToken cancellationToken = default) { try { await EnsureConnectionOpenAsync(cancellationToken); int generatedId = 0;

            //Creating new output parameter to strore generated Id when record is created
            var myReturnId= new SqlParameter("@GeneratedId", SqlDbType.Int)
            {
                Direction = ParameterDirection.Output
            };
            using (var command = CreateCommand("AddCustomDetails", CommandType.StoredProcedure, new SqlParameter[] {
                                        new SqlParameter("@Code", myData.Code),
                                        new SqlParameter("@Description",  myData.Description),
                                        myReturnId
        }))
            {
                await command.ExecuteNonQueryAsync(cancellationToken);
                generatedId = (int)myReturnId.Value;
            }
                            return generatedId;//
        catch (System.Exception)
        {
            throw;
        }
    }

Questions: 1.Can you please help how to publish messages when we are updating the records through stored procedure?

2.One possible approach we can think of is publishing explicit messages as shown below in case of adding or updating the records through stored procedure but how do we append EventName with .Created or .Updated in the ETO? await _distributedEventBus.PublishAsync( new ABCEto { //map the properties from DTO to ETO }

3.Please suggest if explicit publishing as mentioned in point 2 is the recommended approach or suggest any better option.?


3 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    ABP predefined events based on EFCore entity tracking. there will be no entity tracking when you use stored procedures, so you can't automatically publish events.

    So you need to publish the event manually

    but how do we append EventName with .Created or .Updated in the ETO

    You can consider using predefined events:

    await _distributedEventBus.PublishAsync(
        new EntityCreatedEto<XXX>
        {
        }
    
  • User Avatar
    0
    abhichan26 created

    Hi Can we do new on EntityCreatedEto<T> , as below code gives following error?

    public virtual async Task PublishAddAssortmentAsync(ABCDto myData)
            {
                  await _distributedEventBus.PublishAsync(
                    new EntityCreatedEto<ABCEto>
                    {
                        Code = myData.Code,
                        Description = myData.Description,
                        
                    }
                );
            }
    

    Error CS7036 There is no argument given that corresponds to the required parameter 'entity' of 'EntityCreatedEto<ABCEto>.EntityCreatedEto(ABCEto)

    Can you please suggest how to fix it?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Error CS7036 There is no argument given that corresponds to the required parameter 'entity' of 'EntityCreatedEto<ABCEto>.EntityCreatedEto(ABCEto)

    For example:

    new EntityCreatedEto<UserEto>(new UserEto
    {
        .....
    });
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 26, 2024, 06:07