<?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: Ahmed Elmehalawi</title>
    <description>The latest articles on DEV Community by Ahmed Elmehalawi (@ahmedelmehalawi).</description>
    <link>https://dev.to/ahmedelmehalawi</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%2F1946759%2F0267c2c8-b0bb-4ad6-b774-b4e4d18d0022.png</url>
      <title>DEV Community: Ahmed Elmehalawi</title>
      <link>https://dev.to/ahmedelmehalawi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmedelmehalawi"/>
    <language>en</language>
    <item>
      <title>Different ways to implement Shallow Copy in C#</title>
      <dc:creator>Ahmed Elmehalawi</dc:creator>
      <pubDate>Fri, 11 Oct 2024 12:09:20 +0000</pubDate>
      <link>https://dev.to/ahmedelmehalawi/different-ways-to-implement-shallow-copy-in-c-1n02</link>
      <guid>https://dev.to/ahmedelmehalawi/different-ways-to-implement-shallow-copy-in-c-1n02</guid>
      <description>&lt;p&gt;In C#, reference types are objects that are stored in the heap. When these types are assigned or passed, they share the same memory location. This behavior can lead to unintended consequences, as modifying one reference affects all other references pointing to the same object. In certain scenarios, it may be necessary to create a separate value copy from a reference type to avoid such side effects.&lt;/p&gt;




&lt;p&gt;Consider the following class, which contains several value-type properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public class Employee
    {
        public string FirstName { get; private set; }
        public string LastName { get; private set; }
        public string Title { get; private set; }


        public Employee(string firstName, string lastName, string title)
        {
            FirstName = firstName;
            LastName = lastName;
            Title = title;
        }
        public void UpdateName(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
        }

        public void UpdateTitle(string title)
        {
            Title = title;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose we create an instance of the Employee class:&lt;br&gt;
&lt;code&gt;Employee employeeInfo = new Employee("Ahmed", "Elmehalawi", "Software Engineer");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we then create another Employee object by copying the employeeInfo data:&lt;br&gt;
&lt;code&gt;Employee copiedEmployeeInfo = employeeInfo;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Any changes made to employeeInfo will be reflected in copiedEmployeeInfo. For example, updating the title using the UpdateTitle method:&lt;br&gt;
&lt;code&gt;employeeInfo.UpdateTitle("Project Manager")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This update will change the title to "Project Manager" for both employeeInfo and copiedEmployeeInfo, as both variables reference the same memory location on the heap.&lt;br&gt;
However, if the intention is to create a copy of the object rather than share the same reference, we need to perform a shallow copy. A shallow copy creates a new object but copies the original object’s properties, ensuring that changes made to one do not affect the other.&lt;/p&gt;
&lt;h2&gt;
  
  
  Ways to implement Shallow Copy in C#:
&lt;/h2&gt;




&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;em&gt;1. MemberwiseClone&lt;/em&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The MemberwiseClone method creates a shallow copy of the object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public Employee ShallowCopy()
{
    return (Employee)this.MemberwiseClone();
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage:-&lt;br&gt;
&lt;code&gt;Employee copiedEmployeeInfo = employeeInfo.ShallowCopy();&lt;/code&gt;&lt;br&gt;
In this case, any changes made to one instance will not reflect on the other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;em&gt;2. ICloneable interface&lt;/em&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Implementing the Clone method using the ICloneable interface, which also utilizes MemberwiseClone:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public object Clone()
{
    return (Employee)this.MemberwiseClone();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;em&gt;3. Copy Constructor&lt;/em&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
A copy constructor explicitly creates a new instance by copying the values of the original object's properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public Employee(Employee employee)
{
    FirstName = employee.FirstName;
    LastName = employee.LastName;
    Title = employee.Title;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage:&lt;br&gt;
&lt;code&gt;Employee copiedEmployeeInfo = new Employee(employeeInfo)&lt;/code&gt;;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;em&gt;4. JSON Serialization and Deserialization&lt;/em&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Another approach is to use JSON serialization and deserialization to create a copy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Employee copiedEmployeeInfo = JsonConvert.DeserializeObject&amp;lt;Employee&amp;gt;(JsonConvert.SerializeObject(employeeInfo));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;em&gt;5. Manual Property Copying&lt;/em&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
You can manually create a new object and assign values from the original object’s properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Employee copiedEmployeeInfo = new Employee(employeeInfo.FirstName, employeeInfo.LastName, employeeInfo.Title);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>API Testing using .http Files in .NET</title>
      <dc:creator>Ahmed Elmehalawi</dc:creator>
      <pubDate>Sat, 28 Sep 2024 10:50:52 +0000</pubDate>
      <link>https://dev.to/ahmedelmehalawi/api-testing-using-http-files-2gde</link>
      <guid>https://dev.to/ahmedelmehalawi/api-testing-using-http-files-2gde</guid>
      <description>&lt;p&gt;In .NET 8, a new type of file, the .http file, has been introduced in API projects. The purpose of this file is to simplify API testing directly by writing and using simple HTTP commands.&lt;/p&gt;

&lt;p&gt;The .http file is a plain text file that contains HTTP commands such as GET, POST, PUT, DELETE, and other requests used in APIs. Instead of relying on external tools like Postman or cURL, you can write your requests inside this file and run them directly from within Visual Studio or VS Code.&lt;br&gt;
The main goal is to provide a simple and fast environment to test the APIs you're working on. Instead of switching to external tools for sending requests, you can handle everything inside the IDE. This also helps dev teams share a set of ready-made requests for testing purposes.&lt;/p&gt;

&lt;p&gt;The file is quite simple and contains multiple requests in a specific format.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET https://localhost:5000/api/products
###
POST https://localhost:5000/api/products
Content-Type: application/json
{
 "name": "Product X",
 "price": 100
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One of the great features of .http files is that you can select which environment to run them in. For example, you might have an API running in different environments like Development and Production.&lt;/p&gt;

&lt;p&gt;You can create an .env file that contains different settings for each environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
 "dev": {
 "HostAddress": "https://localhost:5000"
 },
 "remote": {
 "HostAddress": "https://api.app.com"
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, use these variables inside the .http file:&lt;br&gt;
&lt;code&gt;GET {{HostAddress}}/api/products&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Covering Index</title>
      <dc:creator>Ahmed Elmehalawi</dc:creator>
      <pubDate>Sun, 18 Aug 2024 22:40:56 +0000</pubDate>
      <link>https://dev.to/ahmedelmehalawi/covering-index-3mni</link>
      <guid>https://dev.to/ahmedelmehalawi/covering-index-3mni</guid>
      <description>&lt;p&gt;One of the beautiful concepts in databases is the &lt;em&gt;Covering Index&lt;/em&gt;, which plays a significant role in improving query performance. &lt;br&gt;
Simply put, this index contains all the columns needed in a query without having to perform a lookup inside the data table to fetch the required columns.&lt;/p&gt;

&lt;p&gt;As we know, indexes enable the DBMS to access data more quickly, and indexes can be either clustered or non-clustered.&lt;/p&gt;

&lt;p&gt;Each table has one clustered index, and the clustered index represents the physical order of the records. So, if we have a table named &lt;code&gt;Student&lt;/code&gt; containing the following columns: &lt;code&gt;Id&lt;/code&gt;, &lt;code&gt;FirstName&lt;/code&gt;, &lt;code&gt;LastName&lt;/code&gt;, &lt;code&gt;Email&lt;/code&gt;, &lt;code&gt;GPA&lt;/code&gt;, and our clustered index is on the primary key (which is &lt;code&gt;Id&lt;/code&gt;), when we write a query to retrieve all the columns from &lt;code&gt;Student&lt;/code&gt; where &lt;code&gt;Id = 287&lt;/code&gt;, the clustered index will be used to reach that record and return the data.&lt;/p&gt;

&lt;p&gt;On the other hand, a non-clustered index is stored separately from the table data and contains the columns I specify, along with either the clustered index key (if it exists) or the location of the row in the heap data pages (if the table doesn't have a clustered index).&lt;/p&gt;

&lt;p&gt;So, if I create a non-clustered index on &lt;code&gt;GPA&lt;/code&gt;, it will store &lt;code&gt;GPA&lt;/code&gt; sorted in ascending order along with the clustered index key, which in our case is the &lt;code&gt;Student Id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If I need to retrieve all the columns from the table where the records match the condition &lt;code&gt;GPA &amp;gt; 2.8&lt;/code&gt;, the query will first access the non-clustered index to get the IDs of the students whose GPA is greater than 2.8. Then it will use the original IDs to perform a key lookup to retrieve the column values.&lt;/p&gt;

&lt;p&gt;This approach is much more performant than scanning every record in the original table, checking the GPA, and then retrieving the data.&lt;/p&gt;

&lt;p&gt;However, it still involves two steps: first, fetching the matched records using the non-clustered index and then going back to the table to get the data.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;Is there a way to fetch the data without going back to the table?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The answer is yes, and that's where the &lt;em&gt;Covering Index&lt;/em&gt; comes in.&lt;br&gt;
It's called a &lt;em&gt;Covering Index&lt;/em&gt; because it covers the query's select columns that are needed. &lt;br&gt;
Essentially, it's a non-clustered index with all the columns required for the query added to it, so it doesn't have to go back to the data table for a lookup. The data is already available within the index itself. Additionally, we can include the columns we need in the query without making them key columns in our non-clustered index.&lt;/p&gt;

&lt;p&gt;#sql &lt;br&gt;
#databases&lt;br&gt;
#covering_index&lt;br&gt;
#indexes&lt;br&gt;
#clustered_index&lt;br&gt;
#non_clustered_index&lt;br&gt;
#development&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
