Last active
October 7, 2022 07:35
-
-
Save justinyoo/c20b518aeef74789469c764360076e38 to your computer and use it in GitHub Desktop.
AutoMapper DI into Azure Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppModule : Module | |
{ | |
public override void Load(IServiceCollection services) | |
{ | |
... | |
services.AddAutoMapper(Assembly.GetAssembly(this.GetType())); | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetSecretFunction : FunctionBase<ILogger>, IGetSecretFunction | |
{ | |
... | |
private readonly IMapper _mapper; | |
public GetSecretFunction(AppSettings settings, IMapper mapper, IKeyVaultClient kv) | |
{ | |
... | |
this._mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); | |
} | |
public override async Task<TOutput> InvokeAsync<TInput, TOutput>(TInput input, FunctionOptionsBase options = null) | |
{ | |
... | |
var mapped = this._mapper.Map<SecretModel>(secret); | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SecretsHttpTrigger | |
{ | |
// Register dependencies through the fucntion factory. | |
public static IFunctionFactory Factory = new FunctionFactory<AppModule>(); | |
[FunctionName(nameof(GetSecret))] | |
public static async Task<IActionResult> GetSecret( | |
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "secrets/{name}")] HttpRequest req, | |
string name, | |
ILogger log) | |
{ | |
IActionResult result; | |
try | |
{ | |
... | |
// Invokes function containing all business logics | |
result = await Factory.Create<IGetSecretFunction, ILogger>(log) | |
.InvokeAsync<HttpRequest, IActionResult>(req, options) | |
.ConfigureAwait(false); | |
... | |
} | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecretProfile : Profile | |
{ | |
public SecretProfile() | |
{ | |
this.CreateMap<SecretBundle, SecretModel>() | |
.ForMember(d => d.Id, o => o.MapFrom(s => s.Id)) | |
... | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where can I locate an implemented code for this in azure functions App?