adsense

Sunday, June 15, 2025

ASP.NET Core changes in .NET 9 MapStaticAssets

 MapStaticAssets is a new feature that enhances the delivery of static assets in web applications. It is intended as a  replacement for UseStaticFiles.  

MapStaticAssets offers key advantages over UseStaticFiles, including:

  • Build-time compression:

    • Uses gzip during development and gzip + Brotli during publish, minimizing asset sizes.

  • Content-based ETags:

    • Generates ETags from the SHA-256 hash of each file’s content, ensuring browsers only re-download files when their content changes


Read more here.

Cheers
Samitha

Sunday, May 18, 2025

EF Core Vs Dapper

 Both Dapper and EF Core are excellent ORM options suitable for projects of any size. The choice between them largely depends on personal or organizational preferences. Dapper appeals to those who prefer writing raw SQL queries, while EF Core is favored by those who prefer working with LINQ. Each has its own strengths and weaknesses


You can learn more on Dapper vs EF Core here.


Cheers

Samitha

Sunday, May 4, 2025

C# 14 New feature for Null conditional assignment

The preview of .NET 10 and the upcoming C# version introduces a potential new feature: null-conditional assignment. This aims to simplify code where you assign a value to a property only if the object isn't null.

Currently, you'd write:

if (classObj is not null)

{

  classObj .Property = 100;

}

With the new feature, this can be shortened to:

classObj ?.Property = 100;

This makes the code more concise and readable when dealing with potentially null objects.


Cheers,

Samitha

Wednesday, April 16, 2025

EF Core 10 Support for .NET 10 LeftJoin and RightJoin operators

 In earlier versions of EF Core, performing a LEFT JOIN in LINQ required a complex combination of SelectMany, GroupJoin, and DefaultIfEmpty.

With .NET 10, a new built-in LeftJoin method simplifies this process. It allows developers to write cleaner and more intuitive LINQ queries, and EF Core fully supports it in database queries.


Example:

var studentDepartments = context.Students

    .LeftJoin(

        inner: context.Departments,

        outerKeySelector: student => student.DepartmentID,

        innerKeySelector: department => department.ID,

        resultSelector: (student, department) => new

        {

            FirstName = student.FirstName,

            LastName = student.LastName,

            Department = department.Name ?? "[NONE]"

        });

Cheers
Samitha

Sunday, March 30, 2025

Entity Framework Selectmany

 In Entity Framework Core, the SelectMany operation flattens collections, which can lead to unexpected results if not carefully applied. For example, in a query that filters students and their active courses:

var activeCourses = await _employeeRepository

    .GetAllEmployees()

    .Where(employee=> employee.IsActive)

    .SelectMany(employee=> employee.PayeDetails)

    .Where(payDetail=> payDetail.IsTermnated)

    .ToListAsync();

The Where clause before SelectMany filters the employees, but after SelectMany, the query works with a flattened collection of pay details. This means the second Where clause filters the pay details, not the employees. As a result, you might get pay details from terminated employees because the filtering is done on the flattened collection of pay details, not the original employee collection. 

In some cases Selectmany can act like a CrossJoin as well.


Cheers

Samitha

Saturday, March 1, 2025

Ef .Net Core DbSet.ExecuteUpdate

 EF Core 7.0 introduces the ExecuteUpdate method, which allows for updating entities in the database based on a query result. However, it requires explicit specification of the changes to be made, as EF Core does not automatically detect them. Tracked entities will not remain in sync, and additional commands may need to be issued in a specific order to avoid violating database constraints (e.g., updating dependents before deleting a principal).

The ExecuteUpdate method complements, rather than replaces, the existing SaveChanges mechanism. It functions similarly to the ExecuteDelete method, but with the key difference that an update requires specifying which properties to update and how to do so, typically using calls to SetProperty.


Cheers.

Samitha

Sunday, February 16, 2025

Return no operation form a task implementation

 When you want to return a no operation result from a task implementation from an endpoint you can use following to return no operation performed.



 public Task LongRunningAsync()

        {

            // do some work

            var x = 1;     

         // Can't return 'null' 

            return Task.FromResult<object<(null);

        }


Cheers,

Samitha