How to inject dependency into a dynamically created class?

Asked

Viewed 77 times

0

I’m doing a gateway to a client project consuming Microservices. In this gateway, the way the client will consume the application is through an interface. The implementation of this interface will be created dynamically (At compile time). So I need to know how to inject the dynamically generated class dependency. Example:

[MicroServiceHost("MsProduct")]
public interface IProductService
{

    [MicroService("GetAllProducts")]
    IRestResponse GetAllProducts(List<KeyValuePair<object, object>> parameters = null);


    [MicroService("AddProduct")]
    IRestResponse AddProduct(List<KeyValuePair<object, object>> parameters = null);
}

public class MsProductController : Controller
{
    IProductService _productService;

    public MsProductController(IProductService productService)
    {
        _productService = productService;
    }

    public ActionResult GetAll()
    {
        var request = _productService.GetAllProducts();
        var r = request.Content;

        return View();
    }
}
  • Please, either Ask this Question on SO-EN or Translate this Question to English. tyvm

  • Your question is very wide, it would be nice to put the code of this class... I recommend reading: https://www.devmedia.com.br/introducao-a-injecao-de-dependencia-no-asp-net-mvc/27099

  • I don’t understand, do you want to implement dependency injection in a project of yours? Is that it? I also think the question has nothing to do with "Microservices"

  • 2

    @Guilhermenascimento I think he wants to "inject the dependency" into an instance (object) created via Reflection. Confer, Guilhermeramos?

  • Whereas is this hypothesis, will you load the object from the same dll? Or will you load the dll first?

  • @LINQ was what I imagined, only that the Microservices tag simply made no sense and not even if it had to do with the "early" part: it creates a "dynamic class".

  • Sorry for the way of the question. Adjusting. I am making a gateway for a customer to consume Microservices. In this gateway, the way the client will consume the application is through an interface. The implementation of this interface will be created dynamically (At compile time). So I need to know how to inject the dynamically generated class dependency.

  • 1

    The MEF can’t help you in this case?

  • Thanks @Gabrielheming , worked out. I will write how I solved and will assign you the credits !

  • @Guilhermeramos Excellent! I will be waiting for the implementation.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.