2

I have a n-tiered wpf application using EF in Data Access Layer. The simplified structure looks like this

- DataProject.csproj
    * data.config
    * Repository.cs
- Presentation.csproj
    * presentation.config
    * PresentationCode.cs

As may be seen DataProject.dll is referenced by Presentation.csproj

I use ConfigurationManager in my Repository.cs to get the ConnectionStringSettingsCollection, but ConfigurationManager by default accesses the presentation.config by default, even though the code is called from Repository.cs in DataProject. And this is how is should be.

My question is how should I then access the connection strings correctly as there are several options open.

  1. Copy the connection strings section from data.config to presentation.config. But what if the connection changes ? I have to keep changing them in presentation.config file.

  2. Should I point a link to data.config file from presentation.config file.

<configuration>

`<connectionStrings configSource="connections.config"/>`

</configuration>

  1. Or should I use ConfigurationManager.OpenExeConfiguration() to access the data.config.

Currently, I find option 2 as the best option as it takes less refactoring and maintenance later. But I would be glad to know the appropriate approach.

Edit:

Is there a way that EF points directly to my presentation.config file to access the connection strings ?

1 Answer 1

1

Actually, the most appropriate way is to avoid accessing any configuration data in your DAL and simply pass it on instantiation. This way you can store the configuration wherever your application wants it, and your DAL will be reusable.

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

2 Comments

Ok I get your point. Is there a way that EF points directly to my presentation.config file to access the connection strings ?
DbContext accepts connection string as a constructor argument.

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.