1

エンティティのプロパティをフルテキスト インデックスに設定する CodeFluent アスペクトを開発しようとしています。

私が目指しているものと似たようなことをするこのリンクを見つけました。 http://blog.codefluententities.com/2012/11/27/using-the-sql-server-template-producer-to-generate-clustered-indexes/

ただし、これは SQL テンプレート プロデューサーを使用します。プロパティをアスペクト自体で完全にフルテキスト インデックスに設定する方法はありますか?そのため、すべてのプロジェクトでテンプレート プロデューサーとアスペクトの両方をインストール/維持する必要はありません。

私がこれまでに持っている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());
        }
    }

編集 1:

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;
}
4

1 に答える 1

0

サンプル コードでは、記述子があるという理由だけでアスペクトが使用されます。記述子は、プロパティ グリッドを設定するために 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)

したがって、アスペクトは必須ではありませんが、属性を追加/削除するためのグラフィカル インターフェイスに適した方法を提供します。グラフィカル インターフェイスに統合する必要がない場合は、アスペクトを安全に削除できます。

グラフィカル インターフェイスに統合することが目標の場合は、アスペクト (XML または DLL) またはプロデューサーが必要です。プロデューサーを作成したくない場合は、テンプレートをアスペクトに埋め込むことができます。ビルド中に、SQL テンプレートを抽出し、SQL テンプレート プロデューサーをプロジェクトに追加できます。このようにして、すべてがアスペクトに配置されます。

于 2016-03-10T10:37:56.210 に答える