2

Hi I am trying to implement Generics in C# interfaces. I have one method which should take different models as parameters after implementing Generics. Below is my Interface.

 public interface IKafkaProducer<T>
  {
    Task ProduceAsync(T kafkaEvent, string topicName);
  }

This Kafka event may should be different models after implementing Generics. For example It should be able to take Employee or User class etc. Below is my class implementation.

public class KafkaProducer<T> : IKafkaProducer<T>
  {
   public async Task ProduceAsync(T kafkaEvent, string topicName)
    {
      using (var schemaRegistry = new CachedSchemaRegistryClient(this.kafkaSchemaRegistryConfig.GetConfig()))
      using (var producer =
               new ProducerBuilder<string, ProductRangeEvent>(this.producerConfigFactory.ProducerConfig())
                   .SetKeySerializer(new AvroSerializer<string>(schemaRegistry))
                   .SetValueSerializer(new AvroSerializer<ProductRangeEvent>(schemaRegistry))
                   .Build())
      {
        Console.WriteLine($"{producer.Name} producing on {topicName}. Enter user names, q to exit.");
        await producer
              .ProduceAsync(topicName, new Message<string, ProductRangeEvent> { Key = null, Value = kafkaEvent })
              .ContinueWith(task => task.IsFaulted
                  ? $"error producing message: {task.Exception.Message}"
                  : $"produced to: {task.Result.TopicPartitionOffset}");
      }
    }
  }

In the above code, ProduceAsync(topicName, new Message<string, ProductRangeEvent> { Key = null, Value = kafkaEvent }) value = kafkaEvent is giving error. This kafkaEvent is of type ProductRangeEvent. It is giving me error cannot implicitly convert type T to ProductRangeEvent. I am calling above method as

public class TestController
  {
    private readonly IKafkaProducer kafkaProducer;

    public TestController(IKafkaProducer kafkaProducer)
    {
      this.kafkaProducer = kafkaProducer;
    }
     [HttpGet]
        [Route("/api/test")]
        public IActionResult Test()
        {
          ProductRangeEvent productRangeEvent = new ProductRangeEvent
          {
            id = "101"
          };
          var response = this.kafkaProducer.ProduceAsync(productRangeEvent, "users");
          response.Wait();
          var hi = response.IsCompletedSuccessfully;
          return null;
        }
      }

In the above code private readonly IKafkaProducer kafkaProducer; is also giving me error Using the generic type IKafkaProducer requires one argument. Can someone help me to fix this issue? Any help would be appreciated. Thanks

15
  • 1
    Maybe you need a constraint where T : ProductRangeEvent if you want your generic type to match. Otherwise you need to change the code to be more generic in the use of the generic type. As for the other error you have to specify the generic type IKafkaProducer<ProductRangeEvent> Commented Jun 27, 2019 at 3:31
  • Thanks where T : ProductRangeEvent where should I add? interface or class? Commented Jun 27, 2019 at 3:33
  • @Niranjan on both Commented Jun 27, 2019 at 3:34
  • I added and error gone in class implementation. But error remains in my controller class. Commented Jun 27, 2019 at 3:35
  • @Niranjan refresh page. juharr edited his comment to solve second error Commented Jun 27, 2019 at 3:36

1 Answer 1

2

Change this line:

private readonly IKafkaProducer kafkaProducer;

To

private readonly IKafkaProducer<ProductRangeEvent> kafkaProducer;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks If we want more than one type we need to send in same class then can we make like this IKafkaProducer<ProductRangeEvent, LsplTimephaseEvent>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.