Skip to content

Commit 4facbcd

Browse files
committed
Refactoring
1 parent 5984517 commit 4facbcd

File tree

5 files changed

+89
-98
lines changed

5 files changed

+89
-98
lines changed

VirtoCommerce.OrdersModule.sln.DotSettings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=15b5b1f1_002D457c_002D4ca6_002Db278_002D5615aedc07d3/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static readonly fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
23
<s:Boolean x:Key="/Default/UserDictionary/Words/=auditable/@EntryIndexedValue">True</s:Boolean>
34
<s:Boolean x:Key="/Default/UserDictionary/Words/=hangfire/@EntryIndexedValue">True</s:Boolean>
45
<s:Boolean x:Key="/Default/UserDictionary/Words/=npgsql/@EntryIndexedValue">True</s:Boolean>
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.Reflection;
13
using VirtoCommerce.Platform.Core.Swagger;
24

35
namespace VirtoCommerce.OrdersModule.Core.Model
46
{
57
[SwaggerSchemaId("OrderAddress")]
68
public class Address : CoreModule.Core.Common.Address
79
{
10+
public virtual IEnumerable<PropertyInfo> GetAllProperties()
11+
{
12+
return GetProperties();
13+
}
814
}
9-
1015
}

src/VirtoCommerce.OrdersModule.Data/Handlers/IndexCustomerOrderChangedEventHandler.cs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,54 @@ namespace VirtoCommerce.OrdersModule.Data.Handlers
1717
{
1818
public class IndexCustomerOrderChangedEventHandler : IEventHandler<OrderChangedEvent>
1919
{
20-
protected ISettingsManager SettingsManager { get; }
21-
22-
protected IConfiguration Configuration { get; }
23-
24-
protected IIndexingJobService IndexingJobService { get; }
25-
26-
protected IEnumerable<IndexDocumentConfiguration> IndexingConfigurations { get; }
20+
private readonly ISettingsManager _settingsManager;
21+
private readonly IConfiguration _configuration;
22+
private readonly IIndexingJobService _indexingJobService;
23+
private readonly IEnumerable<IndexDocumentConfiguration> _indexingConfigurations;
2724

2825
public IndexCustomerOrderChangedEventHandler(
2926
ISettingsManager settingsManager,
3027
IConfiguration configuration,
3128
IIndexingJobService indexingJobService,
3229
IEnumerable<IndexDocumentConfiguration> indexingConfigurations)
3330
{
34-
SettingsManager = settingsManager;
35-
Configuration = configuration;
36-
IndexingJobService = indexingJobService;
37-
IndexingConfigurations = indexingConfigurations;
31+
_settingsManager = settingsManager;
32+
_configuration = configuration;
33+
_indexingJobService = indexingJobService;
34+
_indexingConfigurations = indexingConfigurations;
3835
}
3936

4037
public virtual async Task Handle(OrderChangedEvent message)
4138
{
42-
if (!await ShouldIndexAsync())
39+
if (await ShouldIndexAsync())
4340
{
44-
return;
41+
await IndexOrdersAsync(message);
4542
}
46-
47-
await IndexOrdersAsync(message);
4843
}
4944

5045
protected virtual async Task<bool> ShouldIndexAsync()
5146
{
52-
return Configuration.IsOrderFullTextSearchEnabled() &&
53-
await SettingsManager.GetValueAsync<bool>(ModuleConstants.Settings.General.EventBasedIndexation);
47+
return _configuration.IsOrderFullTextSearchEnabled() &&
48+
await _settingsManager.GetValueAsync<bool>(ModuleConstants.Settings.General.EventBasedIndexation);
5449
}
5550

5651
protected virtual Task IndexOrdersAsync(OrderChangedEvent message)
5752
{
5853
var indexEntries = GetOrderIndexEntries(message);
5954

60-
if (indexEntries.Length > 0)
55+
if (indexEntries.Count > 0)
6156
{
62-
var documentBuilders = IndexingConfigurations
57+
var documentBuilders = _indexingConfigurations
6358
.GetDocumentBuilders(ModuleConstants.OrderIndexDocumentType, typeof(CustomerOrderChangesProvider))
6459
.ToList();
6560

66-
IndexingJobService.EnqueueIndexAndDeleteDocuments(indexEntries, JobPriority.Normal, documentBuilders);
61+
_indexingJobService.EnqueueIndexAndDeleteDocuments(indexEntries, JobPriority.Normal, documentBuilders);
6762
}
6863

6964
return Task.CompletedTask;
7065
}
7166

72-
protected virtual IndexEntry[] GetOrderIndexEntries(OrderChangedEvent message)
67+
protected virtual IList<IndexEntry> GetOrderIndexEntries(OrderChangedEvent message)
7368
{
7469
return message?.ChangedEntries
7570
.Select(x => new IndexEntry
@@ -78,7 +73,7 @@ protected virtual IndexEntry[] GetOrderIndexEntries(OrderChangedEvent message)
7873
EntryState = x.EntryState,
7974
Type = ModuleConstants.OrderIndexDocumentType,
8075
})
81-
.ToArray() ?? [];
76+
.ToList() ?? [];
8277
}
8378
}
8479
}

src/VirtoCommerce.OrdersModule.Data/Handlers/LogChangesOrderChangedEventHandler.cs

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Reflection;
65
using System.Threading.Tasks;
76
using AutoCompare;
87
using Hangfire;
@@ -25,11 +24,7 @@ public class LogChangesOrderChangedEventHandler : IEventHandler<OrderChangedEven
2524
private readonly IMemberService _memberService;
2625
private readonly IChangeLogService _changeLogService;
2726
private readonly ISettingsManager _settingsManager;
28-
private static readonly ConcurrentDictionary<Type, HashSet<string>> _auditablePropertiesCacheByTypeDict = new();
29-
private static readonly PropertyInfo[] _addressProperties = typeof(Address)
30-
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
31-
.OrderBy(p => p.Name)
32-
.ToArray();
27+
private static readonly ConcurrentDictionary<Type, List<string>> _auditablePropertiesCacheByTypeDict = new();
3328

3429
public LogChangesOrderChangedEventHandler(IChangeLogService changeLogService, IMemberService memberService, ISettingsManager settingsManager)
3530
{
@@ -62,14 +57,14 @@ protected virtual IList<OperationLog> GetOperationLogs(IEnumerable<GenericChange
6257
switch (changedEntry.EntryState)
6358
{
6459
case EntryState.Modified:
65-
{
66-
var originalOperations = changedEntry.OldEntry.GetFlatObjectsListWithInterface<IOperation>().Distinct().ToList();
67-
var modifiedOperations = changedEntry.NewEntry.GetFlatObjectsListWithInterface<IOperation>().Distinct().ToList();
60+
{
61+
var originalOperations = changedEntry.OldEntry.GetFlatObjectsListWithInterface<IOperation>().Distinct().ToList();
62+
var modifiedOperations = changedEntry.NewEntry.GetFlatObjectsListWithInterface<IOperation>().Distinct().ToList();
6863

69-
modifiedOperations.CompareTo(originalOperations, EqualityComparer<IOperation>.Default,
70-
(state, modified, original) => operationLogs.AddRange(GetChangedEntryOperationLogs(new GenericChangedEntry<IOperation>(modified, original, state))));
71-
break;
72-
}
64+
modifiedOperations.CompareTo(originalOperations, EqualityComparer<IOperation>.Default,
65+
(state, modified, original) => operationLogs.AddRange(GetChangedEntryOperationLogs(new GenericChangedEntry<IOperation>(modified, original, state))));
66+
break;
67+
}
7368
case EntryState.Added or EntryState.Deleted:
7469
operationLogs.AddRange(GetChangedEntryOperationLogs(new GenericChangedEntry<IOperation>(changedEntry.NewEntry, changedEntry.OldEntry, changedEntry.EntryState)));
7570
break;
@@ -91,60 +86,60 @@ protected virtual IEnumerable<OperationLog> GetChangedEntryOperationLogs(Generic
9186
switch (changedEntry.EntryState)
9287
{
9388
case EntryState.Modified:
94-
{
95-
var logs = new List<string>();
96-
var diff = GetOperationDifferences(changedEntry, logs);
97-
var auditableProperties = GetAuditableProperties(changedEntry);
98-
99-
if (auditableProperties.Count > 0)
10089
{
101-
var observedDifferences = diff
102-
.Where(x => auditableProperties.Contains(x.Name))
103-
.Distinct(new DifferenceComparer());
90+
var logs = new List<string>();
91+
var diff = GetOperationDifferences(changedEntry, logs);
92+
var auditableProperties = GetAuditableProperties(changedEntry);
10493

105-
foreach (var difference in observedDifferences)
94+
if (auditableProperties.Count > 0)
10695
{
107-
logs.Add($"The {changedEntry.OldEntry.OperationType} {changedEntry.NewEntry.Number} property '{difference.Name}' changed from '{difference.OldValue}' to '{difference.NewValue}'");
96+
var observedDifferences = diff
97+
.Where(x => auditableProperties.ContainsIgnoreCase(x.Name))
98+
.Distinct(new DifferenceComparer());
99+
100+
foreach (var difference in observedDifferences)
101+
{
102+
logs.Add($"The {changedEntry.OldEntry.OperationType} {changedEntry.NewEntry.Number} property '{difference.Name}' changed from '{difference.OldValue}' to '{difference.NewValue}'");
103+
}
108104
}
109-
}
110105

111-
foreach (var log in logs)
112-
{
113-
result.Add(GetLogRecord(changedEntry.NewEntry, log));
114-
}
106+
foreach (var log in logs)
107+
{
108+
result.Add(GetLogRecord(changedEntry.NewEntry, log));
109+
}
115110

116-
break;
117-
}
111+
break;
112+
}
118113
case EntryState.Deleted:
119-
{
120-
var record = GetLogRecord(changedEntry.NewEntry,
121-
$"The {changedEntry.NewEntry.OperationType} {changedEntry.NewEntry.Number} deleted",
122-
EntryState.Deleted);
123-
result.Add(record);
124-
break;
125-
}
114+
{
115+
var record = GetLogRecord(changedEntry.NewEntry,
116+
$"The {changedEntry.NewEntry.OperationType} {changedEntry.NewEntry.Number} deleted",
117+
EntryState.Deleted);
118+
result.Add(record);
119+
break;
120+
}
126121
case EntryState.Added:
127-
{
128-
var record = GetLogRecord(changedEntry.NewEntry,
129-
$"The new {changedEntry.NewEntry.OperationType} {changedEntry.NewEntry.Number} added",
130-
EntryState.Added);
131-
result.Add(record);
132-
break;
133-
}
122+
{
123+
var record = GetLogRecord(changedEntry.NewEntry,
124+
$"The new {changedEntry.NewEntry.OperationType} {changedEntry.NewEntry.Number} added",
125+
EntryState.Added);
126+
result.Add(record);
127+
break;
128+
}
134129
}
135130

136131
return result;
137132
}
138133

139-
protected static HashSet<string> GetAuditableProperties(GenericChangedEntry<IOperation> changedEntry)
134+
protected static List<string> GetAuditableProperties(GenericChangedEntry<IOperation> changedEntry)
140135
{
141136
var type = changedEntry.OldEntry.GetType();
142137

143-
return _auditablePropertiesCacheByTypeDict.GetOrAdd(type, t => new HashSet<string>(
138+
return _auditablePropertiesCacheByTypeDict.GetOrAdd(type, t =>
144139
t.GetProperties()
145140
.Where(prop => Attribute.IsDefined(prop, typeof(AuditableAttribute)))
146-
.Select(x => x.Name),
147-
StringComparer.OrdinalIgnoreCase));
141+
.Select(x => x.Name)
142+
.ToList());
148143
}
149144

150145
protected virtual IList<Difference> GetOperationDifferences(GenericChangedEntry<IOperation> changedEntry, List<string> logs)
@@ -179,6 +174,7 @@ protected virtual IList<Difference> GetOperationDifferences(GenericChangedEntry<
179174
protected virtual IEnumerable<string> GetCustomerOrderChanges(CustomerOrder originalOrder, CustomerOrder modifiedOrder)
180175
{
181176
var result = new List<string>();
177+
182178
if (originalOrder.EmployeeId != modifiedOrder.EmployeeId)
183179
{
184180
var employeeName = "none";
@@ -189,25 +185,20 @@ protected virtual IEnumerable<string> GetCustomerOrderChanges(CustomerOrder orig
189185
}
190186
result.Add($"Order employee was changed to '{employeeName}'");
191187
}
188+
192189
result.AddRange(GetAddressChanges(originalOrder, originalOrder.Addresses, modifiedOrder.Addresses));
193190

194191
return result;
195192
}
196193

197194
protected virtual IEnumerable<string> GetShipmentChanges(Shipment originalShipment, Shipment modifiedShipment)
198195
{
199-
var result = new List<string>();
200-
result.AddRange(GetAddressChanges(originalShipment, [originalShipment.DeliveryAddress], [modifiedShipment.DeliveryAddress]));
201-
202-
return result;
196+
return GetAddressChanges(originalShipment, [originalShipment.DeliveryAddress], [modifiedShipment.DeliveryAddress]);
203197
}
204198

205199
protected virtual IEnumerable<string> GetPaymentChanges(PaymentIn payment, PaymentIn modifiedPayment)
206200
{
207-
var result = new List<string>();
208-
result.AddRange(GetAddressChanges(payment, [payment.BillingAddress], [modifiedPayment.BillingAddress]));
209-
210-
return result;
201+
return GetAddressChanges(payment, [payment.BillingAddress], [modifiedPayment.BillingAddress]);
211202
}
212203

213204
protected virtual IEnumerable<string> GetAddressChanges(IOperation operation, IEnumerable<Address> originalAddress, IEnumerable<Address> modifiedAddress)
@@ -217,7 +208,7 @@ protected virtual IEnumerable<string> GetAddressChanges(IOperation operation, IE
217208
var modifiedAddressList = modifiedAddress?.Where(x => x != null).ToList() ?? [];
218209
var originalAddressList = originalAddress?.Where(x => x != null).ToList() ?? [];
219210

220-
modifiedAddressList.CompareTo(originalAddressList, EqualityComparer<Address>.Default, (state, source, target) =>
211+
modifiedAddressList.CompareTo(originalAddressList, EqualityComparer<Address>.Default, (state, _, target) =>
221212
{
222213
switch (state)
223214
{
@@ -237,7 +228,7 @@ protected virtual string StringifyAddress(Address address)
237228
{
238229
return address is null
239230
? string.Empty
240-
: string.Join(", ", _addressProperties.Select(p => p.GetValue(address)).Where(x => x != null));
231+
: string.Join(", ", address.GetAllProperties().Select(p => p.GetValue(address)).Where(x => x != null));
241232
}
242233

243234
protected virtual OperationLog GetLogRecord(IOperation operation, string template, EntryState operationType = EntryState.Modified)

0 commit comments

Comments
 (0)