zoukankan      html  css  js  c++  java
  • T4 模板代码生成

    <#
    //*********************************************************
    //
    //    Copyright (c) Microsoft. All rights reserved.
    //    This code is licensed under the Microsoft Public License.
    //    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
    //    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
    //    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
    //    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
    //
    //*********************************************************
    #>
    <#@ template language="C#" debug="false" hostspecific="true"#>
    <#@ include file="EF.Utility.CS.ttinclude"#>
    <#@ output extension=".cs"#>
    <#
    
    CodeGenerationTools code = new CodeGenerationTools(this);
    MetadataLoader loader = new MetadataLoader(this);
    CodeRegion region = new CodeRegion(this, 1);
    MetadataTools ef = new MetadataTools(this);
    
    //string inputFile = @"$edmxInputFile$";
    //string inputFile = @"RoRoWoDB.edmx";
    string inputFile = @"..\RoRoWo.Blog.Infrastructure\RoRoWoDB.edmx";
    
    EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
    string namespaceName = code.VsNamespaceSuggestion();
    
    EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
    
    // Write out support code to primary template output file
    WriteHeader(fileManager);
    BeginNamespace(namespaceName, code);
    WriteCustomObservableCollection();
    EndNamespace(namespaceName);
    
    // Emit Entity Types
    foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
    {
        fileManager.StartNewFile(entity.Name + ".cs");
        BeginNamespace(namespaceName, code);
        bool entityHasNullableFKs = entity.NavigationProperties.Any(np => np.GetDependentProperties().Any(p=>ef.IsNullable(p)));
    #>
    <#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>
    {
        // 测试样本
    }
    <#
        EndNamespace(namespaceName);
    }
    
    if (!VerifyTypesAreCaseInsensitiveUnique(ItemCollection))
    {
        return "";
    }
    
    fileManager.Process();
    
    #>
    <#+
    void WriteHeader(EntityFrameworkTemplateFileManager fileManager, params string[] extraUsings)
    {
        fileManager.StartHeader();
    #>
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated from a template.
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    <#=String.Join(String.Empty, extraUsings.Select(u => "using " + u + ";" + Environment.NewLine).ToArray())#>
    <#+
        fileManager.EndBlock();
          
    }
    
    void BeginNamespace(string namespaceName, CodeGenerationTools code)
    {
        CodeRegion region = new CodeRegion(this);
        if (!String.IsNullOrEmpty(namespaceName))
        {
    #>
    namespace <#=code.EscapeNamespace(namespaceName)#>
    {
    <#+
            PushIndent(CodeRegion.GetIndent(1));
        }
    }
    #>
    
    <#+ 
    
    void EndNamespace(string namespaceName)
    {
        if (!String.IsNullOrEmpty(namespaceName))
        {
            PopIndent();
    #>
    }
    <#+
        }
    }
    
    
    void WriteCustomObservableCollection()
    {
    #>
    // An System.Collections.ObjectModel.ObservableCollection that raises
    // individual item removal notifications on clear and prevents adding duplicates.
    public class FixupCollection<T> : ObservableCollection<T>
    {
        protected override void ClearItems()
        {
            new List<T>(this).ForEach(t => Remove(t));
        }
    
        protected override void InsertItem(int index, T item)
        {
            if (!this.Contains(item))
            {
                base.InsertItem(index, item);
            }
        }
    }
    <#+
    }
    
    bool VerifyTypesAreCaseInsensitiveUnique(EdmItemCollection itemCollection)
    {
        Dictionary<string, bool> alreadySeen = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
        foreach(StructuralType type in itemCollection.GetItems<StructuralType>())
        {
            if (!(type is EntityType || type is ComplexType))
            {
                continue;
            }
    
            if (alreadySeen.ContainsKey(type.FullName))
            {
                Error(String.Format(CultureInfo.CurrentCulture, "This template does not support types that differ only by case, the types {0} are not supported", type.FullName));
                return false;
            }
            else
            {
                alreadySeen.Add(type.FullName, true);
            }
    
        }
    
        return true;
    }
    #>
  • 相关阅读:
    VC下使用Proc连接Oracle数据库
    解决ORACLE账号system被锁和修改密码
    Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)[转]
    ckeditor用fckeditor的文件管理器实现图片上传
    video 播放多个视频
    web worker 发送Ajax
    对投影纹理映射的一些思考
    一个光线跟踪的简单实例
    【转载】齐次坐标概念&&透视投影变换推导
    今天开通了cnblog
  • 原文地址:https://www.cnblogs.com/tianjinquan/p/4345080.html
Copyright © 2011-2022 走看看