2

I have three projects in my C# 4 solution.

  • MooDB: Has my domain objects in, NHibernate mappings, presentation classes.
  • Data: Has my data access layer classes (using NHibernate)
  • Test: Has my test classes

Here's a screenshot:

enter image description here

I've noticed that I've got app.config and hibernate.cfg.xml in my Test project. This does seem right. I don't have an app.config in my Data project which is a problem because I want to use log4net in my Data project but I can't configure it if there's no app.config there.

What's the best way to set up my solution? Should there be an app.config for each project or for the solution as a whole? Do I need one for my Test project? Have I organized it according to "best practise"?

Mark Allison
  • 6,838
  • 33
  • 102
  • 151
  • 1
    I tend to put settings like connection strings, etc in the hosting application's `app.config`, and pass those to the class libraries (`Data` in this case). It's a bad idea to make a class library depend on external resources that are application-specific. – 3Dave Jan 13 '12 at 16:11

2 Answers2

3

I usually see config files at the highest layer; which is usually the UI or Test.

This makes sense (to me at least), because the application is directing the data layer to which repository it should be fetching the data from. Your test layer is probably going to go to a different place than your production application; so having config in the Data project wouldn't make much sense to me... it's the top layer that's "configuring" the bottom layers.

Giovanni Galbo
  • 12,963
  • 13
  • 59
  • 78
1

If you want a single place to make config changes, you can config files in your main project and use post-build events to copy the resulting output files to the bin folders of your other executable projects.

You need to consider whether the configuration that you use in your main application is the same as the one you want to use in your unit tests though. If not, then separate config files are absolutely ok.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • What's confusing me is I'm not sure when the config files are read. If I right click on RepoTests.cs and Run All Tests, which config files are read? The one in the `Test` project, or do other config files get read as well if `Test` references `Data`? – Mark Allison Jan 13 '12 at 16:13
  • 1
    The config files that are read are the ones associated with the executable (or test) project being run. It's a common SO question (http://stackoverflow.com/questions/594298/c-sharp-dll-config-file), but when you think about it the configuration for log4net that you want to use when running a production application is likely different than what you want while testing. That's why configuration is done in a config file and not in code. – Chris Shain Jan 13 '12 at 16:16