So after the basics and after you understood the framework lets look at extending it a bit.
We shall begin with something simple: creating ArcSde domains (not assigning the domain)
Before we run to the code – what do we want to achieve?
Create.Domain(“DomainName”).OfType(TypeEnum.SomeType)
.WithKey(ObjectKey).WithValue(StringValue)
…WithKey(ObjectKey).WithValue(StringValue);
That was what I thought should be the raw basic design.
How can we improve it? Well I don’t like using objects so:
Create.Domain<DomainType>(“DomainName”)
.WithKey(DomainTypeKey).WithValue(StringValue)
…WithKey(DomainTypeKey).WithValue(StringValue);
Isn’t that better? It will need more work but even the newest programmer won’t abuse it and cause you those white hairs…
Now let think about <DomainType> what can it be? Domain could be of the types:
- public enum esriFieldType
- {
- esriFieldTypeSmallInteger,
- esriFieldTypeInteger,
- esriFieldTypeSingle,
- esriFieldTypeDouble,
- esriFieldTypeString,
- esriFieldTypeDate,
- esriFieldTypeOID,
- esriFieldTypeGeometry,
- esriFieldTypeBlob,
- esriFieldTypeRaster,
- esriFieldTypeGUID,
- esriFieldTypeGlobalID,
- esriFieldTypeXML,
- }
<DomainType> could be struct but struct doesn’t include string…
I actually don’t mind limiting the users a bit and giving them the options of only short (SmallInteger), int, float, double and string. All the other options are too specific for a Domain. And string is still a problem! For this example we will stick with struct…
From that we can do the design of CreateDomainExpression:
- public class CreateDomainExpression<T> : SdeMigrationExpressionBase
- where T:struct
- {
- public string DomainName { get; set; }
- private readonly List<DomainValue<T>> _domainValues = new List<DomainValue<T>>();
- public List<DomainValue<T>> DomainValues
- {
- get { return _domainValues; }
- }
Where DomainValue is defined as (KeyValuePair is not changeable after construction):
- public class DomainValue<T>
- {
- public T DomainKey { get; set; }
- public string Value { get; set; }
- }
Now since we don’t want to change too much of the FluentMigrator framework we will use SdeCreate instead of Create, and use SdeMigration abstract class instead of Migration.
The only change in FM framework you need to do is change IMigrationContext to protected in Migration:
- namespace FluentMigrator
- {
- public abstract class Migration : IMigration
- {
- protected IMigrationContext _context;
The reason for that is that our expressions need to be in the MigrationContext when the Runner executes them (part 5 of the previous post).
- public abstract class SdeMigration : Migration
- {
- public ISdeCreateExpressionRoot SdeCreate
- {
- get { return new SdeCreateExpressionRoot(_context); }
- }
- public interface ISdeCreateExpressionRoot : IFluentSyntax
- {
- ICreateDomainSyntax Domain<T>(string domainName)
- where T:struct ;
- public interface ICreateDomainSyntax<T>
- where T: struct
- {
- ICreateDomainValueSyntax<T> WithKey(T key);
- }
- public interface ICreateDomainValueSyntax<T>
- where T:struct
- {
- ICreateDomainSyntax<T> WithValue(string domainValue);
- }
The end result for the look and feel of the Builder:
- [Migration(002)]
- public class Migration_002 : SdeMigration
- {
- public override void Up()
- {
- SdeCreate.Domain<short>("DomainName")
- .WithKey(1).WithValue("1")
- .WithKey(2).WithValue("2");
Looks like the wanted fluent code of FM.
Now lets implement some code:
- public class CreateDomainExpression<T> : SdeMigrationExpressionBase
- where T:struct
- {
- private esriFieldType ExtractType()
- {
- var defaultT = default(T);
- if (defaultT is short)
- return esriFieldType.esriFieldTypeSmallInteger;
- if (defaultT is float)
- return esriFieldType.esriFieldTypeSingle;
- if (defaultT is double)
- return esriFieldType.esriFieldTypeDouble;
- if (defaultT is int)
- return esriFieldType.esriFieldTypeInteger;
- if (defaultT is string)//TODO: will never be string
- return esriFieldType.esriFieldTypeString;
- throw new NotImplementedException("The only types implemented are short,float,double,int and string.");
- }
- private List<KeyValuePair<object , string>> ExtractDomainValues()
- {
- return DomainValues.Select(domainValue => new KeyValuePair<object, string>(domainValue.DomainKey, domainValue.Value))
- .ToList();
- }
- #region Implementation of ICanBeValidated
- public override void CollectValidationErrors(ICollection<string> errors)
- {
- if (String.IsNullOrEmpty(DomainName))
- errors.Add("The domain's name cannot be null or an empty string");
- }
- #endregion
- #region Implementation of IMigrationExpression
- public override void ExecuteWith(DeploymentWorkspaceUtils utils)
- {
- utils.CreateDomain(DomainName, ExtractType(), ExtractDomainValues());
- }
- public override IMigrationExpression Reverse()
- {
- return new DeleteDomainExpression
- {
- DomainName = DomainName
- };
- }
- #endregion
Where SdeMigrationExpressionBase is an implementation of MigrationExpressionBase that parses the ConnectionString from processor to a SDE connection string and creates a DeploymentWorkspaceUtils from it:
- public abstract class SdeMigrationExpressionBase : MigrationExpressionBase
- {
- public abstract void ExecuteWith(DeploymentWorkspaceUtils utils);
- public override void ExecuteWith(IMigrationProcessor processor)
- {
- var utils = DeploymentWorkspaceProvider.Instance.GetWorkspace((SqlServerProcessor)processor);
- ExecuteWith(utils);
- }
- }
- public class DeploymentWorkspaceProvider
- {
- #region Singleton
- private static readonly DeploymentWorkspaceProvider instance = new DeploymentWorkspaceProvider();
- // Explicit static constructor to tell C# compiler
- // not to mark type as beforefieldinit
- private DeploymentWorkspaceProvider()
- {
- }
- public static DeploymentWorkspaceProvider Instance
- {
- get { return instance; }
- }
- #endregion
- public DeploymentWorkspaceUtils GetWorkspace(SqlServerProcessor processor)
- {
- string connectionString = processor.Connection.ConnectionString;
- return
- WorkspaceProvider.Instance.GetDeploymentWorkspace(
- ConnectionStringUtils.GetSdeConnectionString(connectionString));
- }
- }
The basic parser
//TODO: Continue this!
//TODO: replace FM with FluentMigrator
//TODO: replace the links at the top to the real DllShepherd.net blog site
Resources:
Github: FluentMigrator Project (the source code)
Keywords: FluentMigrator , framework, expression, nutshell, design, Fluent, Domain, C#, ArcSde, ArcObjects