|
I have never used Lazy loading in my own applications, for various reasons.
Anyways, looking at your code, Bill, I can say if I had to implement "lazy" loading like feature here, I would simply just set it to null . In most cases of Lazy loading — for instance, the Entity Framework navigation — the types are null, unless you actually specify to need them. Then, once it is required, I would load the data using the yield operator; giving me somewhat relative of the lazy loading.
But my answers depends on the way you fetch the List<Person> , from database perhaps? I have not yet tried .NET 4.7 by now. Does it include anything new?
Shiv provides a good overview, and to rewrite that in a different way, would require the understanding that he provides here, Can you explain Lazy Loading?
Lazy Loading By Using C# Yield Statements | Freaking Awesome
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Quote: if I had to implement "lazy" loading like feature here, I would simply just set it to null Hi, Afzaal,
I do not claim to be an expert on Lazy<T>: the question here comes out of my "quest" to better understand it. I would say that you use a Lazy<T> because instantiating one with 'new does not allocate memory for what it wraps. So you can eliminate guard-code like in the 'getter in this Property:
public List<PersonNode> AllNodes
{
get => _allNodes ?? (_allNodes = new List<PersonNode>());
private set => _allNodes = value;
} Even though I have not used the thread-safety features of Lazy<T>, it's clear those features are extensive.
C# 7 ? : still trying to digest it Some well written articles by Jonathan Allen are helping: [^]
cheers, Bill
«When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal
|
|
|
|
|
I have also not worked too much in this context because most of my data sources are SQL based databases, so I try to load only the data that I require,
SELECT TOP 10 FROM table_name ... That works like a charm in my cases.
BillWoodruff wrote: instantiating one with 'new does not allocate memory for what it wraps Of course, that is why I provided the Entity Framework example, they provide a function Include , which you execute and they then load the rest of the data, until then the objects are null. That EF code, gave me a hint that in most cases I should just leave the objects to null and then yield them when needed; this works in some cases. This yield does somewhat better, as it does provide the objects when we actually iterate over them (use them).
As for C# 7, I just didn't get enough time to actually experiment around with them like I did with C# 6, as I was busy with my final year project and some academics stuff.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Not sure if it's better, but something like this might work:
public class LazyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
{
private readonly ConcurrentDictionary<TKey, TValue> _values;
private readonly Func<TKey, TValue> _valueFactory;
private readonly Func<TKey, bool> _keyValidator;
public LazyDictionary(Func<TKey, TValue> valueFactory, Func<TKey, bool> keyValidator = null, IEqualityComparer<TKey> keyComparer = null)
{
if (valueFactory == null) throw new ArgumentNullException(nameof(valueFactory));
if (keyComparer == null) keyComparer = EqualityComparer<TKey>.Default;
_values = new ConcurrentDictionary<TKey, TValue>(keyComparer);
_valueFactory = valueFactory;
_keyValidator = keyValidator;
}
private bool KeyIsValid(TKey key) => _keyValidator == null || _keyValidator(key);
private TValue GetOrAdd(TKey key) => _values.GetOrAdd(key, _valueFactory);
public int Count => _values.Count;
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => _values.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerable<TKey> Keys => _values.Keys;
public IEnumerable<TValue> Values => _values.Values;
public bool ContainsKey(TKey key) => KeyIsValid(key);
public bool TryGetValue(TKey key, out TValue value)
{
if (!ContainsKey(key))
{
value = default(TValue);
return false;
}
value = GetOrAdd(key);
return true;
}
public TValue this[TKey key]
{
get
{
if (!ContainsKey(key)) throw new KeyNotFoundException();
return GetOrAdd(key);
}
}
}
Usage:
public IReadOnlyDictionary<Division, IList<Person>> DivisionToStaff { get; }
= new LazyDictionary<Division, IList<Person>>(d => new List<Person>(), d => Enum.IsDefined(typeof(Division), d));
...
DivisionToStaff[personInstance.Division].Add(personInstance);
ContainsKey will return true for any valid key.
Count , GetEnumerator , and the Keys and Values properties will only reflect the keys which have already been created.
TryGetValue and the indexer will throw an exception for any invalid key; return the existing value for a valid key if it's already been created; and create the value for a valid key if it hasn't.
It has the added advantage of being (mostly) thread-safe.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Wow ! That's the Ph.D. answer. My reading of the Lazy<T> docs suggests its multi-threading features are robust.
thanks, Bill
«When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal
|
|
|
|
|
The Lazy<T> threading support can be robust, depending on which LazyThreadSafetyMode value you pass to the constructor. (The default is ExecutionAndPublication , which is the most robust.)
But the Dictionary<TKey, TValue> definitely isn't thread-safe if you modify it.
It's not a problem if you're pre-populating the dictionary with a Lazy<T> for every possible key, and never modifying the contents of the dictionary. But if you have a large set of keys, some of which are rarely used, you might want to avoid the memory overhead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have a software that records a wav file with naudio and then I can play the file. Now I shall add dynamic range compression to the file and I have really now idea what to do. I hoped that naudio offers something because there is a class SimpleCompressorStream but I can't find any example how to use it. cscore seems to offer a compressor too but there I can't find any documentation or example either.
I'm very new to the entire audio topic so it would be wonderful to find any example or tutorial for C# about dynamic range compression or any libary that I could use
|
|
|
|
|
Member 13196574 wrote: it would be wonderful to find any example or tutorial Then you need to go and look for them: www.google.com[^].
|
|
|
|
|
That's what I did the past days and so far I didn't find what I was looking for. A lot of compression results are more about data compression like zip and not range compression to make the audio file louder and the other pages were more about the general explaination what range compression is. I didn't find a page whith a good tutorial how to implement it.
|
|
|
|
|
Well maybe no one has yet written one. But if you want help here you need to ask a specific technical question.
|
|
|
|
|
I am not really getting what you were looking for then. A simple search for e.g. 'sound processing library' returns loads of alternatives.
But then: I am not expecting to find a ready-to-call function in my favorite language, isolated from any function that I do not need. I'll take any alternative in any c-class language, from k&r C to C++ to Java to C#, or even Python, and I'd be prepared to rewrite it in a language / form / framework suiting my needs. In other words, I don't expect to use it as a black box but to understand the code.
12-15 years ago I did that for reverb functions, using Csound[^]. A lot has happened with Csound since then. Dynamic compression was not in my list of requirements, but I'd be very surprised if it is not available in the library.
If you consider Csound, you must be prepared to translate from plain C to C#, but that is nice way to get to know the code.
|
|
|
|
|
|
Thank you for the hint. I will take a look at it
|
|
|
|
|
I wana Know how to Campare audio files Waves using c#.net i wana create an application which is More Like Shazam
|
|
|
|
|
Good. So you've got some ideas there for areas you need to research, possibly including reading articles. What do you need from us?
This space for rent
|
|
|
|
|
Pete O'Hanlon wrote: What do you need from us? Since this is the discussion forum and not the QA I would expect they are looking for a discussion.
There are two kinds of people in the world: those who can extrapolate from incomplete data.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I suggest to start doing some research.
Using your favorite serach engine with something like "c# compare wav files" would be a good start.
Shazam (service) - Wikipedia[^] is providing also some information:
Quote: Shazam uses a smartphone or computer's built-in microphone to gather a brief sample of audio being played. It creates an acoustic fingerprint based on the sample and compares it against a central database for a match. The keywords are acoustic fingerprint and database which might be the next search terms besides recording audio.
|
|
|
|
|
I want to send a http request from client to server by adding some headers and want to retrieve back those headers at server side .can some one give me the whole code for this i.e., including clent and server if possible.
Thank you in advance .
|
|
|
|
|
When you send those extra headers to the server, you do so by adding them to the collection of default headers, on the server side you can do the same. You can extract them from the headers collection attached with the request.
We do not provide "whole code" for anything, there are however several ways to get the code or explanation from CodeProject, first of all search for this concept "headers c# http client" in the top right corner, and find yourself a useful article.
Secondly, consider using Google and append " codeproject" to search for project content, and the best way is to show us what you have done. Show us your attempt at solving this problem, and we will help you with making it work.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
|
0
down vote
favorite
I am using clientCache in my web.config:
<staticContent>
<clientCache cacheControlMode="UseMaxAge"cacheControlMaxAge="30.00:00:00"/>
</staticContent>
and in the controller on action I am using the OutputCache attribute:
[OutputCache(Duration = 300)] // 300 for 5 mins
The problem is I am getting the error in google speed insight
thanks in advance
|
|
|
|
|
Your question wasn't entirely clear when you posted it on StackOverflow:
.net - Leverage browser caching for images in mvc using c# - Stack Overflow[^]
It's even less clear here, because you didn't link to the screen-shot or tell us what the error is.
As Dávid Molnár said in the StackOverflow comments, you need to update your question to show how the images are being generated, and the HTTP response headers for the image request. You can use the browser's developer tools, or an external tool like Fiddler[^], to see the headers.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I have a text file that contains html tags. I like to use C# to read this file and list all the src attributes of tags.
I was wondering how I can read through my file and list all of them.
Thank you in advance for your time and consideration.
|
|
|
|
|
Use an existing HTML parser: MIL HTML Parser[^] - let it handle the "donkey work" for you.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
That was a nice project but the problem is my content is coming from a json file, not a single html page.
|
|
|
|
|