How to create/use Configuration File for Infrastructure Layer (Class Library) Project in Domain-Driven Model

2.5k views Asked by At

In my Domain Driven Layers, Infrastructure Layer Project is class library Project which needs to use a configuration file for not only connecting to database but also to create Repositories and Entities. However, the links (mentioned at the end of post state that class library project cannot have a configuration file (I think, class library cannot have a app.config).

My Question is that if not then how to create and use a configuration file for my Infrastructure Layer (Class Library) Project ? under which Project and what should be the naming convention ?

DETAIL:

I am following a book on Domain Driven Design in C#.NET. In this model (Visual Studio Solution), there four layers (each project in Visual Studio as layer) named below:

  1. Presentation Layer Project (WPF, not .NET class library)
  2. Application Layer Project (.NET class Library)
  3. Model Layer Project (class Library)
  4. Infrastructure Layer (.NET class Library)

Infrastructure Layer Project (as .NET clas Library) needs to use a configuration file, in order to:

A) Create Repository (using Repository Factory), B) Create Entities (using EntityFactoryBuilder), C) Connecting to database.

For this i created configuration file under the project "ProjectName.Infrastructure" as "ProjectName.Infrastructure.config" by setting its property (using Visual Studio) "Copy to Output Window" as "CopyAlways".

However, according to some posts (links at the end):

"a class library project (like my "Infrastructure Layer Project") cannot have a configuration file (application configuration file OR app.config file) instead a calling application should supply/provide configuration file and that class library use this provided file".

Links ( That i researched on):

How to access a custom config from my class library application?

and

http://msdn.microsoft.com/en-us/library/ms229689.aspx

1

There are 1 answers

0
Fakhar Anwar On

The idea is to keep configuration file on client side (Application OR Presentation Layer level), not on server side (class library or DLL), Why ?

Bacause, purpose that a class library/dll (Infrastructure Layer/Project) serves is to be shared among more than one Clients (Presentation Layers OR Software Applications), each client (Software Application) configured with its own configuration setting that can be used (at run time) by the Class Library (.NET's ConfigurationManager class of Syste.Configuration ensures provision of Configuratio of calling/client application to class library so class library can use ConfigurationManager to use calling/client software's configuration).

In other words, it is duty of Presentation Layer to supply its configuration setting (in its App.config file) so that Class Library (DLL) access these configuration settings (residing in App.config file of the specific client) on the fly (;at run time), if needed, using ConfigurationManager class (of System.Configuration).

I see it very useful in that every application will have its own configuration whereas class libary will be able to access the caller's/client's configuration through .NET System (System.Configuration) as described above.

(Note: I somwhere read that class library can have its own configuration also (probably for its own internal use) however class library's internal configuration setting is not related to App.Config, i think)