redefine spring.net object in multiple configuration files
Asked Answered
L

2

7

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

<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>

This all works fine.

Now I install my web application in multiple environments so I am creating a spring.net config file for environment eg. dev, qa, prod.

So when installing the application, the applicable environment spring file is referenced in the web.config. This is part of an automated installer.

Within the qa environment file, I want to redefine the object above "obj1" to:

<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 as this is automated (adding the reference to the environment file), the settings.xml file is not changed.

And now referencing 2 files with a defined object with the same id - this causes major problems as run time errors will occur.

Is there any way that I can include in the qa.xml and flag or the like to highlight this object definition overrides any other defined objects in any other xml file with the same object id?

Leonerd answered 22/8, 2011 at 14:37 Comment(3)
You can load two identical ids and this will "override" the first listed object (before creation, AFAIK they must be in different files by having them referenced via <context ...> <resource ...>. Because of this it is a good practice to include your context ressources in the order beginning with "global meaning" down to "local meaning" (having app.config's <resource uri="config://spring/objects" /> at the last entry).Malisamalison
@Stegi: why not provide this as an answer? Looks like this solves OP's problem.Moth
In a single xml file, an id can only be specified once. The id attribute actually is an xml attribute - so the xml parser gives you additional validation, both in Visual Studio and when loading the file at runtime. But as Stegi mentions, you can specify an object with the same id in different files, loaded by the same context. The the defintion from the last loaded file overrides any previous definitions with the same id.Moth
M
6

You can load two identical ids and the last id will "override" the first listed object (before creation, they must be in different files by having them referenced within the context definition).

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

Because of this default behaviour it is a good practice to include your context ressources in the order beginning with "global meaning" (e.g. 3rd party configs you want to reuse) down to "local meaning" (having app.config's as last entry).

Malisamalison answered 14/3, 2012 at 14:54 Comment(0)
M
1

Instead of defininig objects with the same ID (which is not possible as Marijin already mentioned) you could define an alias in a configuration file you are able to control.

E.g. you could have

<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 instance, your web.config.

Membranophone answered 23/8, 2011 at 7:7 Comment(4)
This will only work when you can change settings.xml, namely rename obj1 to ProdObj1. From the question I understand that OP wants to avoid exactly that. So this can be a useful technique -in fact it might be the way to go for OP's scenario- but it would require changing settings.xml.Moth
how do you mean changing settings.xml?Leonerd
Your scenario as I undestand it: from your first paragraph I read that obj1 is defined in a file "settings.xml" rigth? this settings file is deployed to your qa and production environments. On both qa and production you reference settings.xml, which (for some unspecified reason) you can't change. So on both production and qa, obj1 is an object defined in settings.xml.Moth
This alias technique is a good one, but you will have to change "settings.xml", namely take out the obj1 definition. You can define ProdObj1 in "production.xml" and TestObj1 in qa.xml. By adding the alias like Andreas suggests, you will still be able to use the obj1 reference across the container - also in settings.xml.Moth

© 2022 - 2024 — McMap. All rights reserved.