concept composition in category c#

appears as: composition, composition
C# in Depth, Fourth Edition

This is an excerpt from Manning's book C# in Depth, Fourth Edition.

To reiterate a point I made earlier, it’s the combination of automatic wrapping and unwrapping that makes the async feature work so well with composition; async methods can consume the results of async methods easily, so you can build up complex systems from lots of small blocks. You can think of this as being a bit like LINQ: you write operations on each element of a sequence in LINQ, and the wrapping and unwrapping means you can apply those operations to sequences and get sequences back. In an async world, you rarely need to explicitly handle a task; instead, you await the task to consume it, and produce a result task automatically as part of the mechanism of the async method. Now that you know what an asynchronous method looks like, it’s easier to give examples to demonstrate the execution flow.

Anonymous types provide one kind of composition but with the huge restriction of being useful only within a block of code. You can’t declare that a method returns an anonymous type precisely because the return type can’t be named.

Functional Programming in C#: How to write better C# code

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.

We’ll dig deeper into the ideas of composition and statements vs. expressions in section 5.5.1.

Function composition is not only powerful and expressive but also pleasant to work with. It’s used to some extent in any programming style, but in FP it’s used extensively. For example, have you noticed that when you use LINQ to work with lists, you can get a lot done with only a few lines of code? That’s because LINQ is a functional API, designed with composition in mind.

Listing 5.1. Defining a function as the composition of two existing functions
Dependency Injection in .NET

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

Figure 3.6. When composing an application from many loosely coupled classes, the composition should take place as close to the application’s entry point as possible. The COMPOSITION ROOT composes the object graph, which subsequently performs the actual work of the application.

In this example, you’ll configure the sample commerce application from section 2.3. A large part of the task is to apply the configuration outlined in table 3.1, but you must also supply a similar configuration to support composition of the HomeController class. The following listing shows the configuration necessary to get the application up and running.

When we consider the internal architecture of MEF, it turns out that discovery and composition are decoupled. This means that it’s possible for Microsoft to evolve MEF in the direction of a true DI CONTAINER.[1] On the other hand, some DI CONTAINERS offer such powerful convention-based features that they may encroach on MEF in the future.

We can’t configure the container itself in any way—neither with imperative code nor through XML configuration. MEF only gives us one option to define imports and exports, and that is by applying attributes to parts. Attributes are part of the type they annotate, so we must view this mechanism as more explicit and early bound than even CODE AS CONFIGURATION. Catalogs, on the other hand, provide us with a great deal of flexibility because they enable us to pick the types we wish to include into a composition.

Real-World Functional Programming with Examples in F# and C#

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.

On the second line, the table shows a specific type of the snd function after the compiler figures out that the second element of the tuple has to be an integer. We can get this type if we substitute type parameter 'b from the first row with a type int. Now we have two functions that can be composed, because the return type on the second row is the same as the input type on the third row. Using composition, we join the functions together and get a function that calls the first one and passes the result of this call as an input to the second one. The resulting function has the same input type as the function on the second row and the same return type as the function on the third row. Listing 6.17 shows how we can rewrite the original code using function composition.

Listing 6.17. Using the function composition operator (F# Interactive)
> places |> List.map (fun x -> (snd >> statusByPopulation) x);; ❶ val it : string list = ["Village"; "Town"; "City"] > places |> List.map (snd >> statusByPopulation);; ❷ val it : string list = ["Village"; "Town"; "City"]

On the first line ❶, we call the composed function explicitly by giving it the tuple containing the city name and population as an argument. This is to demonstrate that a result of composition is a function that can be called using the usual syntax. However, the reason for using function composition is that we can use the composed function as an argument to other functions. In this case, the composed function takes a tuple and returns a string, so we can immediately use it as an argument to List.map to get a list of the statuses of the sample places ❷.

The example we’ve just seen shows that composition is a powerful principle, so you’re probably eager to know how we can implement the library that enables this. In the next few sections, we’ll implement F# and C# libraries for describing animations, and we’ll also see how combinators and method chains work.