是否存在用于生成 WCF 服务的 T4 模板,以便为自跟踪实体提供获取和保存

本文关键字:跟踪 实体 保存 获取 用于 存在 WCF 服务 模板 T4 是否 | 更新日期: 2023-09-27 18:33:45

我正在 n 层应用程序上使用自跟踪实体。 所以我有一个 WCF 服务,它为客户端提供对数据层的访问,我发现自己实现了许多与从我的模型中获取一些实体(例如 GetOrders(相对应的"相同"函数,并在客户端中更改它们后,执行Save(Order order)Save(TrackableCollection<Orders> orders)操作来保留更改。

我想知道是否存在可以构建基本接口的 T4 模板,其中 Get/Save 用于每个实体的单个和集合以及我的模型中的相应服务实现?

我知道我可以编写自己的 T4 模板来生成此服务,如果有必要,我可能会这样做,但首先我想我会问社区这项工作是否已经存在于互联网上的某个地方,是计划好的,是其他人想要的和/或是一个完全愚蠢的想法。

我确实找到了这个 WCF 数据服务生成器示例,它走在正确的轨道上。

也许那里有更适合在 n 层解决方案中使用 WCF 的 STE 的东西?

是否存在用于生成 WCF 服务的 T4 模板,以便为自跟踪实体提供获取和保存

那里有(更多(,但你总是需要自定义。我已经自定义了在线找到的简单模板,本质上它的作用是创建操作合同和 Web 方法,以便对 win32 客户端进行可见性。以下为摘录:

// 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
<#+