Is there a T4 Template for creating a WCF service to provide Get and Save for self-monitoring objects?

I work with self-control objects in an n-level application. Thus, I have a WCF service that gives the client access to the data layer, and I find that I implement many β€œidentical” functions corresponding to: Retrieving some objects from my model, for example GetOrders, and after changing them in the client, a Save(Order order)or Save(TrackableCollection<Orders> orders)for save changes.

I am wondering if there is a T4 template that could create a basic interface using Get / Save for single and collections of each object and the corresponding service implementation from my model?

I know that I could write my own T4 template to create this service, and I will probably do it if necessary, but at first I thought I would ask the community if this effort already exists somewhere on the Internet, is planned, desirable by others and / or an absolutely stupid idea.

I found this example WCF data generator that looks like on the right track.

Perhaps something more suitable for STE using WCF in an n-level solution?

+3
source share
1 answer

(), . , , , , - 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
<#+ 
+2

All Articles