3

n 層アプリケーションで自己追跡エンティティを使用しています。したがって、データレイヤーへのクライアントアクセスを提供するWCFサービスがあり、モデルからいくつかのエンティティを取得するに対応する多くの「同じ」関数を実装していることに気付きGetOrders、クライアントでそれらを変更した後、Save(Order order)またはSave(TrackableCollection<Orders> orders)変更を永続化する操作。

各エンティティの単一およびコレクションの Get/Save と、モデルからの対応するサービス実装を使用して、基本的なインターフェイスを構築できる T4 テンプレートが存在するかどうか疑問に思っています。

このサービスを生成するために独自の T4 テンプレートを作成できることは承知しており、必要に応じて作成することもあるでしょうが、最初に、この取り組みが既にインターネット上のどこかに存在するかどうかをコミュニティに尋ねてみようと思いました。計画されている、他の人が望んでいる、および/またはまったくばかげた考えです。

このWCF Data Services Generatorの例を見つけましたが、これは正しい方向に進んでいます。

おそらく、n 層ソリューションで WCF を使用する STE により適したものがあるでしょうか?

4

1 に答える 1

2

そこには(もっと)ありますが、常にカスタマイズする必要があります。オンラインで見つけたシンプルなテンプレートをカスタマイズしました。基本的には、win32クライアントに表示するための操作コントラクトとWebメソッドを作成します。以下は抜粋です。

// I obtain anykeys by iterating through the entity keys:
_anyKeys = string.Format("{0}{1} item.{2} == {3}_dto.{2}", _anyKeys, _and, key.ToString(), _double);
    // loop through all the entities:
    foreach (EntityType entity in Item1Collection.GetItems<EntityType>().OrderBy(e => e.Name))
    {

    /// <summary>
    /// <#=code.Escape(entity)#> Operation contracts - <#=code.Escape(entityName)#> 
    /// </summary>  
    [WebMethod]
    public bool Save<#=code.Escape(entity)#>(Dto<#=code.Escape(entity)#> _dto, int racID, string profile)
    {
        try
        {
            // Load the db
            using (<#=ContextName#> db = new <#=ContextName#>(EFUtils.GetEFConnectionString(profile, modelName)))
            {
                // Check if item exists         
                var exists = db.<#=code.Escape(entityName)#>.Any(item=> <#=code.Escape(_anyKeys)#>);
                // convert to entity
                var _entity = _dto.ToEntity();
                if(exists)
                {
                  // Attach the entity to the db
                  db.<#=code.Escape(entityName)#>.Attach(_entity);
                  // Change tracking
                  ChangeTracking<<#=code.Escape(entity)#>>(_dto.ModifiedProperties, db, _entity);
                }
                else
                {
                  // New entity
                  db.<#=code.Escape(entityName)#>.Add(_entity);
                }
                // Save changes
                return db.SaveChanges() > 0;
            }
        }
        catch(Exception ex)
        {               
            Global.LogMessageToFile(ex, string.Format("{0} - profile {1}, Save<#=code.Escape(entity)#>", racID, profile));
        }
        finally
        {       
        }
        return false;
    }


// Then the interfaces
#>
#region <#=code.Escape(entity)#>
    /// <summary>
    /// <#=code.Escape(entity)#> Operation contracts - <#=code.Escape(entity.Name)#> 
    /// </summary>  
    [OperationContract]
    bool Save<#=code.Escape(entity)#>(Dto<#=code.Escape(entity)#> _dto, int racID, string profile);
    [OperationContract]
    bool Delete<#=code.Escape(entity)#>(<#=code.Escape(_keys)#>, int racID, string profile);
    [OperationContract]
    Dto<#=code.Escape(entity)#> Get<#=code.Escape(entity)#>(<#=code.Escape(_keys)#>, string  filterXml, int racID, string profile);
    [OperationContract]
    List<Dto<#=code.Escape(entity)#>> List<#=code.Escape(entityName)#>(string  filterXml, int racID, string profile);   
#endregion
<#+ 
于 2012-10-11T08:46:30.517 に答える