concept consumer in category .net

appears as: A consumer, consumer, consumers, The consumer, consumers, The consumers, consumer
Concurrency in .NET

This is an excerpt from Manning's book Concurrency in .NET.

Concurrent collections work well with algorithms that consider the producer/consumer6  implementation. A Producer/Consumer pattern aims to partition and balance the workload between one or more producers and one or more consumers. A producer generates data in an independent thread and inserts it into a queue. A consumer runs a separate thread concurrently, which consumes the data from the queue. For example, a producer could download images and store them in a queue that’s accessed by a consumer that performs image processing. These two entities work independently, and if the workload from the producer increases, you can spawn a new consumer to balance the workload. The Producer/Consumer pattern is one of the most widely used parallel programming patterns, and it will be discussed and implemented in chapter 7.

Back-pressure refers to a situation where the event-fetching producer is getting too far ahead of the event-processing consumer. This could generate potential spikes in memory consumption and possibly reserve more system resources for the consumer until the consumer is caught up. More details regarding back-pressure are covered later in the chapter.

Figure 6.7 Push vs. pull model. The IEnumerable/IEnumerator pattern is based on the pull model, which asks for new data from the source. Alternatively, the IObservable/IObserver pattern is based on the push model, which receives a notification when new data is available to send to the consumer.

c06-07.png
Listing 6.5 Observable pipeline to analyze tweets
let emotionMap =
    [(Unhappy, 0)
     (Indifferent, 0)
     (Happy, 0)] |> Map.ofSeq

let observableTweets =
 stream.TweetReceived   #1  
 |> Observable.throttle(TimeSpan.FromMilliseconds(100.))   #2  
    |> Observable.filter(fun args ->
 args.Tweet.Language = Language.English)   #3  
    |> Observable.groupBy(fun args ->
 evaluateEmotion args.Tweet.FullText)   #4  
    |> Observable.selectMany(fun args ->
        args |> Observable.map(fun i ->
 (args.Key, (max 1 i.Tweet.FavoriteCount))))   #5  
    |> Observable.scan(fun sm (key,count) ->
        match sm |> Map.tryFind key with
        | Some(v) -> sm |> Map.add key (v + count)
 | None -> sm ) emotionMap   #6  
    |> Observable.map(fun sm ->
 let total = sm |> Seq.sumBy(fun v -> v.Value)   #7  
        sm |> Seq.map(fun k ->
            let percentageEmotion = ((float k.Value) * 100.) 
➥ / (float total)
            let labelText = sprintf "%A - %.2f.%%" (k.Key) 
➥ percentageEmotion
            (labelText, percentageEmotion)
        ))

#1   Generates the event stream from the Twitter API
#2   Controls the rate of events to avoid overwhelming the consumer
#3   Filters the incoming messages to target only those in English
#4   Partitions the message by emotion analysis
#5   Flattens messages into one sequence of emotions with the count of favorites
#6   Maintains the state of the total partition of messages by emotion
#7   Calculates the total percentage of emotions and returns an observable to live update a chart


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":7,\"ch\":15},{\"line\":7,\"ch\":23}],[{\"line\":8,\"ch\":18},{\"line\":8,\"ch\":24}],[{\"line\":10,\"ch\":18},{\"line\":10,\"ch\":25}],[{\"line\":12,\"ch\":18},{\"line\":12,\"ch\":28}],[{\"line\":15,\"ch\":18},{\"line\":15,\"ch\":22}],[{\"line\":13,\"ch\":27},{\"line\":13,\"ch\":30}],[{\"line\":19,\"ch\":18},{\"line\":19,\"ch\":21}],[{\"line\":21,\"ch\":18},{\"line\":21,\"ch\":21}]]"}
!@%STYLE%@!
Dependency Injection in .NET

This is an excerpt from Manning's book Dependency Injection in .NET.

However, OBJECT COMPOSITION isn’t the only dimension of control that we remove, because a class also loses the ability to control the lifetime of the object. When a DEPENDENCY instance is injected into a class, the consumer doesn’t know when it was created, or when it will go out of scope. Many times, this is of no concern to the consumer, but in other cases, it may be.

DI gives us an opportunity to manage DEPENDENCIES in a uniform way. When consumers directly create and set up instances of DEPENDENCIES, each may do so in its own way, which may be inconsistent with how other consumers do it. We have no way to centrally manage DEPENDENCIES, and no easy way to address CROSS-CUTTING CONCERNS. With DI, we gain the ability to intercept each DEPENDENCY instance and act upon it before it’s passed to the consumer.

When a consumer like an ASP.NET MVC Controller needs to convert prices, it requires a Currency as a DEPENDENCY to perform the conversion. In the sample commerce application used in this book, the Money class used to represent prices has this conversion method:

public Money ConvertTo(Currency currency)

A consumer such as a Controller can supply a Currency instance to all prices to convert them, but the question now arises, which Currency instance?

Figure 6.3. We can design an interface so that it’s sufficiently coarse-grained that each method encapsulates all interactions with an external resource in a single batch. The Consumer invokes a method on the IResource interface. An implementation of that method could open a connection and invoke several methods against the external resource before closing the connection and returning a result to the consumer.

The consumer is never aware that some implementations may be opening and closing connections on its behalf.

Whenever possible, we should strive to design a consumer’s DEPENDENCIES so that we never need to explicitly deal with the lifetime of the DEPENDENCY at this level. There are, however, instances where we can’t do that.