File Layout - Entry.Match for Factory Method

One of the requirements that our company standards specify is that static factory methods must be grouped with the constructors.  I am attempting to set up the File Layout options to accomplish this.  So far, I have:

          <Kind Is="Constructor" />
               <Kind Is="Method" />
               <Access Is="Public" />
               <Static />
               <-- What do I put here to specify that the method should return the current type? -->

1 comment
Comment actions Permalink

You've probably already thought of this, but you can match by name instead of return type, using a regular expression. Example rule: factory method names start with "Create".

<Entry.Match>      <Or>           <Kind Is="Constructor" />           <And>                <Kind Is="Method" />                <Access Is="Public" />                <Static />                <Name Is="Create.*" />           </And>      </Or> </Entry.Match>

Alternatively, you can use attributes to tag type members with special meaning. You could create a "ConstructorAttribute" that can be applied to factory methods. Then update your pattern to search for that attribute.

<Entry.Match>      <Or>           <Kind Is="Constructor" />           <And>                <Kind Is="Method" />                <Access Is="Public" />                <Static />                <HasAttribute Name="MyCompany.MyNamespace.ConstructorAttribute"/>           </And>      </Or> </Entry.Match>

The first pattern would be convention-based while the second pattern would be configuration-based. But why not allow both?

<Entry.Match>      <Or>           <Kind Is="Constructor" />           <And>                <Kind Is="Method" />                <Access Is="Public" />                <Static />                <Or>                     <Name Is="Create.*" />                     <HasAttribute Name="MyCompany.MyNamespace.ConstructorAttribute"/>                </Or>           </And>      </Or> </Entry.Match>


Please sign in to leave a comment.