1

My project is based on .NET Core WEB API and it has 3 layers within it i.e. The controllers, Business layer & Data layer.

I have introduced paging in WEB API so that data can be retrieved in batches. The page size is the one I'm looking forward to keep it configurable in one of configuration file.

I'm carrying my paging operation at data access layer and want to read that environment variable.

Is it good practice to do so and in what way I can achieve it ?

Any help or pointers are welcomed.

1
  • 1
    Environment variables are already available through the Configuration middleware. You can access their values the same way you would any configuration setting - either through configuration objects, the IOption<> pattern or an injected IConfiguration interface Commented Jul 25, 2022 at 18:54

1 Answer 1

1

The preferred way to do this is via the .NET Core configuration. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0

You can have the default values set in your configuration file but they can also be overridden by environment variables.

You can also bind the configuration sections to a POCO as described here. https://medium.com/@dozieogbo/a-better-way-to-inject-appsettings-in-asp-net-core-96be36ffa22b

This will give you a strongly typed configuration object which is easier to work with.

Sign up to request clarification or add additional context in comments.

Comments

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.