Skip to content

Commit 7ed9e3f

Browse files
committed
DRYout Web API and Repositories
1 parent 7392d7b commit 7ed9e3f

20 files changed

+365
-321
lines changed

IntegrationEngine.ConsoleHost/Program.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ public class Program
1111
public static EngineHost EngineHosts { get; set; }
1212
public static void Main(string[] args)
1313
{
14-
if (!Environment.UserInteractive)
15-
{
16-
// Set current working directory as services use the system directory by default.
17-
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
18-
using (var service = new Service())
19-
ServiceBase.Run(service);
20-
}
21-
else
22-
{
23-
Start(args);
24-
Console.WriteLine("Press any key to stop...");
25-
Console.ReadLine();
26-
Stop();
27-
}
14+
Start(args);
15+
Console.WriteLine("Press any key to stop...");
16+
Console.ReadLine();
17+
Stop();
18+
19+
// if (!Environment.UserInteractive)
20+
// {
21+
// // Set current working directory as services use the system directory by default.
22+
// Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
23+
// using (var service = new Service())
24+
// ServiceBase.Run(service);
25+
// }
26+
// else
27+
// {
28+
// Start(args);
29+
// Console.WriteLine("Press any key to stop...");
30+
// Console.ReadLine();
31+
// Stop();
32+
// }
2833
}
2934

3035
private static void Start(string[] args)

IntegrationEngine.Core/IntegrationEngine.Core.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@
9696
<Compile Include="Reports\IReport.cs" />
9797
<Compile Include="R\RScriptRunner.cs" />
9898
<Compile Include="Storage\DatabaseInitializer.cs" />
99-
<Compile Include="Storage\ESRepository.cs" />
10099
<Compile Include="Storage\IntegrationEngineContext.cs" />
101100
<Compile Include="Storage\IntegrationEngineDbConfiguration.cs" />
102101
<Compile Include="Storage\IRepository.cs" />
103102
<Compile Include="Storage\Repository.cs" />
104103
<Compile Include="Jobs\IElasticsearchJob.cs" />
104+
<Compile Include="Storage\IElasticsearchRepository.cs" />
105+
<Compile Include="Storage\ElasticsearchRepository.cs" />
106+
<Compile Include="Storage\IDatabaseRepository.cs" />
105107
</ItemGroup>
106108
<ItemGroup>
107109
<None Include="App.config" />

IntegrationEngine.Core/Storage/ESRepository.cs

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Common.Logging;
2+
using IntegrationEngine.Model;
3+
using Nest;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace IntegrationEngine.Core.Storage
9+
{
10+
public class ElasticsearchRepository : IElasticsearchRepository
11+
{
12+
public IElasticClient ElasticClient { get; set; }
13+
public ILog Log { get; set; }
14+
15+
public ElasticsearchRepository()
16+
{
17+
}
18+
19+
public IEnumerable<TItem> SelectAll<TItem>() where TItem : class, IHasStringId
20+
{
21+
var response = ElasticClient.Search<TItem>(x => x);
22+
return response.Hits.Select(h => {
23+
h.Source.Id = h.Id;
24+
return h.Source;
25+
}).ToList();
26+
}
27+
28+
public TItem SelectById<TItem>(object id) where TItem : class, IHasStringId
29+
{
30+
var response = ElasticClient.Get<TItem>(x => x.Id(id.ToString()));
31+
if (response.Source == null)
32+
return null;
33+
var item = response.Source;
34+
item.Id = response.Id;
35+
return item;
36+
}
37+
38+
public TItem Insert<TItem>(TItem item) where TItem : class, IHasStringId
39+
{
40+
return SelectById<TItem>(ElasticClient.Index<TItem>(item).Id);
41+
}
42+
43+
public TItem Update<TItem>(TItem item) where TItem : class, IHasStringId
44+
{
45+
var response = ElasticClient.Update<TItem, object>(x => x
46+
.Id(item.Id)
47+
.Doc(item)
48+
);
49+
return response as TItem;
50+
}
51+
52+
public void Delete<TItem>(object id) where TItem : class
53+
{
54+
ElasticClient.Delete<TItem>(x => x.Id(id.ToString()));
55+
}
56+
57+
public bool Exists<TItem>(object id) where TItem : class
58+
{
59+
return ElasticClient.DocumentExists<TItem>(x => x.Id(id.ToString())).Exists;
60+
}
61+
62+
public bool IsServerAvailable()
63+
{
64+
try
65+
{
66+
return ElasticClient.Ping(new PingRequest()).ConnectionStatus.Success;
67+
}
68+
catch(Exception exception)
69+
{
70+
Log.Error(exception);
71+
return false;
72+
}
73+
}
74+
}
75+
}
76+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Data.Entity;
3+
4+
namespace IntegrationEngine.Core.Storage
5+
{
6+
public interface IDatabaseRepository : IRepository, IDisposable
7+
{
8+
void Save();
9+
void SetState<TItem>(TItem item, EntityState entityState) where TItem : class;
10+
}
11+
}
12+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using IntegrationEngine.Model;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace IntegrationEngine.Core.Storage
6+
{
7+
public interface IElasticsearchRepository : IRepository
8+
{
9+
new IEnumerable<TItem> SelectAll<TItem>() where TItem : class, IHasStringId;
10+
new TItem SelectById<TItem>(object id) where TItem : class, IHasStringId;
11+
new TItem Update<TItem>(TItem item) where TItem : class, IHasStringId;
12+
new TItem Insert<TItem>(TItem item) where TItem : class, IHasStringId;
13+
}
14+
}
15+

IntegrationEngine.Core/Storage/IRepository.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77

88
namespace IntegrationEngine.Core.Storage
99
{
10-
public interface IRepository<T> where T : class
10+
public interface IRepository
1111
{
12-
IEnumerable<T> SelectAll();
13-
T SelectById(object id);
14-
T Insert(T value);
15-
T Update(T value);
16-
void Delete(object id);
17-
void Save();
18-
bool Exists(object id);
19-
void SetState(T value, EntityState entityState);
20-
void Dispose();
12+
IEnumerable<TItem> SelectAll<TItem>() where TItem : class;
13+
TItem SelectById<TItem>(object id) where TItem : class;
14+
TItem Insert<TItem>(TItem item) where TItem : class;
15+
TItem Update<TItem>(TItem item) where TItem : class;
16+
void Delete<TItem>(object id) where TItem : class;
17+
bool Exists<TItem>(object id) where TItem : class;
2118
bool IsServerAvailable();
2219
}
2320
}
Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using Common.Logging;using System;
22
using System.Collections.Generic;
33
using System.Data.Entity;
44
using System.Linq;
@@ -7,10 +7,10 @@
77

88
namespace IntegrationEngine.Core.Storage
99
{
10-
public class Repository<T> : IRepository<T> where T : class
10+
public class Repository : IDatabaseRepository
1111
{
12+
public ILog Log { get; set; }
1213
public IntegrationEngineContext db = null;
13-
public DbSet<T> table = null;
1414

1515
public Repository()
1616
{
@@ -19,50 +19,50 @@ public Repository()
1919
public Repository(IntegrationEngineContext db)
2020
{
2121
this.db = db;
22-
table = db.Set<T>();
2322
}
2423

25-
public IEnumerable<T> SelectAll()
24+
public IEnumerable<TItem> SelectAll<TItem>() where TItem : class
2625
{
27-
return table.ToList();
26+
return db.Set<TItem>().ToList<TItem>();
2827
}
2928

30-
public T SelectById(object id)
29+
public TItem SelectById<TItem>(object id) where TItem : class
3130
{
32-
return table.Find(id);
31+
return db.Set<TItem>().Find(id);
32+
3333
}
3434

35-
public T Insert(T value)
35+
public TItem Insert<TItem>(TItem item) where TItem : class
3636
{
37-
return table.Add(value);
37+
return db.Set<TItem>().Add(item);
3838
}
3939

40-
public T Update(T value)
40+
public TItem Update<TItem>(TItem item) where TItem : class
4141
{
42-
table.Attach(value);
43-
db.Entry(value).State = EntityState.Modified;
44-
return db.Entry(value).Entity;
42+
db.Set<TItem>().Attach(item);
43+
db.Entry(item).State = EntityState.Modified;
44+
return db.Entry(item).Entity;
4545
}
4646

47-
public void Delete(object id)
47+
public void Delete<TItem>(object id) where TItem : class
4848
{
49-
T existing = table.Find(id);
50-
table.Remove(existing);
49+
TItem existing = db.Set<TItem>().Find(id);
50+
db.Set<TItem>().Remove(existing);
5151
}
5252

5353
public void Save()
5454
{
5555
db.SaveChanges();
5656
}
5757

58-
public bool Exists(object id)
58+
public bool Exists<TItem>(object id) where TItem : class
5959
{
60-
return table.Find(id) != null;
60+
return db.Set<TItem>().Find(id) != null;
6161
}
6262

63-
public void SetState(T value, EntityState entityState)
63+
public void SetState<TItem>(TItem item, EntityState entityState) where TItem : class
6464
{
65-
db.Entry(value).State = entityState;
65+
db.Entry(item).State = entityState;
6666
}
6767

6868
public void Dispose()
@@ -72,7 +72,16 @@ public void Dispose()
7272

7373
public bool IsServerAvailable()
7474
{
75-
throw new NotImplementedException();
75+
try
76+
{
77+
db.Database.Connection.Open();
78+
return true;
79+
}
80+
catch(Exception exception)
81+
{
82+
Log.Error(exception);
83+
return false;
84+
}
7685
}
7786
}
7887
}

0 commit comments

Comments
 (0)