Redefine spring.net object in multiple configuration files

I am setting up my xml configuration files for my asp.net web application using spring.net-dependent IOC injection. I referenced each of my configuration files in the web.config file. Sample settings in spring.net configuration file (settings.xml):

<object id="obj1" type="NS.Common.Cache.Class, NS.Common" singleton="true" init-method="Initialize" destroy-method="Dispose"> <property name="Name" value="My Name" /> </object> 

It all works great.

Now I install my web application in several environments, so I am creating a spring.net configuration file for the environment, for example. dev, qa, prod.

Thus, when installing the application, the corresponding spring environment file is referenced in the web.config file. This is part of an automatic installer.

Inside the qa environment file, I want to override the object above "obj1":

 <object id="obj1" type="NS.Common.Cache.Class2, NS.Common" singleton="true" init-method="Initialize" destroy-method="Dispose"> <property name="Name" value="My New Name" /> </object> 

However, since it is automated (adding a link to the environment file), the settings.xml file does not change.

And now referring to 2 files with a specific object with the same identifier - this causes serious problems, because runtime errors will occur.

Is there a way that I can include in qa.xml a flag or the like to highlight this object definition overriding any other specific objects in any other XML file with the same object identifier?

+7
source share
2 answers

You can load two identical identifiers, and the last identifier will "redefine" the first object (before creation, they must be in different files, referring to them in the context definition).

 <context ...> <resource ... /> <!-- put your 3rd-party (read-only config here) --> <resource ... /> <!-- put your override ids here --> </context> 

Because of this default behavior, it is recommended that you enable your contextual resources in the order starting from the "global value" (for example, the third-party configurations you want to use) to the "local value" (with app.config as the last entry).

+6
source

Instead of certain objects with the same identifier (which is impossible, as mentioned in Marijin), you can define an alias in the configuration file that you can control.

eg. could you

 <object name="ProdObj1" type="NS.Common.Cache.Class, NS.Common" singleton="true"> <property name="Name" value="Prod" /> </object> 

and

 <object name="TestObj1" type="NS.Common.Cache.Class, NS.Common" singleton="true"> <property name="Name" value="Test" /> </object> 

and then use

 <alias name="ProdObj1" alias="obj1"/> 

in, for example, your web.config.

+1
source

All Articles