Skip to content

Instantly share code, notes, and snippets.

@PalleAgermark
Last active January 6, 2017 07:08
Show Gist options
  • Save PalleAgermark/569306b3ac3b8e03894804c2d08cad41 to your computer and use it in GitHub Desktop.
Save PalleAgermark/569306b3ac3b8e03894804c2d08cad41 to your computer and use it in GitHub Desktop.
BP Extension Name Check
namespace MVP.AX.BestPracticeCheckExtensions
{
using System;
using System.Text.RegularExpressions;
using Microsoft.Dynamics.AX.Framework.BestPractices.Extensions;
using Microsoft.Dynamics.AX.Metadata.Upgrade.Common;
using System.Runtime.Serialization;
[BestPracticeRule(
InvalidExtensionnameDiagnosticItem.DiagnosticMoniker,
typeof(Messages),
InvalidExtensionnameDiagnosticItem.DiagnosticMoniker + "Description",
BestPracticeCheckerTargets.TableExtension),
RuleAttributes("MVPProductPrefix.xml")]
public class TableExtensionNameCheck : MetadataDetectorWithXMLDataFile<Microsoft.Dynamics.AX.Metadata.MetaModel.AxTableExtension, EGProductPrefix>
{
/// <summary>
/// This method is called with the top level table extensions - everything else is ignored.
/// </summary>
/// <param name="tableExtension">A tableExtension instance to check for BP violations.</param>
protected override void RunChecksOn(Microsoft.Dynamics.AX.Metadata.MetaModel.AxTableExtension tableExtension)
{
visitTableName(tableExtension.Name);
}
private void visitTableName(string tableName)
{
string requiredPrefix = base.RulePatterns.Prefix;
string dotExtensionPattern = @"\w+." + requiredPrefix + "Extension";
var match = Regex.IsMatch(tableName, dotExtensionPattern);
if (!match)
{
// Didn't fint the correct pattern in the table extension name
// Build a diagnostic ...
InvalidExtensionnameDiagnosticItem diagnostic = new InvalidExtensionnameDiagnosticItem(
ModelElementPathBuilder.CreatePathForTable(tableName),
"TableExtension",
null,
tableName);
// ... and report the error.
this.ExtensionContext.AddErrorMessage(diagnostic);
}
}
}
[Serializable, DataContract(Name = "MVPProductPrefix", Namespace = "")]
public class MVPProductPrefix : RulePatternsBase
{
/// <summary>
/// This class is the data contract for the XML file
/// </summary>
// Fields
[DataMember(Name="Prefix")]
public string Prefix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment