I'm wondering why anyone (including myself) bothers to create insanely long and tedious xdt transforms for each key in a web.config file when one can simply put a "Replace" statement alongside the configuration declaration.

Let me explain with an example:

You are a developer that has been tasked to create a series of web.config transforms for a large web application.

You are given the web.configs for each environment and told to make:

  • a base web.config that contains all keys and values common to each environment
  • sets of transform files that contain all keys and values that differ from environment to environment

Here is a sample base web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
    <add key="db.schema" value="app" />
    <add key="versionNumber" value="" />
    <add key="culture" value="en-US" />
    <add key="url" value="" />
    <add key="cache.Duration" value="0" />
</appSettings>
</configuration>

Here is a sample transform for the base web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="versionNumber"
     value="01.67.00"
     xdt:Transform="Replace" xdt:Locator="Match(key)"/>
<add key="url"
     value="http://thisIsNotAnActualURL.com"
     xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>

Which outputs, as desired, the following:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
    <add key="db.schema" value="app" />
    <add key="versionNumber" value="01.67.00" />
    <add key="culture" value="en-US" />
    <add key="url" value="http://thisIsNotAnActualURL.com" />
    <add key="cache.Duration" value="0" />
</appSettings>
</configuration>



This is all fair and well but if you're a developer that's creating transforms based on massive web.configs that already exist wouldn't it be a lot easier to do the following as opposed to the above approach:

Base web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
</configuration>

Transform:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="Replace">
<appSettings>
    <add key="db.schema" value="app" />
    <add key="versionNumber" value="01.67.00" />
    <add key="culture" value="en-US" />
    <add key="url" value="http://thisIsNotAnActualURL.com" />
    <add key="cache.Duration" value="0" />
</appSettings>

The result is identical to the previous example, with far less work involved:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
    <add key="db.schema" value="app" />
    <add key="versionNumber" value="01.67.00" />
    <add key="culture" value="en-US" />
    <add key="url" value="http://thisIsNotAnActualURL.com" />
    <add key="cache.Duration" value="0" />
</appSettings>
</configuration>

I understand that by using this approach, when a change needs to occur in every environment, the change needs to be reflected in every transform; but apart from that, I can't see any drawback.

Please tell me I'm missing something obvious here, as I'm finding transforms that took me over 8 hours to code can be done in a matter of seconds with no apparent drawback

有帮助吗?

解决方案

Conjecture was the base of the advice I got here, we did decide on using "shorter" transformation method because of the following problems with the official method:

  1. Takes longer to code
  2. Takes longer to edit
  3. Sharp learning curve for developers that have no experience with xdt
  4. Increases code base without cutting on duplication
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top