Skip to content

Commit 71b2535

Browse files
committed
Initial service library and example app
1 parent 63008f0 commit 71b2535

8 files changed

+376
-29
lines changed

example/LogService.cs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace GeekyMonkey.Example
7+
{
8+
/// <summary>
9+
/// Simple logging service for output
10+
/// </summary>
11+
public class LogService
12+
{
13+
/// <summary>
14+
/// Output text
15+
/// </summary>
16+
/// <param name="message">Message to output</param>
17+
public void Log(string message)
18+
{
19+
Console.WriteLine(message);
20+
}
21+
22+
/// <summary>
23+
/// Output a list of products
24+
/// </summary>
25+
/// <param name="products">List of products</param>
26+
public void Log(ProductModel product)
27+
{
28+
this.Log($" Product={product.ProductName}");
29+
}
30+
31+
/// <summary>
32+
/// Output a list of products
33+
/// </summary>
34+
/// <param name="products">List of products</param>
35+
public void Log(List<ProductModel> products)
36+
{
37+
List<string> productNames = new List<string>();
38+
foreach (var product in products)
39+
{
40+
productNames.Add(product.ProductName);
41+
}
42+
this.Log($"{products.Count} products: {string.Join(", ", productNames)}");
43+
}
44+
}
45+
}

example/ProductModel.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace GeekyMonkey.Example
2+
{
3+
public class ProductModel
4+
{
5+
public string ProductCategory { get; set; }
6+
7+
public string ProductType { get; set; }
8+
9+
public string ProductName { get; set; }
10+
}
11+
}

example/ProductProvider.cs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using GeekyMonkey.DotNetCore;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace GeekyMonkey.Example
8+
{
9+
/// <summary>
10+
/// Simulates accessing product data from an expensive to hit database or api
11+
/// </summary>
12+
public class ProductProvider
13+
{
14+
/// <summary>
15+
/// Construct the data provider
16+
/// </summary>
17+
/// <param name="memoryCacheService">Injected memory cache service</param>
18+
public ProductProvider(MemoryCacheService memoryCacheService, LogService logService)
19+
{
20+
this.LogSerivce = logService;
21+
this.MemoryCacheService = memoryCacheService;
22+
}
23+
24+
/// <summary>
25+
/// Injected log service
26+
/// </summary>
27+
private readonly LogService LogSerivce;
28+
29+
/// <summary>
30+
/// Injected memory cache service
31+
/// </summary>
32+
private readonly MemoryCacheService MemoryCacheService;
33+
34+
/// <summary>
35+
/// Our complete product database
36+
/// </summary>
37+
private List<ProductModel> ProductData = new List<ProductModel>
38+
{
39+
new ProductModel { ProductCategory = "Groceries", ProductType = "Beer", ProductName = "Guinness"},
40+
new ProductModel { ProductCategory = "Groceries", ProductType = "Beer", ProductName = "Beamish"},
41+
new ProductModel { ProductCategory = "Groceries", ProductType = "Fruit", ProductName = "Apple"},
42+
new ProductModel { ProductCategory = "Groceries", ProductType = "Fruit", ProductName = "Orange"},
43+
new ProductModel { ProductCategory = "Groceries", ProductType = "Meat", ProductName = "Bacon"},
44+
new ProductModel { ProductCategory = "Groceries", ProductType = "Meat", ProductName = "Steak"},
45+
new ProductModel { ProductCategory = "Tools", ProductType = "Hand Tool", ProductName = "Hammer"},
46+
new ProductModel { ProductCategory = "Tools", ProductType = "Hand Tool", ProductName = "Saw"},
47+
};
48+
49+
/// <summary>
50+
/// Get products for a given category and type
51+
/// </summary>
52+
/// <param name="category">Category filter</param>
53+
/// <param name="productType">Product type filter</param>
54+
/// <returns></returns>
55+
public List<ProductModel> GetProducts(string category, string productType)
56+
{
57+
var productList = MemoryCacheService.GetOrCreate<List<ProductModel>>(
58+
$"Products_Category={category}", $"GetProducts_Category={category}_Type={productType}",
59+
60, (cacheEntry) => {
60+
61+
LogSerivce.Log($"!!! Generating List for category={category} type={productType} !!! <--- Expensive!");
62+
var filteredProducts = ProductData
63+
.Where(p => p.ProductCategory == category && p.ProductType == productType)
64+
.OrderBy(p => p.ProductName)
65+
.ToList();
66+
return filteredProducts;
67+
});
68+
69+
return productList;
70+
}
71+
72+
/// <summary>
73+
/// Add a new product to the databaes (and invalidate the cache)
74+
/// </summary>
75+
/// <param name="category">new product category</param>
76+
/// <param name="productType">new product type</param>
77+
/// <param name="productName">new product name</param>
78+
public void AddProduct(string category, string productType, string productName)
79+
{
80+
ProductData.Add(new ProductModel
81+
{
82+
ProductCategory = category,
83+
ProductType = productType,
84+
ProductName = productName
85+
});
86+
87+
MemoryCacheService.ClearCacheGroup($"Products_Category={category}");
88+
}
89+
}
90+
}

example/Program.cs

+58-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,67 @@
1-
using System;
1+
using GeekyMonkey.DotNetCore;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System;
4+
using System.Collections.Generic;
25

3-
namespace example
6+
namespace GeekyMonkey.Example
47
{
58
class Program
69
{
10+
static IServiceProvider serviceProvider;
11+
712
static void Main(string[] args)
813
{
9-
Console.WriteLine("Hello World!");
14+
// Setup services with dependency injector
15+
serviceProvider = ConfigureServices();
16+
17+
// Get an instance of our data provider and log serivce
18+
var productProvider = serviceProvider.GetService<ProductProvider>();
19+
var logService = serviceProvider.GetRequiredService<LogService>();
20+
21+
// Get beer list multiple times - should only generate the data once
22+
for (int i = 0; i < 3; i++)
23+
{
24+
logService.Log("Requesting beer list.");
25+
var beer = productProvider.GetProducts("Groceries", "Beer");
26+
logService.Log(beer);
27+
logService.Log("");
28+
}
29+
30+
// Add a new beer to the database
31+
logService.Log("");
32+
logService.Log("++ Adding a new Beer ++");
33+
logService.Log("");
34+
productProvider.AddProduct("Groceries", "Beer", "Lagunitas IPA");
35+
36+
// Get beer list multiple times - should only generate the data once
37+
for (int i = 0; i < 3; i++)
38+
{
39+
logService.Log("Requesting beer list.");
40+
var beer = productProvider.GetProducts("Groceries", "Beer");
41+
logService.Log(beer);
42+
logService.Log("");
43+
}
44+
}
45+
46+
/// <summary>
47+
/// Register services with dependency injector
48+
/// </summary>
49+
/// <returns>Service provider</returns>
50+
private static IServiceProvider ConfigureServices()
51+
{
52+
ServiceCollection services = new ServiceCollection();
53+
54+
// Setup memory cach service
55+
services.AddMemoryCache();
56+
services.AddSingleton<MemoryCacheService, MemoryCacheService>();
57+
services.AddTransient<ProductProvider, ProductProvider>();
58+
59+
// Add a logging service for output
60+
services.AddSingleton<LogService, LogService>();
61+
62+
// Done adding services
63+
ServiceProvider serviceProvider = services.BuildServiceProvider();
64+
return serviceProvider;
1065
}
1166
}
1267
}

example/example.csproj

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
6-
</PropertyGroup>
7-
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
<RootNamespace>GeekyMonkey.Example</RootNamespace>
7+
<AssemblyName>CacheExample</AssemblyName>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\source\DotNetCoreMemoryCacheService.csproj" />
12+
</ItemGroup>
13+
814
<ItemGroup>
9-
<ProjectReference Include="..\source\DotNetCoreMemoryCacheService.csproj" />
10-
</ItemGroup>
11-
12-
</Project>
15+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
16+
</ItemGroup>
17+
18+
</Project>

source/Class1.cs

-8
This file was deleted.
+13-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
5-
</PropertyGroup>
6-
7-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<RootNamespace>GeekyMonkey.DotNetCore</RootNamespace>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
10+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)