Do you know how to manage compatibility between different Target Framework Monikers (TFMs)?

Last updated by Yazhi Chen [SSW] 8 months ago.See history

Migrating your project to a new Target Framework Moniker (TFM) can be a complex task, especially when you're dealing with compatibility issues between different Target Framework Monikers (TFMs). It is suggested to handle your migration PBIs (Product Backlog Items) collectively and transition your main branch to the new TFM. Making this judgment call requires careful consideration of factors like the number of PBIs and their estimated completion time.

Here are some essential tips for managing changes that are not compatible with both the old and new TFMs:

Using #if Pragma Statements

You can use #if pragma statements to compile code exclusively for a specific TFM. This technique also simplifies the removal process during post-migration cleanup, especially for incompatible code segments.

Whenever possible, consider using dependency injection or factory patterns to inject the appropriate implementation based on the TFM you are targeting. This approach promotes code flexibility and maintainability, as it abstracts away TFM-specific details.

public static class WebClientFactory
{
  public static IWebClient GetWebClient()
  {
#if NET472
    return new CustomWebClient();
#else
    return new CustomHttpClient();
#endif
  }
}

Code: Good example - Using #if Pragma statements and factory pattern

Using MSBuild conditions

You can use MSBuild conditions to add references to different libraries that are only compatible with a specific TFM. This enables you to manage references dynamically based on the TFM in use.

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
    <Reference Include="System.Web" />
    <Reference Include="System.Web.Extensions" />
    <Reference Include="System.Web.ApplicationServices" />
</ItemGroup>

Code: Good example - Using MSBuild conditions

We open source. Powered by GitHub