Skip to content

Commit d4fdcc3

Browse files
Kielekaustinlparker
authored andcommitted
[cartservice] code cleanup (open-telemetry#943)
* File scope namespaces * Sort modifiers * Remove redundant type declaration * Avoid hiding variables * join declaration and assignment * Use standard .NET convention for fields and consts * inline out variable * object initializer * collection initializer * drop unused using * drop unused parameters * remove redundant field initializer --------- Co-authored-by: Austin Parker <[email protected]>
1 parent 3285894 commit d4fdcc3

File tree

7 files changed

+356
-363
lines changed

7 files changed

+356
-363
lines changed

src/cartservice/src/Program.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818

1919
var builder = WebApplication.CreateBuilder(args);
2020
string redisAddress = builder.Configuration["REDIS_ADDR"];
21-
RedisCartStore cartStore = null;
2221
if (string.IsNullOrEmpty(redisAddress))
2322
{
2423
Console.WriteLine("REDIS_ADDR environment variable is required.");
2524
Environment.Exit(1);
2625
}
27-
cartStore = new RedisCartStore(redisAddress);
26+
var cartStore = new RedisCartStore(redisAddress);
2827

2928
// Initialize the redis store
3029
await cartStore.InitializeAsync();
@@ -41,15 +40,15 @@
4140

4241
builder.Services.AddOpenTelemetry()
4342
.ConfigureResource(appResourceBuilder)
44-
.WithTracing(builder => builder
43+
.WithTracing(tracerBuilder => tracerBuilder
4544
.AddRedisInstrumentation(
4645
cartStore.GetConnection(),
4746
options => options.SetVerboseDatabaseStatements = true)
4847
.AddAspNetCoreInstrumentation()
4948
.AddGrpcClientInstrumentation()
5049
.AddHttpClientInstrumentation()
5150
.AddOtlpExporter())
52-
.WithMetrics(builder => builder
51+
.WithMetrics(meterBuilder => meterBuilder
5352
.AddRuntimeInstrumentation()
5453
.AddAspNetCoreInstrumentation()
5554
.AddOtlpExporter());

src/cartservice/src/cartstore/ICartStore.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
// SPDX-License-Identifier: Apache-2.0
33
using System.Threading.Tasks;
44

5-
namespace cartservice.cartstore
5+
namespace cartservice.cartstore;
6+
7+
public interface ICartStore
68
{
7-
public interface ICartStore
8-
{
9-
Task InitializeAsync();
9+
Task InitializeAsync();
1010

11-
Task AddItemAsync(string userId, string productId, int quantity);
12-
Task EmptyCartAsync(string userId);
11+
Task AddItemAsync(string userId, string productId, int quantity);
12+
Task EmptyCartAsync(string userId);
1313

14-
Task<Oteldemo.Cart> GetCartAsync(string userId);
14+
Task<Oteldemo.Cart> GetCartAsync(string userId);
1515

16-
bool Ping();
17-
}
16+
bool Ping();
1817
}

src/cartservice/src/cartstore/LocalCartStore.cs

+41-44
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@
66
using System.Linq;
77
using System.Threading.Tasks;
88

9-
namespace cartservice.cartstore
9+
namespace cartservice.cartstore;
10+
11+
internal class LocalCartStore : ICartStore
1012
{
11-
internal class LocalCartStore : ICartStore
12-
{
13-
// Maps between user and their cart
14-
private ConcurrentDictionary<string, Oteldemo.Cart> userCartItems = new ConcurrentDictionary<string, Oteldemo.Cart>();
15-
private readonly Oteldemo.Cart emptyCart = new Oteldemo.Cart();
13+
// Maps between user and their cart
14+
private readonly ConcurrentDictionary<string, Oteldemo.Cart> _userCartItems = new();
15+
private readonly Oteldemo.Cart _emptyCart = new();
1616

17-
public Task InitializeAsync()
18-
{
19-
Console.WriteLine("Local Cart Store was initialized");
17+
public Task InitializeAsync()
18+
{
19+
Console.WriteLine("Local Cart Store was initialized");
2020

21-
return Task.CompletedTask;
22-
}
21+
return Task.CompletedTask;
22+
}
2323

24-
public Task AddItemAsync(string userId, string productId, int quantity)
24+
public Task AddItemAsync(string userId, string productId, int quantity)
25+
{
26+
Console.WriteLine($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}");
27+
var newCart = new Oteldemo.Cart
2528
{
26-
Console.WriteLine($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}");
27-
var newCart = new Oteldemo.Cart
28-
{
29-
UserId = userId,
30-
Items = { new Oteldemo.CartItem { ProductId = productId, Quantity = quantity } }
31-
};
32-
userCartItems.AddOrUpdate(userId, newCart,
33-
(k, exVal) =>
29+
UserId = userId,
30+
Items = { new Oteldemo.CartItem { ProductId = productId, Quantity = quantity } }
31+
};
32+
_userCartItems.AddOrUpdate(userId, newCart,
33+
(_, exVal) =>
3434
{
3535
// If the item exists, we update its quantity
3636
var existingItem = exVal.Items.SingleOrDefault(item => item.ProductId == productId);
@@ -46,35 +46,32 @@ public Task AddItemAsync(string userId, string productId, int quantity)
4646
return exVal;
4747
});
4848

49-
return Task.CompletedTask;
50-
}
49+
return Task.CompletedTask;
50+
}
5151

52-
public Task EmptyCartAsync(string userId)
53-
{
54-
var eventTags = new ActivityTagsCollection();
55-
eventTags.Add("userId", userId);
56-
Activity.Current?.AddEvent(new ActivityEvent("EmptyCartAsync called.", default, eventTags));
52+
public Task EmptyCartAsync(string userId)
53+
{
54+
var eventTags = new ActivityTagsCollection {{"userId", userId}};
55+
Activity.Current?.AddEvent(new ActivityEvent("EmptyCartAsync called.", default, eventTags));
5756

58-
userCartItems[userId] = new Oteldemo.Cart();
59-
return Task.CompletedTask;
60-
}
57+
_userCartItems[userId] = new Oteldemo.Cart();
58+
return Task.CompletedTask;
59+
}
6160

62-
public Task<Oteldemo.Cart> GetCartAsync(string userId)
61+
public Task<Oteldemo.Cart> GetCartAsync(string userId)
62+
{
63+
Console.WriteLine($"GetCartAsync called with userId={userId}");
64+
if (!_userCartItems.TryGetValue(userId, out var cart))
6365
{
64-
Console.WriteLine($"GetCartAsync called with userId={userId}");
65-
Oteldemo.Cart cart = null;
66-
if (!userCartItems.TryGetValue(userId, out cart))
67-
{
68-
Console.WriteLine($"No carts for user {userId}");
69-
return Task.FromResult(emptyCart);
70-
}
71-
72-
return Task.FromResult(cart);
66+
Console.WriteLine($"No carts for user {userId}");
67+
return Task.FromResult(_emptyCart);
7368
}
7469

75-
public bool Ping()
76-
{
77-
return true;
78-
}
70+
return Task.FromResult(cart);
71+
}
72+
73+
public bool Ping()
74+
{
75+
return true;
7976
}
8077
}

0 commit comments

Comments
 (0)