Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/25/12 14:12:47 (12 years ago)
Author:
fschoepp
Message:

#1888:

  • Added IConnectionProvider interface + implementation based on jheinzelreiters transaction management
  • Fixed transaction being promoted to dtc; transactions enabled again
  • Changed HiveDao: transactions are managed properly
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OaaS/HeuristicLab.Services.Hive/3.3/HiveDao.cs

    r7916 r8326  
    2525using System.Linq.Expressions;
    2626using DT = HeuristicLab.Services.Hive.DataTransfer;
     27using System.ServiceModel;
     28using HeuristicLab.Services.Hive.Interfaces;
     29using System.Data.Common;
    2730
    2831namespace HeuristicLab.Services.Hive.DataAccess {
    2932  public class HiveDao : IHiveDao {
    30     public static HiveDataContext CreateContext(bool longRunning = false) {
    31       var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
     33   
     34    public HiveDataContext CreateContext(bool longRunning = false) {
     35      //var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
     36      var context = new HiveDataContext(provider.GetOpenConnection(Settings.Default.HiveConnectionStringName));     
    3237      if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
    3338      return context;
    3439    }
    3540
    36     public HiveDao() { }
     41    private IConnectionProvider provider;
     42
     43    public HiveDao(IConnectionProvider provider) { this.provider = provider; }
     44
     45    private delegate T ExecuteWithContextDelegate<T>(HiveDataContext context);
     46    private delegate void ExecuteWithContextDelegate(HiveDataContext context);
     47
     48    private void ExecuteWithContext(ExecuteWithContextDelegate call) {
     49      DbConnection con = null;
     50      try {
     51        using (var db = CreateContext()) {
     52          con = db.Connection;
     53          call(db);
     54        }
     55      }
     56      finally {
     57        provider.ReleaseConnection(con);
     58      }
     59    }
     60
     61    private T ExecuteWithContext<T>(ExecuteWithContextDelegate<T> call) {
     62      DbConnection con = null;
     63      try {
     64        using (var db = CreateContext()) {
     65          con = db.Connection;
     66          T result = call(db);
     67          return result;
     68        }
     69      }
     70      finally {
     71        provider.ReleaseConnection(con);
     72      }
     73    }
    3774
    3875    #region Task Methods
    3976    public DT.Task GetTask(Guid id) {
    40       using (var db = CreateContext()) {
     77      return ExecuteWithContext<DT.Task>((db) => {
    4178        return DT.Convert.ToDto(db.Tasks.SingleOrDefault(x => x.TaskId == id));
    42       }
     79      });
    4380    }
    4481
    4582    public IEnumerable<DT.Task> GetTasks(Expression<Func<Task, bool>> predicate) {
    46       using (var db = CreateContext()) {
     83      return ExecuteWithContext<IEnumerable<DT.Task>>((db) => {
    4784        return db.Tasks.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    48       }
     85      });
    4986    }
    5087
    5188    public Guid AddTask(DT.Task dto) {
    52       using (var db = CreateContext()) {
     89      return ExecuteWithContext<Guid>((db) => {
    5390        var entity = DT.Convert.ToEntity(dto);
    5491        db.Tasks.InsertOnSubmit(entity);
     
    5996        db.SubmitChanges();
    6097        return entity.TaskId;
    61       }
     98      });
    6299    }
    63100
    64101    public void UpdateTask(DT.Task dto) {
    65       using (var db = CreateContext()) {
     102      ExecuteWithContext((db) => {
    66103        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id);
    67104        if (entity == null) db.Tasks.InsertOnSubmit(DT.Convert.ToEntity(dto));
     
    73110        }
    74111        db.SubmitChanges();
    75       }
     112      });
    76113    }
    77114
    78115    public void DeleteTask(Guid id) {
    79       using (var db = CreateContext()) {
     116      ExecuteWithContext((db) => {
    80117        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == id);
    81118        if (entity != null) db.Tasks.DeleteOnSubmit(entity);
    82119        db.SubmitChanges(); // taskData and child tasks are deleted by db-trigger
    83       }
     120      });
    84121    }
    85122
     
    92129    /// <returns></returns>
    93130    public IEnumerable<DT.Task> GetParentTasks(IEnumerable<Guid> resourceIds, int count, bool finished) {
    94       using (var db = CreateContext()) {
     131      return ExecuteWithContext<IEnumerable<DT.Task>>((db) => {
    95132        var query = from ar in db.AssignedResources
    96133                    where resourceIds.Contains(ar.ResourceId)
     
    109146                    select DT.Convert.ToDto(ar.Task);
    110147        return count == 0 ? query.ToArray() : query.Take(count).ToArray();
    111       }
     148      });
    112149    }
    113150
    114151    public IEnumerable<DT.Task> GetWaitingTasks(DT.Slave slave, int count) {
    115       using (var db = CreateContext()) {
     152      return ExecuteWithContext<IEnumerable<DT.Task>>((db) => {
    116153        var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
    117154        //Originally we checked here if there are parent tasks which should be calculated (with GetParentTasks(resourceIds, count, false);).
     
    129166        var waitingTasks = (count == 0 ? query : query.Take(count)).ToArray();
    130167        return waitingTasks;
    131       }
     168      });
    132169    }
    133170
    134171    public DT.Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
    135       using (var db = CreateContext()) {
     172      return ExecuteWithContext<DT.Task>((db) => {
    136173        var job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
    137174        job.State = taskState;
     
    147184        job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
    148185        return DT.Convert.ToDto(job);
    149       }
     186      });
    150187    }
    151188    #endregion
     
    153190    #region TaskData Methods
    154191    public DT.TaskData GetTaskData(Guid id) {
    155       using (var db = CreateContext(true)) {
     192      return ExecuteWithContext<DT.TaskData>((db) => {
    156193        return DT.Convert.ToDto(db.TaskDatas.SingleOrDefault(x => x.TaskId == id));
    157       }
     194      });
    158195    }
    159196
    160197    public IEnumerable<DT.TaskData> GetTaskDatas(Expression<Func<TaskData, bool>> predicate) {
    161       using (var db = CreateContext(true)) {
     198      return ExecuteWithContext<IEnumerable<DT.TaskData>>((db) => {
    162199        return db.TaskDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    163       }
     200      });
    164201    }
    165202
    166203    public Guid AddTaskData(DT.TaskData dto) {
    167       using (var db = CreateContext(true)) {
     204      return ExecuteWithContext<Guid>((db) => {
    168205        var entity = DT.Convert.ToEntity(dto);
    169206        db.TaskDatas.InsertOnSubmit(entity);
    170207        db.SubmitChanges();
    171208        return entity.TaskId;
    172       }
     209      });
    173210    }
    174211
    175212    public void UpdateTaskData(DT.TaskData dto) {
    176       using (var db = CreateContext(true)) {
     213      ExecuteWithContext((db) => {
    177214        var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == dto.TaskId);
    178215        if (entity == null) db.TaskDatas.InsertOnSubmit(DT.Convert.ToEntity(dto));
    179216        else DT.Convert.ToEntity(dto, entity);
    180217        db.SubmitChanges();
    181       }
     218      });
    182219    }
    183220
    184221    public void DeleteTaskData(Guid id) {
    185       using (var db = CreateContext()) {
     222      ExecuteWithContext((db) => {
    186223        var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
    187224        if (entity != null) db.TaskDatas.DeleteOnSubmit(entity);
    188225        db.SubmitChanges();
    189       }
     226      });
    190227    }
    191228    #endregion
     
    193230    #region StateLog Methods
    194231    public DT.StateLog GetStateLog(Guid id) {
    195       using (var db = CreateContext()) {
     232      return ExecuteWithContext<DT.StateLog>((db) => {
    196233        return DT.Convert.ToDto(db.StateLogs.SingleOrDefault(x => x.StateLogId == id));
    197       }
     234      });
    198235    }
    199236
    200237    public IEnumerable<DT.StateLog> GetStateLogs(Expression<Func<StateLog, bool>> predicate) {
    201       using (var db = CreateContext()) {
     238      return ExecuteWithContext<IEnumerable<DT.StateLog>>((db) => {
    202239        return db.StateLogs.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    203       }
     240      });
    204241    }
    205242
    206243    public Guid AddStateLog(DT.StateLog dto) {
    207       using (var db = CreateContext()) {
     244      return ExecuteWithContext<Guid>((db) => {
    208245        var entity = DT.Convert.ToEntity(dto);
    209246        db.StateLogs.InsertOnSubmit(entity);
    210247        db.SubmitChanges();
    211248        return entity.StateLogId;
    212       }
     249      });
    213250    }
    214251
    215252    public void UpdateStateLog(DT.StateLog dto) {
    216       using (var db = CreateContext()) {
     253      ExecuteWithContext((db) => {
    217254        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == dto.Id);
    218255        if (entity == null) db.StateLogs.InsertOnSubmit(DT.Convert.ToEntity(dto));
    219256        else DT.Convert.ToEntity(dto, entity);
    220257        db.SubmitChanges();
    221       }
     258      });
    222259    }
    223260
    224261    public void DeleteStateLog(Guid id) {
    225       using (var db = CreateContext()) {
     262      ExecuteWithContext((db) => {
    226263        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == id);
    227264        if (entity != null) db.StateLogs.DeleteOnSubmit(entity);
    228265        db.SubmitChanges();
    229       }
     266      });
    230267    }
    231268    #endregion
     
    233270    #region Job Methods
    234271    public DT.Job GetJob(Guid id) {
    235       using (var db = CreateContext()) {
     272      return ExecuteWithContext<DT.Job>((db) => {
    236273        return AddStatsToJob(db, DT.Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id)));
    237       }
     274      });
    238275    }
    239276
     
    250287
    251288    public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
    252       using (var db = CreateContext()) {
     289      return ExecuteWithContext<IEnumerable<DT.Job>>((db) => {
    253290        return db.Jobs.Where(predicate).Select(x => AddStatsToJob(db, DT.Convert.ToDto(x))).ToArray();
    254       }
     291      });
    255292    }
    256293
    257294    public Guid AddJob(DT.Job dto) {
    258       using (var db = CreateContext()) {
     295      return ExecuteWithContext<Guid>((db) => {
    259296        var entity = DT.Convert.ToEntity(dto);
    260297        db.Jobs.InsertOnSubmit(entity);
    261298        db.SubmitChanges();
    262299        return entity.JobId;
    263       }
     300      });
    264301    }
    265302
    266303    public void UpdateJob(DT.Job dto) {
    267       using (var db = CreateContext()) {
     304      ExecuteWithContext((db) => {
    268305        var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
    269306        if (entity == null) db.Jobs.InsertOnSubmit(DT.Convert.ToEntity(dto));
    270307        else DT.Convert.ToEntity(dto, entity);
    271308        db.SubmitChanges();
    272       }
     309      });
    273310    }
    274311
    275312    public void DeleteJob(Guid id) {
    276       using (var db = CreateContext()) {
     313      ExecuteWithContext((db) => {
    277314        var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
    278315        if (entity != null) db.Jobs.DeleteOnSubmit(entity);
    279316        db.SubmitChanges();
    280       }
     317      });
    281318    }
    282319    #endregion
     
    284321    #region JobPermission Methods
    285322    public DT.JobPermission GetJobPermission(Guid jobId, Guid grantedUserId) {
    286       using (var db = CreateContext()) {
     323      return ExecuteWithContext<DT.JobPermission>((db) => {
    287324        return DT.Convert.ToDto(db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId));
    288       }
     325      });
    289326    }
    290327
    291328    public IEnumerable<DT.JobPermission> GetJobPermissions(Expression<Func<JobPermission, bool>> predicate) {
    292       using (var db = CreateContext()) {
     329      return ExecuteWithContext<IEnumerable<DT.JobPermission>>((db) => {
    293330        return db.JobPermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    294       }
     331      });
    295332    }
    296333
    297334    public void AddJobPermission(DT.JobPermission dto) {
    298       using (var db = CreateContext()) {
     335      ExecuteWithContext((db) => {
    299336        var entity = DT.Convert.ToEntity(dto);
    300337        db.JobPermissions.InsertOnSubmit(entity);
    301338        db.SubmitChanges();
    302       }
     339      });
    303340    }
    304341
    305342    public void UpdateJobPermission(DT.JobPermission dto) {
    306       using (var db = CreateContext()) {
     343      ExecuteWithContext((db) => {
    307344        var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == dto.JobId && x.GrantedUserId == dto.GrantedUserId);
    308345        if (entity == null) db.JobPermissions.InsertOnSubmit(DT.Convert.ToEntity(dto));
    309346        else DT.Convert.ToEntity(dto, entity);
    310347        db.SubmitChanges();
    311       }
     348      });
    312349    }
    313350
    314351    public void DeleteJobPermission(Guid jobId, Guid grantedUserId) {
    315       using (var db = CreateContext()) {
     352      ExecuteWithContext((db) => {
    316353        var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
    317354        if (entity != null) db.JobPermissions.DeleteOnSubmit(entity);
    318355        db.SubmitChanges();
    319       }
     356      });
    320357    }
    321358
     
    324361    /// </summary>
    325362    public void SetJobPermission(Guid jobId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
    326       using (var db = CreateContext()) {
     363      ExecuteWithContext((db) => {
    327364        JobPermission jobPermission = db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
    328365        if (jobPermission != null) {
     
    330367            // not allowed, delete
    331368            db.JobPermissions.DeleteOnSubmit(jobPermission);
    332           } else {
     369          }
     370          else {
    333371            // update
    334372            jobPermission.Permission = permission;
    335373            jobPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored
    336374          }
    337         } else {
     375        }
     376        else {
    338377          // insert
    339378          if (permission != Permission.NotAllowed) {
     
    343382        }
    344383        db.SubmitChanges();
    345       }
     384      });
    346385    }
    347386    #endregion
     
    349388    #region Plugin Methods
    350389    public DT.Plugin GetPlugin(Guid id) {
    351       using (var db = CreateContext()) {
     390      return ExecuteWithContext<DT.Plugin>((db) => {
    352391        return DT.Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
    353       }
     392      });
    354393    }
    355394
    356395    public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
    357       using (var db = CreateContext()) {
     396      return ExecuteWithContext<IEnumerable<DT.Plugin>>((db) => {
    358397        return db.Plugins.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    359       }
     398      });
    360399    }
    361400
    362401    public Guid AddPlugin(DT.Plugin dto) {
    363       using (var db = CreateContext()) {
     402      return ExecuteWithContext<Guid>((db) => {
    364403        var entity = DT.Convert.ToEntity(dto);
    365404        db.Plugins.InsertOnSubmit(entity);
    366405        db.SubmitChanges();
    367406        return entity.PluginId;
    368       }
     407      });
    369408    }
    370409
    371410    public void UpdatePlugin(DT.Plugin dto) {
    372       using (var db = CreateContext()) {
     411      ExecuteWithContext((db) => {
    373412        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
    374413        if (entity == null) db.Plugins.InsertOnSubmit(DT.Convert.ToEntity(dto));
    375414        else DT.Convert.ToEntity(dto, entity);
    376415        db.SubmitChanges();
    377       }
     416      });
    378417    }
    379418
    380419    public void DeletePlugin(Guid id) {
    381       using (var db = CreateContext()) {
     420      ExecuteWithContext((db) => {
    382421        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
    383422        if (entity != null) db.Plugins.DeleteOnSubmit(entity);
    384423        db.SubmitChanges();
    385       }
     424      });
    386425    }
    387426    #endregion
     
    389428    #region PluginData Methods
    390429    public DT.PluginData GetPluginData(Guid id) {
    391       using (var db = CreateContext()) {
     430      return ExecuteWithContext<DT.PluginData>((db) => {
    392431        return DT.Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
    393       }
     432      });
    394433    }
    395434
    396435    public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
    397       using (var db = CreateContext()) {
     436      return ExecuteWithContext <IEnumerable<DT.PluginData>>((db) => {
    398437        return db.PluginDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    399       }
     438      });
    400439    }
    401440
    402441    public Guid AddPluginData(DT.PluginData dto) {
    403       using (var db = CreateContext()) {
     442      return ExecuteWithContext<Guid>((db) => {
    404443        var entity = DT.Convert.ToEntity(dto);
    405444        db.PluginDatas.InsertOnSubmit(entity);
    406445        db.SubmitChanges();
    407446        return entity.PluginDataId;
    408       }
     447      });
    409448    }
    410449
    411450    public void UpdatePluginData(DT.PluginData dto) {
    412       using (var db = CreateContext()) {
     451      ExecuteWithContext((db) => {
    413452        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
    414453        if (entity == null) db.PluginDatas.InsertOnSubmit(DT.Convert.ToEntity(dto));
    415454        else DT.Convert.ToEntity(dto, entity);
    416455        db.SubmitChanges();
    417       }
     456      });
    418457    }
    419458
    420459    public void DeletePluginData(Guid id) {
    421       using (var db = CreateContext()) {
     460      ExecuteWithContext((db) => {
    422461        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id);
    423462        if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
    424463        db.SubmitChanges();
    425       }
     464      });
    426465    }
    427466    #endregion
     
    429468    #region Slave Methods
    430469    public DT.Slave GetSlave(Guid id) {
    431       using (var db = CreateContext()) {
     470      return ExecuteWithContext<DT.Slave>((db) => {
    432471        return DT.Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
    433       }
     472      });
    434473    }
    435474
    436475    public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
    437       using (var db = CreateContext()) {
     476      return ExecuteWithContext<IEnumerable<DT.Slave>>((db) => {
    438477        return db.Resources.OfType<Slave>().Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    439       }
     478      });
    440479    }
    441480
    442481    public Guid AddSlave(DT.Slave dto) {
    443       using (var db = CreateContext()) {
     482      return ExecuteWithContext<Guid>((db) => {
    444483        var entity = DT.Convert.ToEntity(dto);
    445484        db.Resources.InsertOnSubmit(entity);
    446485        db.SubmitChanges();
    447486        return entity.ResourceId;
    448       }
     487      });
    449488    }
    450489
    451490    public void UpdateSlave(DT.Slave dto) {
    452       using (var db = CreateContext()) {
     491      ExecuteWithContext((db) => {
    453492        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
    454493        if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
    455494        else DT.Convert.ToEntity(dto, entity);
    456495        db.SubmitChanges();
    457       }
     496      });
    458497    }
    459498
    460499    public void DeleteSlave(Guid id) {
    461       using (var db = CreateContext()) {
     500      ExecuteWithContext((db) => {
    462501        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
    463502        if (entity != null) db.Resources.DeleteOnSubmit(entity);
    464503        db.SubmitChanges();
    465       }
     504      });
    466505    }
    467506    #endregion
     
    469508    #region SlaveGroup Methods
    470509    public DT.SlaveGroup GetSlaveGroup(Guid id) {
    471       using (var db = CreateContext()) {
     510      return ExecuteWithContext<DT.SlaveGroup>((db) => {
    472511        return DT.Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
    473       }
     512      });
    474513    }
    475514
    476515    public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
    477       using (var db = CreateContext()) {
     516      return ExecuteWithContext<IEnumerable<DT.SlaveGroup>>((db) => {
    478517        return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    479       }
     518      });
    480519    }
    481520
    482521    public Guid AddSlaveGroup(DT.SlaveGroup dto) {
    483       using (var db = CreateContext()) {
     522      return ExecuteWithContext<Guid>((db) => {
    484523        if (dto.Id == Guid.Empty)
    485524          dto.Id = Guid.NewGuid();
     
    488527        db.SubmitChanges();
    489528        return entity.ResourceId;
    490       }
     529      });
    491530    }
    492531
    493532    public void UpdateSlaveGroup(DT.SlaveGroup dto) {
    494       using (var db = CreateContext()) {
     533      ExecuteWithContext((db) => {
    495534        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
    496535        if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
    497536        else DT.Convert.ToEntity(dto, entity);
    498537        db.SubmitChanges();
    499       }
     538      });
    500539    }
    501540
    502541    public void DeleteSlaveGroup(Guid id) {
    503       using (var db = CreateContext()) {
     542      ExecuteWithContext((db) => {
    504543        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
    505544        if (entity != null) {
     
    510549        }
    511550        db.SubmitChanges();
    512       }
     551      });
    513552    }
    514553    #endregion
     
    516555    #region Resource Methods
    517556    public DT.Resource GetResource(Guid id) {
    518       using (var db = CreateContext()) {
     557      return ExecuteWithContext<DT.Resource>((db) => {
    519558        return DT.Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
    520       }
     559      });
    521560    }
    522561
    523562    public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
    524       using (var db = CreateContext()) {
     563      return ExecuteWithContext<IEnumerable<DT.Resource>>((db) => {
    525564        return db.Resources.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    526       }
     565      });
    527566    }
    528567
    529568    public Guid AddResource(DT.Resource dto) {
    530       using (var db = CreateContext()) {
     569      return ExecuteWithContext<Guid>((db) => {
    531570        var entity = DT.Convert.ToEntity(dto);
    532571        db.Resources.InsertOnSubmit(entity);
    533572        db.SubmitChanges();
    534573        return entity.ResourceId;
    535       }
     574      });
    536575    }
    537576
    538577    public void UpdateResource(DT.Resource dto) {
    539       using (var db = CreateContext()) {
     578      ExecuteWithContext((db) => {
    540579        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
    541580        if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
    542581        else DT.Convert.ToEntity(dto, entity);
    543582        db.SubmitChanges();
    544       }
     583      });
    545584    }
    546585
    547586    public void DeleteResource(Guid id) {
    548       using (var db = CreateContext()) {
     587      ExecuteWithContext((db) => {
    549588        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
    550589        if (entity != null) db.Resources.DeleteOnSubmit(entity);
    551590        db.SubmitChanges();
    552       }
     591      });
    553592    }
    554593
    555594    public void AssignJobToResource(Guid jobId, Guid resourceId) {
    556       using (var db = CreateContext()) {
     595      ExecuteWithContext((db) => {
    557596        var job = db.Tasks.Where(x => x.TaskId == jobId).Single();
    558597        job.AssignedResources.Add(new AssignedResource() { TaskId = jobId, ResourceId = resourceId });
    559598        db.SubmitChanges();
    560       }
     599      });
    561600    }
    562601
    563602    public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
    564       using (var db = CreateContext()) {
     603      return ExecuteWithContext <IEnumerable<DT.Resource>>((db) => {
    565604        var job = db.Tasks.Where(x => x.TaskId == jobId).Single();
    566605        return job.AssignedResources.Select(x => DT.Convert.ToDto(x.Resource)).ToArray();
    567       }
     606      });
    568607    }
    569608
     
    572611    /// </summary>
    573612    public IEnumerable<DT.Resource> GetParentResources(Guid resourceId) {
    574       using (var db = CreateContext()) {
     613      return ExecuteWithContext<IEnumerable<DT.Resource>>((db) => {
    575614        var resources = new List<Resource>();
    576615        CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
    577616        return resources.Select(r => DT.Convert.ToDto(r)).ToArray();
    578       }
     617      });
    579618    }
    580619
     
    589628    /// </summary>
    590629    public IEnumerable<DT.Resource> GetChildResources(Guid resourceId) {
    591       using (var db = CreateContext()) {
     630      return ExecuteWithContext<IEnumerable<DT.Resource>>((db) => {
    592631        var childs = new List<DT.Resource>();
    593632        foreach (var child in db.Resources.Where(x => x.ParentResourceId == resourceId)) {
     
    596635        }
    597636        return childs;
    598       }
     637      });
    599638    }
    600639
    601640    public IEnumerable<DT.Task> GetJobsByResourceId(Guid resourceId) {
    602       using (var db = CreateContext()) {
     641      return ExecuteWithContext<IEnumerable<DT.Task>>((db) => {
    603642        var resources = GetChildResources(resourceId).Select(x => x.Id).ToList();
    604643        resources.Add(resourceId);
     
    609648          resources.Contains(j.StateLogs.OrderByDescending(x => x.DateTime).First().SlaveId.Value));
    610649        return jobs.Select(j => DT.Convert.ToDto(j)).ToArray();
    611       }
     650      });
    612651    }
    613652    #endregion
     
    615654    #region ResourcePermission Methods
    616655    public DT.ResourcePermission GetResourcePermission(Guid resourceId, Guid grantedUserId) {
    617       using (var db = CreateContext()) {
     656      return ExecuteWithContext <DT.ResourcePermission> ((db) => {
    618657        return DT.Convert.ToDto(db.ResourcePermissions.SingleOrDefault(x => x.ResourceId == resourceId && x.GrantedUserId == grantedUserId));
    619       }
     658      });
    620659    }
    621660
    622661    public IEnumerable<DT.ResourcePermission> GetResourcePermissions(Expression<Func<ResourcePermission, bool>> predicate) {
    623       using (var db = CreateContext()) {
     662      return ExecuteWithContext<IEnumerable<DT.ResourcePermission>>((db) => {
    624663        return db.ResourcePermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    625       }
     664      });
    626665    }
    627666
    628667    public void AddResourcePermission(DT.ResourcePermission dto) {
    629       using (var db = CreateContext()) {
     668      ExecuteWithContext((db) => {
    630669        var entity = db.ResourcePermissions.SingleOrDefault(x => x.ResourceId == dto.ResourceId && x.GrantedUserId == dto.GrantedUserId);
    631670        if (entity == null) { db.ResourcePermissions.InsertOnSubmit(DT.Convert.ToEntity(dto)); db.SubmitChanges(); }
    632       }
     671      });
    633672    }
    634673
    635674    public void UpdateResourcePermission(DT.ResourcePermission dto) {
    636       using (var db = CreateContext()) {
     675      ExecuteWithContext((db) => {
    637676        var entity = db.ResourcePermissions.FirstOrDefault(x => x.ResourceId == dto.ResourceId && x.GrantedUserId == dto.GrantedUserId);
    638677        if (entity == null) db.ResourcePermissions.InsertOnSubmit(DT.Convert.ToEntity(dto));
    639678        else DT.Convert.ToEntity(dto, entity);
    640679        db.SubmitChanges();
    641       }
     680      });
    642681    }
    643682
    644683    public void DeleteResourcePermission(Guid resourceId, Guid grantedUserId) {
    645       using (var db = CreateContext()) {
     684      ExecuteWithContext((db) => {
    646685        var entity = db.ResourcePermissions.FirstOrDefault(x => x.ResourceId == resourceId && x.GrantedUserId == grantedUserId);
    647686        if (entity != null) db.ResourcePermissions.DeleteOnSubmit(entity);
    648687        db.SubmitChanges();
    649       }
     688      });
    650689    }
    651690    #endregion
     
    653692    #region Authorization Methods
    654693    public Permission GetPermissionForTask(Guid taskId, Guid userId) {
    655       using (var db = CreateContext()) {
     694      return ExecuteWithContext<Permission>((db) => {
    656695        return GetPermissionForJob(GetJobForTask(taskId), userId);
    657       }
     696      });
    658697    }
    659698
    660699    public Permission GetPermissionForJob(Guid jobId, Guid userId) {
    661       using (var db = CreateContext()) {
     700      return ExecuteWithContext<Permission>((db) => {
    662701        Job job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
    663702        if (job == null) return Permission.NotAllowed;
     
    665704        JobPermission permission = db.JobPermissions.SingleOrDefault(p => p.JobId == jobId && p.GrantedUserId == userId);
    666705        return permission != null ? permission.Permission : Permission.NotAllowed;
    667       }
     706      });
    668707    }
    669708
    670709    public Guid GetJobForTask(Guid taskId) {
    671       using (var db = CreateContext()) {
     710      return ExecuteWithContext<Guid>((db) => {
    672711        return db.Tasks.Single(j => j.TaskId == taskId).JobId;
    673       }
     712      });
    674713    }
    675714    #endregion
     
    677716    #region Lifecycle Methods
    678717    public DateTime GetLastCleanup() {
    679       using (var db = CreateContext()) {
     718      return ExecuteWithContext<DateTime>((db) => {
    680719        var entity = db.Lifecycles.SingleOrDefault();
    681720        return entity != null ? entity.LastCleanup : DateTime.MinValue;
    682       }
     721      });
    683722    }
    684723
    685724    public void SetLastCleanup(DateTime datetime) {
    686       using (var db = CreateContext()) {
     725      ExecuteWithContext((db) => {
    687726        var entity = db.Lifecycles.SingleOrDefault();
    688727        if (entity != null) {
    689728          entity.LastCleanup = datetime;
    690         } else {
     729        }
     730        else {
    691731          entity = new Lifecycle();
    692732          entity.LifecycleId = 0; // always only one entry with ID:0
     
    695735        }
    696736        db.SubmitChanges();
    697       }
     737      });
    698738    }
    699739    #endregion
     
    701741    #region Downtime Methods
    702742    public DT.Downtime GetDowntime(Guid id) {
    703       using (var db = CreateContext()) {
     743      return ExecuteWithContext<DT.Downtime>((db) => {
    704744        return DT.Convert.ToDto(db.Downtimes.SingleOrDefault(x => x.DowntimeId == id));
    705       }
     745      });
    706746    }
    707747
    708748    public IEnumerable<DT.Downtime> GetDowntimes(Expression<Func<Downtime, bool>> predicate) {
    709       using (var db = CreateContext()) {
     749      return ExecuteWithContext<IEnumerable<DT.Downtime>>((db) => {
    710750        return db.Downtimes.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    711       }
     751      });
    712752    }
    713753
    714754    public Guid AddDowntime(DT.Downtime dto) {
    715       using (var db = CreateContext()) {
     755      return ExecuteWithContext<Guid>((db) => {
    716756        var entity = DT.Convert.ToEntity(dto);
    717757        db.Downtimes.InsertOnSubmit(entity);
    718758        db.SubmitChanges();
    719759        return entity.DowntimeId;
    720       }
     760      });
    721761    }
    722762
    723763    public void UpdateDowntime(DT.Downtime dto) {
    724       using (var db = CreateContext()) {
     764      ExecuteWithContext((db) => {
    725765        var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == dto.Id);
    726766        if (entity == null) db.Downtimes.InsertOnSubmit(DT.Convert.ToEntity(dto));
    727767        else DT.Convert.ToEntity(dto, entity);
    728768        db.SubmitChanges();
    729       }
     769      });
    730770    }
    731771
    732772    public void DeleteDowntime(Guid id) {
    733       using (var db = CreateContext()) {
     773      ExecuteWithContext((db) => {
    734774        var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == id);
    735775        if (entity != null) db.Downtimes.DeleteOnSubmit(entity);
    736776        db.SubmitChanges();
    737       }
     777      });
    738778    }
    739779    #endregion
     
    741781    #region Statistics Methods
    742782    public DT.Statistics GetStatistic(Guid id) {
    743       using (var db = CreateContext()) {
     783      return ExecuteWithContext<DT.Statistics>((db) => {
    744784        return DT.Convert.ToDto(db.Statistics.SingleOrDefault(x => x.StatisticsId == id));
    745       }
     785      });
    746786    }
    747787
    748788    public IEnumerable<DT.Statistics> GetStatistics(Expression<Func<Statistics, bool>> predicate) {
    749       using (var db = CreateContext()) {
     789      return ExecuteWithContext<IEnumerable<DT.Statistics>>((db) => {
    750790        return db.Statistics.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
    751       }
     791      });
    752792    }
    753793
    754794    public Guid AddStatistics(DT.Statistics dto) {
    755       using (var db = CreateContext()) {
     795      return ExecuteWithContext<Guid>((db) => {
    756796        var entity = DT.Convert.ToEntity(dto);
    757797        db.Statistics.InsertOnSubmit(entity);
     
    769809        db.SubmitChanges();
    770810        return entity.StatisticsId;
    771       }
     811      });
    772812    }
    773813
    774814    public void DeleteStatistics(Guid id) {
    775       using (var db = CreateContext()) {
     815      ExecuteWithContext((db) => {
    776816        var entity = db.Statistics.FirstOrDefault(x => x.StatisticsId == id);
    777817        if (entity != null) db.Statistics.DeleteOnSubmit(entity);
    778818        db.SubmitChanges();
    779       }
     819      });
    780820    }
    781821
    782822    public List<DT.UserStatistics> GetUserStatistics() {
    783       using (var db = CreateContext()) {
     823      return ExecuteWithContext<List<DT.UserStatistics>>((db) => {
    784824        var userStats = new Dictionary<Guid, DT.UserStatistics>();
    785825
     
    853893
    854894        return userStats.Values.ToList();
    855       }
     895      });
    856896    }
    857897    #endregion
Note: See TracChangeset for help on using the changeset viewer.