-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some refactoring, bool value generator added and subitem list size pr…
…operty added
- Loading branch information
1 parent
03fad72
commit f370af1
Showing
10 changed files
with
69 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,45 @@ | ||
using System.Collections.Concurrent; | ||
using StubGenerator.Core; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Reflection; | ||
|
||
namespace StubGenerator.Caching | ||
{ | ||
public class MemoryStubTypeCache : IStubTypeCache | ||
{ | ||
private readonly ConcurrentDictionary<string, PropertyInfo[]> Cache; | ||
private readonly ConcurrentDictionary<string, PropertyInfo[]> _cache = new ConcurrentDictionary<string, PropertyInfo[]>(); | ||
private readonly IStubTypeCacheKeyGenerator _cacheKeyGenerator; | ||
|
||
public MemoryStubTypeCache() | ||
:this(new DefaultStubTypeCacheKeyGenerator()) | ||
: this(new DefaultStubTypeCacheKeyGenerator()) | ||
{ | ||
} | ||
|
||
public MemoryStubTypeCache(IStubTypeCacheKeyGenerator cacheKeyGenerator) | ||
{ | ||
Cache = new ConcurrentDictionary<string, PropertyInfo[]>(); | ||
_cacheKeyGenerator = cacheKeyGenerator; | ||
_cacheKeyGenerator = cacheKeyGenerator ?? throw new ArgumentNullException(nameof(cacheKeyGenerator)); | ||
} | ||
|
||
public PropertyInfo[] Get<T>(T instance) where T : class | ||
{ | ||
string cacheKey = _cacheKeyGenerator.GenerateKey<T>(); | ||
Cache.TryGetValue(cacheKey, out PropertyInfo[] result); | ||
_cache.TryGetValue(cacheKey, out PropertyInfo[] result); | ||
return result; | ||
} | ||
|
||
public PropertyInfo[] GetOrAdd<T>(T instance, PropertyInfo[] propertyInfos) where T : class | ||
{ | ||
var cacheKey = _cacheKeyGenerator.GenerateKey<T>(); | ||
return Cache.GetOrAdd(cacheKey, i => { return propertyInfos; }); | ||
return _cache.GetOrAdd(cacheKey, i => { return propertyInfos; }); | ||
} | ||
|
||
public bool Set<T>(T instance, PropertyInfo[] stubTypeItem) where T : class | ||
{ | ||
string cacheKey = _cacheKeyGenerator.GenerateKey<T>(); | ||
return Cache.TryAdd(cacheKey, stubTypeItem); | ||
return _cache.TryAdd(cacheKey, stubTypeItem); | ||
} | ||
|
||
public void Clear() => Cache.Clear(); | ||
public void Clear() => _cache.Clear(); | ||
|
||
public bool IsEmpty() | ||
{ | ||
return Cache.Count == 0; | ||
} | ||
public bool IsEmpty() => _cache.Count == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/StubMiddleware.Core/Core/FakeDataGenerators/BoolValueGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
|
||
namespace StubGenerator.Core.FakeDataGenerators | ||
{ | ||
public class BoolValueGenerator : IValueGenerator | ||
{ | ||
private readonly Random _random; | ||
public BoolValueGenerator() | ||
{ | ||
_random = new Random(); | ||
} | ||
public object Generate() | ||
{ | ||
return _random.NextDouble() >= 0.5; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters