1

I'm currently developing a web API in .NET core. I have three projects in my solution with the following references:

Web -> Services -> DataAccess

So the web layer does not have a direct reference to the DataAccess layer.

My question is: What is the right way to get the connectionstring in this type of architecture with three layers? I have read around, but can't find any nice solution where I can access my connectionstring in the third layer, just because the web layer does not have a reference to the third layer.

I came accross this approach:

services.Configure<ConnectionConfig>(Configuration.GetSection("ConnectionStrings"));
services.AddScoped<IQueryHelper>(c => new QueryHelper(cn));

This work well if I just have two layers, where the QueryHelper is in the service-layer.

But I want to access one or multiple connectionstrings in my DataAccess-layer.

1
  • Bryan, I wonder if I can ask you to take a bit more care when writing questions? We are keen on making questions succinct and readable here, and to that end, spelling matters. The contraction of "I am" is "I'm", which always has an apostrophe (63 posts to repair). There is no need to add "please help me" and other forms of pleading (39 posts to repair). We don't have enough editor volunteers here as it is, so would you bear this in mind for the future? Thank you. Commented Aug 12, 2018 at 12:13

1 Answer 1

0

Edit: Injecting the configuration might not be the smartest idea as you can read here. Better way would be to configure options for each connection string that can be accessed by the DAL aswell.

services.Configure<MyConnectionInfo>(options => Configuration.GetSection("MyConnectionInfo").Bind(options));

Now in your repository just inject IOptions<MyConnection> and use the values.

Old Answer: Just inject your configuration into your datalayer-classes. Before you have to register the configuration with the ioc-container.

services.AddSingleton(typeof(IConfiguration), Configuration);

Now access the connectionstrings you need by injection the instance of IConfiguration. You could also configure more options instead, but injecting the configuration is fine aswell.

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

8 Comments

According to this answer: stackoverflow.com/a/43063213/4444267 you should not do that.
Better don't mess with what Steven says :P he should know best. Updated my answer accordingly.
So how do I access the connectionstring in my repositories in the third layer? :P
Configure an option class that can be injected in your third layer. The option class must be in a standalone assembly if you want to access it in multiple assemblies. Provided an example.
so I much add a new project(assemblie) where I put my option class.After that, I must add a reference to this assemblie from my third layer?
|

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.