<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>netstandard Archive - Robert Skibbe</title>
	<atom:link href="https://robbelroot.de/blog/tag/netstandard/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>alias RobbelRoot – Freelance Full Stack Developer .NET</description>
	<lastBuildDate>Thu, 28 Sep 2023 08:13:36 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://robbelroot.de/wp-content/uploads/2020/12/cropped-favicon-32x32.png</url>
	<title>netstandard Archive - Robert Skibbe</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>rskibbe.Core.Composition – Building better and more flexible objects</title>
		<link>https://robbelroot.de/blog/rskibbe-core-composition-building-better-and-more-flexible-objects/</link>
					<comments>https://robbelroot.de/blog/rskibbe-core-composition-building-better-and-more-flexible-objects/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Mon, 11 Sep 2023 22:01:31 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<category><![CDATA[NuGet Packages]]></category>
		<category><![CDATA[rskibbe]]></category>
		<category><![CDATA[rskibbe.Core]]></category>
		<category><![CDATA[rskibbe.Core.Composition]]></category>
		<category><![CDATA[aggregate]]></category>
		<category><![CDATA[combine]]></category>
		<category><![CDATA[composite]]></category>
		<category><![CDATA[composition]]></category>
		<category><![CDATA[core]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[netstandard]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[vbnet]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=16075</guid>

					<description><![CDATA[<p>What is rskibbe.Core.Composition? My NuGet package called &#8222;rskibbe.Core.Composition&#8220; is about helping you build more complex objects easier. It provides a common infrastructure for you to design your classes upon. It was private at first, but I&#8217;m using it in a lot of my projects, so I thought, why wouldn&#8217;t I &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/rskibbe-core-composition-building-better-and-more-flexible-objects/">rskibbe.Core.Composition – Building better and more flexible objects</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/09/rskibbe.Core_.Composition-Building-better-and-more-flexible-objects.png"><img fetchpriority="high" decoding="async" width="640" height="360" src="https://robbelroot.de/wp-content/uploads/2023/09/rskibbe.Core_.Composition-Building-better-and-more-flexible-objects.png" alt="rskibbe.Core.Composition - Building better and more flexible objects" class="wp-image-16078" title="rskibbe.Core.Composition - Building better and more flexible objects"/></a><figcaption class="wp-element-caption">rskibbe.Core.Composition &#8211; Building better and more flexible objects</figcaption></figure>






<h2 class="wp-block-heading">What is rskibbe.Core.Composition?</h2>



<p>My NuGet package called &#8222;rskibbe.Core.Composition&#8220; is about helping you build more complex objects easier. It provides a common infrastructure for you to design your classes upon. It was private at first, but I&#8217;m using it in a lot of my projects, so I thought, why wouldn&#8217;t I publish it.</p>



<p>If you need more info on how it&#8217;s done behind the scenes, feel free to scroll further down. In a section at the bottom, I&#8217;ve provided more detailed explanation on this package. This way you can comprehend the idea behind everything a bit more and maybe even extend on it.</p>



<p>I&#8217;ve also added some small class diagram to have everything visually summarized and in one simple spot.</p>



<style>
.info-banner {
    background: #03a9f4;
    padding: 2em;
    border-radius: 0.5em;
    color: whitesmoke;
}
.info-banner a {
    color: #FED766;
}
</style>



<h2 class="wp-block-heading">Quick example</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/09/Quick-example-rskibbe.Core_.Composition.png"><img decoding="async" width="640" height="360" src="https://robbelroot.de/wp-content/uploads/2023/09/Quick-example-rskibbe.Core_.Composition.png" alt="Quick example - rskibbe.Core.Composition" class="wp-image-16097" title="Quick example - rskibbe.Core.Composition"/></a><figcaption class="wp-element-caption">Quick example &#8211; rskibbe.Core.Composition</figcaption></figure>



<h3 class="wp-block-heading">Step 1 – Install the package</h3>



<p>Open the NuGet Package Manager and execute the following commands (or install it by GUI). This command will install the package to enable you using its interfaces and classes.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Install-Package rskibbe.Core.Composition</pre>



<h3 class="wp-block-heading">Step 2 – Start composing components</h3>



<p>Now you are already ready to go and start composing your own components / classes. Please move to <strong><a href="#detailed-explanation">the detailed explanation further down</a></strong> to get a better hang of the package.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="quickstart-object">public class MyComponent : Component
{

   // add different sub-components to your class
   // extend it e. g. with interfaces delegating their actions to
   // the corresponding sub-components

}</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="quickstart-object">Public Class MyComponent
        Inherits Component

   ' add different sub-components to your class
   ' extend it e. g. with interfaces delegating their actions to
   ' the corresponding sub-components

End Class</pre>



<h2 class="wp-block-heading">Using composition over inheritance is the deal, right?</h2>



<p>When creating your classes there can be different scenarios where you sometimes need different approaches to solve designing them. Fore sure, we could like mostly use typical inheritance like &#8222;a robot is a machine&#8220; &#8211; but what if those machines are so damn (pardon me) complex, that you can&#8217;t fit them the right way by inheriting? This is where a typical composition scenario could fit for you!</p>



<p><strong>To get to know more about the &#8222;composition over inheritance pattern&#8220;, I will write a separate blog post, but for now this post here is about my corresponding NuGet package &#8222;rskibbe.Core.Composition&#8220;.</strong> This way, you will have a common denominator where you can build upon and you don&#8217;t have to start from scratch as well.</p>



<h2 class="wp-block-heading" id="detailed-explanation">Detailed explanation</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/09/Detailed-explanation-rskibbe.Core_.Composition.png"><img decoding="async" width="640" height="360" src="https://robbelroot.de/wp-content/uploads/2023/09/Detailed-explanation-rskibbe.Core_.Composition.png" alt="Detailed explanation - rskibbe.Core.Composition" class="wp-image-16110" title="Detailed explanation - rskibbe.Core.Composition"/></a><figcaption class="wp-element-caption">Detailed explanation &#8211; rskibbe.Core.Composition</figcaption></figure>



<h3 class="wp-block-heading">The core conception</h3>



<p>When designing or &#8211; in other words &#8211; composing your object of desire, I found it pretty matching, to call the absolute &#8222;base&#8220; actually &#8222;<strong>IComposable</strong>&#8222;. It&#8217;s something you can almost magically compose out of different objects which then builds a new object – what I&#8217;m calling an &#8222;<strong>IComponent</strong>&#8222;. So basically the &#8222;IComponent&#8220; interface inherits from the &#8222;IComspoable&#8220; interface.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/09/A-component-is-a-composable-being-composed-of-different-sub-components.png"><img loading="lazy" decoding="async" width="571" height="194" src="https://robbelroot.de/wp-content/uploads/2023/09/A-component-is-a-composable-being-composed-of-different-sub-components.png" alt="A component is a composable being composed of different sub-components" class="wp-image-16114" title="A component is a composable being composed of different sub-components"/></a><figcaption class="wp-element-caption">A component is a composable being composed of different sub-components</figcaption></figure>



<p>To actually do / execute this kind of composition, you will need to be able to <strong>dynamically add sub-components</strong> to your composable(s). Maybe you want to even <strong>dynamically remove the components</strong> again? This is why the following methods have been implement in the class &#8222;Composable&#8220;.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/09/Component-class-inheriting-from-the-Composable-class-1.png"><img loading="lazy" decoding="async" width="728" height="337" src="https://robbelroot.de/wp-content/uploads/2023/09/Component-class-inheriting-from-the-Composable-class-1.png" alt="Component class inheriting from the Composable class" class="wp-image-16123" title="Component class inheriting from the Composable class"/></a><figcaption class="wp-element-caption">Component class inheriting from the Composable class</figcaption></figure>



<p>Adding and removing components won&#8217;t help much, if you can&#8217;t access and use them. So &#8211; for sure &#8211; and as visually mentioned inside the image above, there are methods to get your sub-components from a &#8222;parent&#8220;.</p>



<h3 class="wp-block-heading">Concrete example – A machine</h3>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/09/A-composed-robot-out-of-machine-parts-being-a-composable-object.png"><img loading="lazy" decoding="async" width="640" height="427" src="https://robbelroot.de/wp-content/uploads/2023/09/A-composed-robot-out-of-machine-parts-being-a-composable-object.png" alt="A composed robot out of machine parts being a composable object" class="wp-image-16200" title="A composed robot out of machine parts being a composable object"/></a><figcaption class="wp-element-caption">A composed robot out of machine parts being a composable object</figcaption></figure>



<p>Let&#8217;s now start a complete example of our first designed component being composed of composables – what a language-play. Keep in mind, that this actually – just an example, feel free to change it in any way fitting your needs&#8230; For this example, we&#8217;re taking a machine which could possibly connect many things to itself to use them.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="convrete-example">// a base class for some sort of common contextual denominator
class Machine : Composable
{

  public Machine()
  {
    // instantiated automatically - needs an empty constructor
    AddComponent&lt;LightComponent>();
    // manual instantiation, maybe with DI whatever
    var giantPowerBattery = new GiantPowerBattery();
    var laserBeamComponent = new LaserBeamComponent(giantPowerBattery);
    AddComponent(laserBeamComponent);
  }

}

// component 1
class LightComponent : Component
{

    public void Toggle() { /* some code.. */ }
    public void TurnOn() { /* some code.. */ }
    public void TurnOff() { /* some code */ }

}

// component 2
class LaserBeamComponent: Component
{

    IEnergySource _energySource;

    // having a ctor with dependency
    public LaserBeamComponent(IEnergySource energySource)
    {
        _energySource = energySource;
    }

    public void Shoot() { // some code.. }

}

// just some helper stuff..
interface IEnergySource { /* .. */ }

class GiantPowerBattery : IEnergySource { /* .. */ }</pre>



<h3 class="wp-block-heading">Using the sub-components</h3>



<p>Above, we just provided the composition side of things while composing our machine, but we didn&#8217;t actually use anything. If a machine wants to &#8222;shoot&#8220; with our &#8222;LaserBeamComponent&#8220; or if it wants to toggle the lights, it has to resolve the corresponding thing and use it. So we could just write something like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="using-sub-components">class Machine : Composable
{

  // rest of the machine code from above

  public void ToggleLight()
  {
    var light = GetComponent&lt;LightComponent>();
    light.Toggle();
  }

  // or shorter
  public void ToggleLight2()
    => GetComponent&lt;LightComponent>().Toggle();

  // rest of the machine code from above

}

// somewhere executable
Machine machine = new Machine();
machine.ToggleLight();

// maybe add more things?
// and call them dynamically?</pre>



<h3 class="wp-block-heading">A word on component types</h3>



<p>As you&#8217;ve probably seen in the example from above here, concrete types were used (in the constructors), like:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="word-on-component-types">AddComponent&lt;LightComponent>();</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="word-on-component-types">AddComponent(Of LightComponent)()</pre>



<p>The problem is now, that we would have a more strict dependency on the &#8222;LightComponent&#8220;. I would prefer to rely on abstractions (interfaces) instead. And this is no probem, as you can just use other overloads of the &#8222;AddComponent&#8220; method:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class Machine : Composable
{

  public Machine()
  {
    // using an interface alias when calling add
    AddComponent&lt;LightComponent, ILight>

    // or with an instance
    // remember to actually implement the interface here...
    LightComponent light = new LightComponent();
    AddComponent&lt;ILight>(light);
  }

  public void ToggleLight()
    => GetComponent&lt;ILight>().Toggle();

}

interface ILight : IComponent
{
  void Toggle();
  void TurnOn();
  void TurnOff();
}</pre>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<h3 class="wp-block-heading">More reusable functionality with interface traits</h3>



<p>Imagine you&#8217;re having two objects being able to do pretty much the same. A &#8222;light-aware-machine&#8220; &#8211; like the one above &#8211; could toggle their light, a car could do so and your gaming console as well. Maybe we don&#8217;t want to create stuff like &#8222;TurnOn&#8220;, &#8222;TurnOff&#8220;, etc. again and again. As we can&#8217;t inherit from a base class implementing it, we can just use the &#8211; sort of &#8211; &#8222;Trait&#8220; feature from C#.</p>



<p>I&#8217;m talking about default implementations which interfaces can provide since C# version 8. Sadly you can&#8217;t have states in interfaces, so properties still need to be implemented. You could potentially define your interface trait like this: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    interface ILightTrait : IComponent
    {

        public void ToggleLight()
            => GetComponent&lt;ILight>().Toggle();

        public void TurnOnLight()
            => GetComponent&lt;ILight>().TurnOn();

        public void TurnOffLight()
            => GetComponent&lt;ILight>().TurnOff();

    }</pre>
</div></div>



<p>After defining that interface, every component – which should have a &#8222;Light functionality&#8220; could now just inherit from that interface. That means a machine, a gaming console, whatever could just inherit the functionality from that interface having the possibility to call the defined methods. When having like those 1 liners of methods, it&#8217;s no big deal, but as soon as it starts growing, it can make a big difference.</p>


<p class="rr-alert warning" style="background:#fff8c4;"><img decoding="async" alt="Warning notice" src="https://robbelroot.de/wp-content/themes/pinboard-child/imgs/warning.png"><span style="color:black;align-self: center;">Keep in mind that default implementation style used interfaces require you to use them as interface casted type, not the actual type. Take a look at the following two examples.</span></p>



<p>Let&#8217;s now define two classes which are able to toggle their lights and then use them. Our trait helps us not having to redefine everything from scratch.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    class GamingConsole : Composable, ILightTrait
    {

        public GamingConsole()
        {
            AddComponent&lt;LightComponent, ILightComponent>();
        }

    }

    class Car : Composable, ILightTrait
    {

        public Car()
        {
            AddComponent&lt;LightComponent, ILightComponent>();
        }

    }

    // more...</pre>



<p>Now let&#8217;s actually use these, care for the warning inside the yellow box from above. You have to cast the object to the interface type to actually use the trait-defined methods.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">GamingConsole gamingConsole

// won't work due to how default implementations are usable
gamingConsole.TurnOnLight();

// need's to look like this instead
((ILightTrait)this).TurnOnLight();</pre>



<h3 class="wp-block-heading">Publishing property notifications</h3>



<p>When building your complex objects, it could be useful to be able to publish &#8222;INotifyPropertyChanged&#8220; notifications. As both classes (Composable and Component) directly or indirectly inherit from &#8222;PropertyChangedBase&#8220;, you can do so pretty easily by just calling &#8222;NotifyOfPropertyChange&#8220;. Just provide the changed property name or let it figure it out itself by using the &#8222;CallerMemberNameAttribute&#8220;.</p>



<p>This could look like the following property implementation:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        bool _isOn;

        public bool IsOn
        {
            get => _isOn;
            set
            {
                if (_isOn == value)
                    return;
                // will automatically use "IsOn" as property name
                NotifyOfPropertyChange();
                // provide a name manually - but in favor with nameof()
                // NotifyOfPropertyChange(nameof(IsOn));
                // or completely manual
                // NotifyOfPropertyChange("IsOn");
            }
        }</pre>
<p>Der Beitrag <a href="https://robbelroot.de/blog/rskibbe-core-composition-building-better-and-more-flexible-objects/">rskibbe.Core.Composition – Building better and more flexible objects</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/rskibbe-core-composition-building-better-and-more-flexible-objects/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
