<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Refaat Elabd</title>
    <description>The latest articles on DEV Community by Refaat Elabd (@refaatelabddev).</description>
    <link>https://dev.to/refaatelabddev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F657053%2F64495b23-c24c-476d-b859-06dc0b682fb6.jpeg</url>
      <title>DEV Community: Refaat Elabd</title>
      <link>https://dev.to/refaatelabddev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/refaatelabddev"/>
    <language>en</language>
    <item>
      <title>Mixin in C#</title>
      <dc:creator>Refaat Elabd</dc:creator>
      <pubDate>Fri, 04 Aug 2023 17:32:51 +0000</pubDate>
      <link>https://dev.to/refaatelabddev/mixin-in-c-43nj</link>
      <guid>https://dev.to/refaatelabddev/mixin-in-c-43nj</guid>
      <description>&lt;p&gt;In this post I will talk about Mixin concept and how to implement it in C# and I provide a code example to prove concept, So let's go ahead.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Mixins&lt;/strong&gt; are a language concept that &lt;strong&gt;allows a programmer to inject some code into a class.&lt;/strong&gt; Mixin programming is a style of software development, in which units of functionality are created in a class and then mixed in with other classes but which cannot itself be instantiated.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Mixins are useful in object oriented languages which only support single inheritance Like C#, Java, Kotlin, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A mixin class &lt;strong&gt;acts as the parent class&lt;/strong&gt;, containing the desired functionality.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In order to implement a mixin in c# we will use a &lt;em&gt;combination of interfaces and extension methods&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Let's dive into an example:&lt;/p&gt;

&lt;p&gt;Consider the &lt;strong&gt;Employee&lt;/strong&gt; Class that is a &lt;strong&gt;Person&lt;/strong&gt; needs to use &lt;strong&gt;work experience&lt;/strong&gt; functions&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc4da40svzisu2ob31d19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc4da40svzisu2ob31d19.png" alt="interface to extend functionality from"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Work experience extension methods:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmevve7o0saertssnxihi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmevve7o0saertssnxihi.png" alt="Work experience extension methods"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Employee Class: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp7802vst4oftmbtvvk70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp7802vst4oftmbtvvk70.png" alt="Employee class"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we can Simply get Employee Level:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwl33qsnjb48rw7u9gm6l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwl33qsnjb48rw7u9gm6l.png" alt="Client Using"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;output&lt;/strong&gt;: &lt;code&gt;Name Refaat, Level = Junior&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Does this mean that mixins can only have methods - what about fields?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No, they can have fields.
To do that we may apply a nested class contains needed fields and then associate the instance of this nested class with each object which implements the mixin interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;will use &lt;strong&gt;&lt;em&gt;ConditionalWeakTable&lt;/em&gt;&lt;/strong&gt; collection to associate mixin classes with fields in the example to let garbage collector cleaning up these Fields.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.conditionalweaktable-2?view=net-7.0" rel="noopener noreferrer"&gt;know more about ConditionalWeakTable&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final example&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's establish fields at &lt;em&gt;&lt;strong&gt;WorkExperienceProvider&lt;/strong&gt;&lt;/em&gt; static class that contains extention methods&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjcbwn7pt8lsx7uem09y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjcbwn7pt8lsx7uem09y.png" alt="Establish Fields"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Put years of experience set &amp;amp; get extention methods:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu9qt1mb3xl7i5v6v3gtk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu9qt1mb3xl7i5v6v3gtk.png" alt="years of experience set &amp;amp; get extention methods"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now we can update GetTechLevel method to not wait years of experience from client&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsm5ztauae0yeu3ad4bnc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsm5ztauae0yeu3ad4bnc.png" alt="GetTechLevel method"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can get Employee Level:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2v8mka3d3psfkpkx0ifx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2v8mka3d3psfkpkx0ifx.png" alt="Client Using"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; &lt;code&gt;Name Esraa, Level = Junior&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code sample&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnd8so2nfkunfifklykz9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnd8so2nfkunfifklykz9.png" alt="Whole Code Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It provides a mechanism for multiple inheritance by allowing one class to use common functionality from multiple classes, but without the complex semantics of multiple inheritance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code reusability: Mixins are useful when a programmer wants to share functionality between different classes. Instead of repeating the same code over and over again, the common functionality can simply be grouped into a mixin and then included into each class that requires it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run code and trace it to more illustration, Have fun&lt;/p&gt;

</description>
      <category>mixins</category>
      <category>csharp</category>
      <category>oop</category>
      <category>developer</category>
    </item>
  </channel>
</rss>
