Azure-functions-durable-extension: Enable DI support for class-based entities

Created on 12 Jul 2019  路  2Comments  路  Source: Azure/azure-functions-durable-extension

Description

Would love to see class based Durable Entities work with the built-in dependency injection support for Functions. Looks like it was mentioned in #763, I don鈥檛 think the feature was enabled.

Example

I鈥檓 guessing the programming model would look similar to how DI works with non-Durable Functions today.

public class Counter
{
    [JsonProperty("value")]
    public int CurrentValue { get; set; }
    public int Get() => this.CurrentValue;

    private ILogger _logger;
    private IMyService _service;

    public Counter(ILogger logger, IMyService service) {
         _logger = logger; 
         _service = service; 
   }
    [FunctionName(nameof(Counter))]
    public static Task Run([EntityTrigger] IDurableEntityContext ctx)
        => ctx.DispatchAsync<Counter>();
}
enhancement

Most helpful comment

This has been merged and will be part of the v2.0.0-beta2 release.

All 2 comments

@ConnorMcMahon I think this would be a good one for you to investigate. I think it's going to be really important to enable something like this in order for Durable Entities to become viable for production applications. There might be two angles to consider:

  1. Could we accomplish what Cecil mentioned above? Basically, allowing users to declare what interfaces they want in the entity class constructor.
  2. (Possibly related) Could users call ctx.DispatchAsync<ICounter>() and then get the implementation of ICounter from DI? I haven't figured out exactly what scenarios this would enable, so it might be worth considering possible use-cases first.

Fabio might be able to provide some guidance on how we can achieve this, if necessary.

This has been merged and will be part of the v2.0.0-beta2 release.

Was this page helpful?
0 / 5 - 0 ratings