全文索引的 CodeFluent 方面

CodeFluent Aspect for Full-Text Index

我正在尝试开发一个 CodeFluent 方面来将实体的 属性 设置为全文索引。

我找到了这个 link,它的作用与我的目标相似。 http://blog.codefluententities.com/2012/11/27/using-the-sql-server-template-producer-to-generate-clustered-indexes/

然而,这使用了 SQL 模板制作器。无论如何,是否可以将 属性 完全设置为方面本身的全文索引,这样我就不必 install/maintain 所有项目的模板制作者和方面?

这是我目前拥有的 C# 方面代码:

    public class FullTextIndexing : IProjectTemplate
    {
        public static readonly XmlDocument Descriptor;
        public const string Namespace = "http://www.softfluent.com/aspects/samples/FullTextIndexing";

        static FullTextIndexing()
        {
            Descriptor = new XmlDocument();
            Descriptor.LoadXml(
@"<cf:project xmlns:cf='http://www.softfluent.com/codefluent/2005/1' defaultNamespace='FullTextIndexing'>
    <cf:pattern name='Full Text Indexing' namespaceUri='" + Namespace + @"' preferredPrefix='fti' step='Tables'>
        <cf:message class='_doc'>CodeFluent Full Text Indexing Aspect</cf:message>
        <cf:descriptor name='fullTextIndexing'
            typeName='boolean'
            category='Full Text Indexing'
            targets='Property'
            defaultValue='false'
            displayName='Full-Text Index'
            description='Determines if property should be full text indexed.' />
    </cf:pattern>
</cf:project>");
        }

        public Project Project { get; set; }

        public XmlDocument Run(IDictionary context)
        {
            if (context == null || !context.Contains("Project"))
            {
                // we are probably called for meta data inspection, so we send back the descriptor xml
                return Descriptor;
            }

            // the dictionary contains at least these two entries
            Project = (Project)context["Project"];

            // the dictionary contains at least these two entries
            XmlElement element = (XmlElement)context["Element"];
            Project project = (Project)context["Project"];

            foreach (Entity entity in project.Entities)
            {
                Console.WriteLine(">>PROPERTY LOGGING FOR ENTITY "+entity.Name.ToUpper()+":<<");
                foreach (Property property in entity.Properties)
                {
                    Log(property);
                    if(MustFullTextIndex(property))
                    {
                        Console.WriteLine("CHANGING PROPERTY");
                        property.TypeName = "bool";
                        Log(property);
                    }
                }
            }

            // we have no specific Xml to send back, but aspect description
            return Descriptor;
        }

        private static bool MustFullTextIndex(Property property)
        {
            return property != null && property.IsPersistent && property.GetAttributeValue("fullTextIndexing", Namespace, false);
        }

        private static void Log(Property property)
        {
            Console.WriteLine(property.Trace());
        }
    }

编辑一个:

按照 Meziantou 的回答,我正在尝试创建一个模板制作者,但是当我尝试将新的模板制作者添加到项目制作者列表时它给了我编译错误,所以我可能做错了。

错误说:

Cannot convert type 'CodeFluent.Model.Producer' to 'CodeFluent.Producers.SqlServer.TemplateProducer'

这是我目前的代码:

public XmlDocument Run(IDictionary context)
{
    if (context == null || !context.Contains("Project"))
    {
        // we are probably called for meta data inspection, so we send back the descriptor xml
        return Descriptor;
    }

    // the dictionary contains at least these two entries
    XmlElement element = (XmlElement)context["Element"];
    Project project = (Project)context["Project"];

    CodeFluent.Producers.SqlServer.TemplateProducer producer = new CodeFluent.Producers.SqlServer.TemplateProducer();
    producer.AddNamespace("CodeFluent.Model");
    producer.AddNamespace("CodeFluent.Model.Persistence");
    producer.AddNamespace("CodeFluent.Producers.SqlServer");

    Console.WriteLine(producer.Element);
    //TODO: Need to figure out how to modify the actual template's contents

    project.Producers.Add(producer); //Error happens here


    // we have no specific Xml to send back, but aspect description
    return Descriptor;
}

在示例代码中,使用方面只是因为它有一个描述符。 CodeFluent 实体使用描述符来填充 属性 网格:

<cf:descriptor name="IsClusteredIndex" typeName="boolean" targets="Property" defaultValue="false" displayName="IsClusteredIndex" />

因此,当您将此 属性 的值设置为 true 或 false 时,xml 属性 ns:IsClusteredIndex 将被添加或从 xml 文件中删除。

然后 SQL 模板读取属性的值以生成预期的 SQL 文件:

property.GetAttributeValue("sa:IsClusteredIndex", false)

所以方面不是强制性的,而是为add/remove属性提供了图形界面友好的方式。如果不需要集成到图形界面,可以放心的去掉切面。

如果您的目标是集成到图形界面中,则需要一个方面(XML 或 DLL)或一个生产者。如果您不想创建生产者,您可以将模板嵌入到您的方面。在构建过程中,您可以提取 SQL 模板并将 SQL 模板制作者添加到项目中,这样一切都位于方面。