Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/09/10 17:24:13 (14 years ago)
Author:
gkronber
Message:

Implemented and tested rudimentary WCF service interface on top of the Linq2Sql data access layer. #860

Location:
branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.Services.Deployment.DataAccess
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.Services.Deployment.DataAccess/PluginDescription.cs

    r2742 r2771  
    3434
    3535    [DataMember(Name = "Dependencies")]
    36     private IEnumerable<PluginDescription> dependencies;
    37     public IEnumerable<PluginDescription> Dependencies {
     36    private List<PluginDescription> dependencies;
     37    public List<PluginDescription> Dependencies {
    3838      get { return dependencies; }
    3939    }
     
    5151      this.name = name;
    5252      this.version = version;
    53       this.dependencies = new List<PluginDescription>(dependencies).AsReadOnly();
     53      this.dependencies = new List<PluginDescription>(dependencies); //.AsReadOnly();
    5454      //this.isDatabaseInstance = true;
    5555    }
  • branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.Services.Deployment.DataAccess/PluginStore.cs

    r2766 r2771  
    1010  public class PluginStore {
    1111
    12     private PluginStoreClassesDataContext ctx;
    1312    public PluginStore() {
    14       ctx = new PluginStoreClassesDataContext();
    15     }
    16 
     13    }
     14
     15    #region context creating members
    1716    public IEnumerable<ProductDescription> Products {
    1817      get {
    19         return from p in ctx.Products
    20                let plugins = from pair in ctx.ProductPlugins
    21                              from plugin in ctx.Plugins
    22                              where pair.ProductId == p.Id
    23                              where plugin.Id == pair.PluginId
    24                              select plugin
    25                select MakeProductDescription(p, plugins);
    26       }
    27     }
    28 
    29     private ProductDescription MakeProductDescription(Product p, IQueryable<Plugin> plugins) {
    30       var desc = new ProductDescription(p.Id, p.Name, new Version(p.Version), from plugin in plugins
    31                                                                               select MakePluginDescription(plugin));
    32       return desc;
    33     }
    34 
    35     private PluginDescription MakePluginDescription(Plugin plugin) {
    36       var desc = new PluginDescription(plugin.Id, plugin.Name, new Version(plugin.Version), from dep in GetDependencies(plugin)
    37                                                                                             select MakePluginDescription(dep));
    38       return desc;
    39     }
    40 
    41     private IEnumerable<Plugin> GetDependencies(Plugin plugin) {
    42       return from pair in ctx.Dependencies
    43              from dependency in ctx.Plugins
    44              where pair.PluginId == plugin.Id
    45              where pair.DependencyId == dependency.Id
    46              select dependency;
     18        using (var ctx = new PluginStoreClassesDataContext()) {
     19          return (from p in ctx.Products
     20                  let plugins = from pair in ctx.ProductPlugins
     21                                from plugin in ctx.Plugins
     22                                where pair.ProductId == p.Id
     23                                where plugin.Id == pair.PluginId
     24                                select plugin
     25                  select MakeProductDescription(ctx, p, plugins)).ToList();
     26        }
     27      }
    4728    }
    4829
    4930    public IEnumerable<PluginDescription> Plugins {
    5031      get {
    51         return from plugin in ctx.Plugins
    52                select MakePluginDescription(plugin);
     32        using (var ctx = new PluginStoreClassesDataContext()) {
     33          return (from plugin in ctx.Plugins
     34                  select MakePluginDescription(ctx, plugin)).ToList();
     35        }
    5336      }
    5437    }
    5538
    5639    public byte[] PluginFile(PluginDescription pluginDescription) {
    57       return GetExistingPlugin(pluginDescription.Name, pluginDescription.Version).PluginPackage.Data.ToArray();
     40      using (var ctx = new PluginStoreClassesDataContext()) {
     41        return GetExistingPlugin(ctx, pluginDescription.Name, pluginDescription.Version).PluginPackage.Data.ToArray();
     42      }
    5843    }
    5944
    6045    public void Persist(PluginDescription pluginDescription, byte[] pluginPackage) {
    61       try {
    62         using (var transaction = new TransactionScope()) {
    63           Plugin pluginEntity = InsertOrUpdatePlugin(pluginDescription);
    64           if (pluginEntity.PluginPackage == null) {
    65             // insert
    66             pluginEntity.PluginPackage = MakePluginPackage(pluginDescription, pluginPackage);
    67           } else {
    68             // update
    69             pluginEntity.PluginPackage.Data = pluginPackage;
     46      using (var ctx = new PluginStoreClassesDataContext()) {
     47        try {
     48          using (var transaction = new TransactionScope()) {
     49            Plugin pluginEntity = InsertOrUpdatePlugin(ctx, pluginDescription);
     50            if (pluginEntity.PluginPackage == null) {
     51              // insert
     52              pluginEntity.PluginPackage = MakePluginPackage(pluginDescription, pluginPackage);
     53            } else {
     54              // update
     55              pluginEntity.PluginPackage.Data = pluginPackage;
     56            }
     57            ctx.SubmitChanges();
     58            transaction.Complete();
    7059          }
    71           ctx.SubmitChanges();
    72           transaction.Complete();
    73         }
    74       }
    75       catch (SqlException ex) {
    76         throw new ArgumentException("Something went wrong while trying to persist plugin", ex);
    77       }
    78       catch (InvalidOperationException ex) {
    79         throw new ArgumentException("Something went wrong while trying to persist plugin", ex);
     60        }
     61        catch (SqlException ex) {
     62          throw new ArgumentException("Something went wrong while trying to persist plugin", ex);
     63        }
     64        catch (InvalidOperationException ex) {
     65          throw new ArgumentException("Something went wrong while trying to persist plugin", ex);
     66        }
    8067      }
    8168    }
    8269
    8370    public void Persist(ProductDescription product) {
    84       try {
    85         using (var transaction = new TransactionScope()) {
    86           InsertOrUpdateProduct(product);
    87           foreach (var plugin in product.Plugins) {
    88             InsertOrUpdatePlugin(plugin);
     71      using (var ctx = new PluginStoreClassesDataContext()) {
     72        try {
     73          using (var transaction = new TransactionScope()) {
     74            foreach (var plugin in product.Plugins) {
     75              var pluginEntity = GetExistingPlugin(ctx, plugin.Name, plugin.Version);
     76              UpdatePlugin(ctx, pluginEntity, plugin);
     77            }
     78            InsertOrUpdateProduct(ctx, product);
     79            ctx.SubmitChanges();
     80            transaction.Complete();
    8981          }
    90           ctx.SubmitChanges();
    91           transaction.Complete();
    92         }
    93       }
    94       catch (SqlException ex) {
    95         throw new ArgumentException("Something went wrong while trying to persist product", ex);
    96       }
    97       catch (InvalidOperationException ex) {
    98         throw new ArgumentException("Something went wrong while trying to persist product", ex);
    99       }
    100     }
    101 
    102     private void InsertOrUpdateProduct(ProductDescription product) {
     82        }
     83        catch (SqlException ex) {
     84          throw new ArgumentException("Something went wrong while trying to persist product", ex);
     85        }
     86        catch (InvalidOperationException ex) {
     87          throw new ArgumentException("Something went wrong while trying to persist product", ex);
     88        }
     89      }
     90    }
     91
     92    #endregion
     93
     94    #region insert/update product
     95    private void InsertOrUpdateProduct(PluginStoreClassesDataContext ctx, ProductDescription product) {
    10396      var productEntity = (from p in ctx.Products
    10497                           where p.Name == product.Name
     
    113106      product.Id = productEntity.Id;
    114107
    115       DeleteOldPlugins(productEntity);
     108      DeleteOldPlugins(ctx, productEntity);
    116109
    117110      foreach (var plugin in product.Plugins) {
    118         var existingPlugin = GetExistingPlugin(plugin.Name, plugin.Version);
     111        var existingPlugin = GetExistingPlugin(ctx, plugin.Name, plugin.Version);
    119112        ProductPlugin prodPlugin = new ProductPlugin();
    120113        prodPlugin.PluginId = existingPlugin.Id;
     
    124117    }
    125118
    126     private void DeleteOldPlugins(Product productEntity) {
     119    private void DeleteOldPlugins(PluginStoreClassesDataContext ctx, Product productEntity) {
    127120      var oldPlugins = (from p in ctx.ProductPlugins
    128121                        where p.ProductId == productEntity.Id
     
    131124      ctx.SubmitChanges();
    132125    }
    133 
    134     private Plugin InsertOrUpdatePlugin(PluginDescription pluginDescription) {
     126    #endregion
     127
     128    #region insert/update plugins
     129    private Plugin InsertOrUpdatePlugin(PluginStoreClassesDataContext ctx, PluginDescription pluginDescription) {
    135130      var pluginEntity = (from p in ctx.Plugins
    136131                          where p.Name == pluginDescription.Name
     
    142137        ctx.SubmitChanges();
    143138      }
     139
     140      UpdatePlugin(ctx, pluginEntity, pluginDescription);
     141      return pluginEntity;
     142    }
     143
     144    private void UpdatePlugin(PluginStoreClassesDataContext ctx, Plugin pluginEntity, PluginDescription pluginDescription) {
     145      // delete cached entry
     146      if (pluginDescriptions.ContainsKey(pluginEntity)) pluginDescriptions.Remove(pluginEntity);
     147
    144148      pluginDescription.Id = pluginEntity.Id;
    145149
    146       DeleteOldDependencies(pluginEntity);
     150      DeleteOldDependencies(ctx, pluginEntity);
    147151
    148152      foreach (var dependency in pluginDescription.Dependencies) {
    149         var dependencyEntity = GetExistingPlugin(dependency.Name, dependency.Version);
     153        var dependencyEntity = GetExistingPlugin(ctx, dependency.Name, dependency.Version);
    150154        Dependency d = new Dependency();
    151155        d.PluginId = pluginDescription.Id;
     
    153157        ctx.Dependencies.InsertOnSubmit(d);
    154158      }
    155       return pluginEntity;
    156 
    157     }
    158 
    159     private void DeleteOldDependencies(Plugin pluginEntity) {
     159    }
     160
     161
     162
     163    private void DeleteOldDependencies(PluginStoreClassesDataContext ctx, Plugin pluginEntity) {
    160164      var oldDependencies = (from dep in ctx.Dependencies
    161165                             where dep.PluginId == pluginEntity.Id
     
    165169      ctx.SubmitChanges();
    166170    }
    167 
    168     private Plugin GetExistingPlugin(string name, Version version) {
    169       return (from p in ctx.Plugins
    170               where p.Name == name
    171               where p.Version == version.ToString()
    172               select p).Single();
     171    #endregion
     172
     173    #region product <-> productDescription transformation
     174    private ProductDescription MakeProductDescription(PluginStoreClassesDataContext ctx, Product p, IQueryable<Plugin> plugins) {
     175      var desc = new ProductDescription(p.Id, p.Name, new Version(p.Version), from plugin in plugins
     176                                                                              select MakePluginDescription(ctx, plugin));
     177      return desc;
     178    }
     179    private Product MakeProductFromDescription(ProductDescription desc) {
     180      var product = new Product();
     181      product.Id = desc.Id;
     182      product.Name = desc.Name;
     183      product.Version = desc.Version.ToString();
     184      return product;
     185    }
     186    #endregion
     187
     188    #region plugin <-> pluginDescription transformation
     189    // cache for plugin descriptions
     190    private Dictionary<Plugin, PluginDescription> pluginDescriptions = new Dictionary<Plugin, PluginDescription>();
     191    private PluginDescription MakePluginDescription(PluginStoreClassesDataContext ctx, Plugin plugin) {
     192      if (!pluginDescriptions.ContainsKey(plugin)) {
     193        // no cached description -> create new
     194        var desc = new PluginDescription(plugin.Id, plugin.Name, new Version(plugin.Version), from dep in GetDependencies(ctx, plugin)
     195                                                                                              select MakePluginDescription(ctx, dep));
     196        pluginDescriptions[plugin] = desc;
     197      }
     198      return pluginDescriptions[plugin];
    173199    }
    174200
     
    181207    }
    182208
    183     private Product MakeProductFromDescription(ProductDescription desc) {
    184       var product = new Product();
    185       product.Id = desc.Id;
    186       product.Name = desc.Name;
    187       product.Version = desc.Version.ToString();
    188       return product;
    189     }
    190 
    191209    private PluginPackage MakePluginPackage(PluginDescription pluginDescription, byte[] pluginPackage) {
    192210      var package = new PluginPackage();
     
    196214      return package;
    197215    }
     216
     217    #endregion
     218
     219    #region helper queries
     220    private Plugin GetExistingPlugin(PluginStoreClassesDataContext ctx, string name, Version version) {
     221      return (from p in ctx.Plugins
     222              where p.Name == name
     223              where p.Version == version.ToString()
     224              select p).Single();
     225    }
     226
     227    private IEnumerable<Plugin> GetDependencies(PluginStoreClassesDataContext ctx, Plugin plugin) {
     228      return from pair in ctx.Dependencies
     229             from dependency in ctx.Plugins
     230             where pair.PluginId == plugin.Id
     231             where pair.DependencyId == dependency.Id
     232             select dependency;
     233    }
     234    #endregion
    198235  }
    199236}
Note: See TracChangeset for help on using the changeset viewer.