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.

ilkerkaran
  • 4,214
  • 3
  • 27
  • 42
Bryan
  • 3,421
  • 8
  • 37
  • 77
  • 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. – halfer Aug 12 '18 at 12:13

1 Answers1

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.

alsami
  • 8,996
  • 3
  • 25
  • 36
  • According to this answer: https://stackoverflow.com/a/43063213/4444267 you should not do that. – Bryan Aug 01 '18 at 07:53
  • Better don't mess with what Steven says :P he should know best. Updated my answer accordingly. – alsami Aug 01 '18 at 08:10
  • So how do I access the connectionstring in my repositories in the third layer? :P – Bryan Aug 01 '18 at 08:19
  • 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. – alsami Aug 01 '18 at 08:20
  • 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? – Bryan Aug 01 '18 at 13:32
  • Only if you need that option in multiple assemblies. Otherwise you can create that option in the DAL project. – alsami Aug 01 '18 at 13:34
  • But If I create the Option class in my third layer(DAL), I must add a reference to It from my web layer? I don't want that. – Bryan Aug 01 '18 at 13:36
  • I would suggest you to have a gluer project that only contains program and startup class. You put your controllers in a separate project. The gluer project then references everything. Is the only way you can make it if you want it that way and a lot of people do that. Here is a sample of how I am doing that. https://github.com/alsami/etdb-userservice-aspnet-core – alsami Aug 01 '18 at 13:39