by Joche Ojeda | Jan 12, 2025 | ADO.NET, C#, CPU, dotnet, ORM, XAF, XPO
Introduction
In the .NET ecosystem, “AnyCPU” is often considered a silver bullet for cross-platform deployment. However, this assumption can lead to significant problems when your application depends on native assemblies. In this post, I want to share a personal story that highlights how I discovered these limitations and how native dependencies affect the true portability of AnyCPU applications, especially for database access through ADO.NET and popular ORMs.
My Journey to Understanding AnyCPU’s Limitations
Every year, around Thanksgiving or Christmas, I visit my friend, brother, and business partner Javier. Two years ago, during one of these visits, I made a decision that would lead me to a pivotal realization about AnyCPU architecture.
At the time, I was tired of traveling with my bulky MSI GE72 Apache Pro-24 gaming laptop. According to MSI’s official specifications, it weighed 5.95 pounds—but that number didn’t include the hefty charger, which brought the total to around 12 pounds. Later, I upgraded to an MSI GF63 Thin, which was lighter at 4.10 pounds—but with the charger, it was still around 7.5 pounds. Lugging these laptops through airports felt like a workout.
Determined to travel lighter, I purchased a MacBook Air with the M2 chip. At just 2.7 pounds, including the charger, the MacBook Air felt like a breath of fresh air. The Apple Silicon chip was incredibly fast, and I immediately fell in love with the machine.
Having used a MacBook Pro with Bootcamp and Windows 7 years ago, I thought I could recreate that experience by running a Windows virtual machine on my MacBook Air to check projects and do some light development while traveling.
The Virtualization Experiment
As someone who loves virtualization, I eagerly set up a Windows virtual machine on my MacBook Air. I grabbed my trusty Windows x64 ISO, set up the virtual machine, and attempted to boot it—but it failed. I quickly realized the issue was related to CPU architecture. My x64 ISO wasn’t compatible with the ARM-based M2 chip.
Undeterred, I downloaded a Windows 11 ISO for ARM architecture and created the VM. Success! Windows was up and running, and I installed Visual Studio along with my essential development tools, including DevExpress XPO (my favorite ORM).
The Demo Disaster
The real test came during a trip to Dubai, where I was scheduled to give a live demo showcasing how quickly you can develop Line-of-Business (LOB) apps with XAF. Everything started smoothly until I tried to connect my XAF app to the database. Despite my best efforts, the connection failed.
In the middle of the demo, I switched to an in-memory data provider to salvage the presentation. After the demo, I dug into the issue and realized the root cause was related to the CPU architecture. The native database drivers I was using weren’t compatible with the ARM architecture.
A Familiar Problem
This situation reminded me of the transition from x86 to x64 years ago. Back then, I encountered similar issues where native drivers wouldn’t load unless they matched the process architecture.
The Native Dependency Challenge
Platform-Specific Loading Requirements
Native DLLs must exactly match the CPU architecture of your application:
- If your app runs as x86, it can only load x86 native DLLs.
- If running as x64, it requires x64 native DLLs.
- ARM requires ARM-specific binaries.
- ARM64 requires ARM64-specific binaries.
There is no flexibility—attempting to load a DLL compiled for a different architecture results in an immediate failure.
How Native Libraries are Loaded
When your application loads a native DLL, the operating system follows a specific search pattern:
- The application’s directory
- System directories (System32/SysWOW64)
- Directories listed in the PATH environment variable
Crucially, these native libraries must match the exact architecture of the running process.
// This seemingly simple code
[DllImport("native.dll")]
static extern void NativeMethod();
// Actually requires:
// - native.dll compiled for x86 when running as 32-bit
// - native.dll compiled for x64 when running as 64-bit
// - native.dll compiled for ARM64 when running on ARM64
The SQL Server Example
Let’s look at SQL Server connectivity, a common scenario where the AnyCPU illusion breaks down:
// Traditional ADO.NET connection
using (var connection = new SqlConnection(connectionString))
{
// This requires SQL Native Client
// Which must match the process architecture
await connection.OpenAsync();
}
Even though your application is compiled as AnyCPU, the SQL Native Client must match the process architecture. This becomes particularly problematic on newer architectures like ARM64, where native drivers may not be available.
Impact on ORMs
Entity Framework Core
Entity Framework Core, despite its modern design, still relies on database providers that may have native dependencies:
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// This configuration depends on:
// 1. SQL Native Client
// 2. Microsoft.Data.SqlClient native components
optionsBuilder.UseSqlServer(connectionString);
}
}
DevExpress XPO
DevExpress XPO faces similar challenges:
// XPO configuration
string connectionString = MSSqlConnectionProvider.GetConnectionString("server", "database");
XpoDefault.DataLayer = XpoDefault.GetDataLayer(connectionString, AutoCreateOption.DatabaseAndSchema);
// The MSSqlConnectionProvider relies on the same native SQL Server components
Solutions and Best Practices
1. Architecture-Specific Deployment
Instead of relying on AnyCPU, consider creating architecture-specific builds:
<PropertyGroup>
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
</PropertyGroup>
2. Runtime Provider Selection
Implement smart provider selection based on the current architecture:
public static class DatabaseProviderFactory
{
public static IDbConnection GetProvider()
{
return RuntimeInformation.ProcessArchitecture switch
{
Architecture.X86 => new SqlConnection(), // x86 native provider
Architecture.X64 => new SqlConnection(), // x64 native provider
Architecture.Arm64 => new Microsoft.Data.SqlClient.SqlConnection(), // ARM64 support
_ => throw new PlatformNotSupportedException()
};
}
}
3. Managed Fallbacks
Implement fallback strategies when native providers aren’t available:
public class DatabaseConnection
{
public async Task<IDbConnection> CreateConnectionAsync()
{
try
{
var connection = new SqlConnection(_connectionString);
await connection.OpenAsync();
return connection;
}
catch (DllNotFoundException)
{
var managedConnection = new Microsoft.Data.SqlClient.SqlConnection(_connectionString);
await managedConnection.OpenAsync();
return managedConnection;
}
}
}
4. Deployment Considerations
- Include all necessary native dependencies for each target architecture.
- Use architecture-specific directories in your deployment.
- Consider self-contained deployment to include the correct runtime.
Real-World Implications
This experience taught me that while AnyCPU provides excellent flexibility for managed code, it has limitations when dealing with native dependencies. These limitations become more apparent in scenarios like cloud deployments, ARM64 devices, and live demos.
Conclusion
The transition to ARM architecture is accelerating, and understanding the nuances of AnyCPU and native dependencies is more important than ever. By planning for architecture-specific deployments and implementing fallback strategies, you can build more resilient applications that can thrive in a multi-architecture world.
by Joche Ojeda | Jun 10, 2024 | Blockchain, C#, Smart contracts, Stratis
Introduction
Blockchain technology has revolutionized various industries by providing a decentralized and secure way to manage data and transactions. At the heart of this innovation are smart contracts—self-executing contracts with the terms directly written into code. My journey into blockchain development began with the excitement of these possibilities, but it also came with challenges, particularly with the Solidity programming language. However, everything changed when I discovered the Stratis platform, which supports smart contracts using C#, making development much more accessible for me. In this article, I’ll share my experiences, challenges, and the eventual breakthrough that came with Stratis.
Challenges with Solidity
Solidity is the most popular language for writing smart contracts on Ethereum, but it has a steep learning curve. My background in programming didn’t include a lot of JavaScript-like languages, so adapting to Solidity’s syntax and concepts was daunting. The process of writing, testing, and deploying smart contracts often felt cumbersome. Debugging was a particular pain point, with cryptic error messages and a lack of mature tooling compared to more established programming environments.
The complexity and frustration of dealing with these issues made me seek an alternative that could leverage my existing programming skills. I wanted a platform that was easier to work with and more aligned with languages I was already comfortable with. This search led me to discover Stratis.
Introduction to Stratis
Stratis is a blockchain development platform designed to meet the needs of enterprises and developers by offering a simpler and more efficient way to build blockchain solutions. What caught my attention was its support for C#—a language I was already proficient in. Stratis allows developers to create smart contracts using C#, integrating seamlessly with the .NET ecosystem.
This discovery was a game-changer for me. The prospect of using a familiar language in a robust development environment like Visual Studio, combined with the powerful features of Stratis, promised a much smoother and more productive development experience.
Why Stratis Stood Out
The primary benefit of using C# over Solidity is the familiarity and maturity of the development tools. With C#, I could leverage the rich ecosystem of libraries, tools, and frameworks available in the .NET environment. This not only sped up the development process but also reduced the time spent on debugging and testing.
Stratis offers a comprehensive suite of tools designed to simplify blockchain development. The Stratis Full Node, for instance, provides a fully functional blockchain node that can be easily integrated into existing applications. Additionally, Stratis offers a smart contract template for Visual Studio, making it straightforward to start building and deploying smart contracts.
Another significant advantage is the support and community around Stratis. The documentation is thorough, and the community is active, providing a wealth of resources and assistance for developers at all levels.
Conclusion
Transitioning from Solidity to Stratis was a pivotal moment in my blockchain development journey. The challenges I faced with Solidity were mitigated by the ease and familiarity of C#. Stratis provided a robust and efficient platform that significantly improved my development workflow.
In the next article, I will dive into the practical steps of setting up the Stratis development environment. We’ll cover everything you need to get started, from installing the necessary tools to configuring your first Stratis Full Node. Stay tuned for a detailed guide that will set the foundation for your journey into C# smart contract development.
by Joche Ojeda | Jun 10, 2024 | C#, Data Synchronization, dotnet
The Ephemeral State of SOLID Code: Capturing the Perfect Snapshot
In the world of software development, the SOLID principles are often upheld as the gold standard for designing maintainable and scalable code. These principles — Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion — form the bedrock of robust object-oriented design. However, achieving a state where code fully adheres to these principles is a fleeting moment, much like capturing a perfect snapshot in time.
What Does It Mean for Code to Be in a SOLID State?
A SOLID state in source code is a condition where the code perfectly aligns with all five SOLID principles. This means:
- Single Responsibility Principle (SRP): Every class has one, and only one, reason to change.
- Open/Closed Principle (OCP): Software entities should be open for extension but closed for modification.
- Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types.
- Interface Segregation Principle (ISP): No client should be forced to depend on methods it does not use.
- Dependency Inversion Principle (DIP): Depend on abstractions, not concretions.
In this state, the codebase is a model of clarity, flexibility, and robustness. But this state is inherently transient.
The Moment of SOLID Perfection
The reality of software development is that code is in a constant state of flux. New features are added, bugs are fixed, and refactoring is a continuous process. During these periods of active development, maintaining perfect adherence to SOLID principles is challenging. The code may temporarily violate one or more principles as developers refactor or introduce new functionality.
The truly SOLID state can thus be seen as a snapshot — a moment frozen in time when the code perfectly adheres to all five principles. This moment typically occurs:
- Post-Refactoring: After a significant refactoring effort, where the focus has been on aligning the code with SOLID principles.
- Before Major Changes: Just before starting a new major feature or overhaul, the existing codebase might be in a perfect SOLID state.
- Code Reviews: Following a rigorous code review process, where adherence to SOLID principles is explicitly checked and enforced.
- Milestone Deliveries: Before delivering a major milestone or release, when the code is thoroughly tested and cleaned up.
The Nature of Active Development
Active development is a chaotic process. As new requirements emerge and priorities shift, developers might temporarily sacrifice adherence to SOLID principles for the sake of rapid progress or to meet deadlines. This is a natural part of the development cycle. The key is to recognize that while the code may deviate from these principles during active development, the goal is to continually steer it back towards a SOLID state.
The SOLID State as Nirvana
Achieving a perfect SOLID state can be likened to reaching nirvana — an ideal that is almost impossible to fully attain. Just as nirvana represents a state of ultimate peace and enlightenment, a perfectly SOLID codebase represents the pinnacle of software design. However, this state is incredibly difficult to reach and even harder to maintain. Therefore, it is more practical to view adherence to SOLID principles as a spectrum rather than a binary state.
Measuring SOLID Adherence
Instead of aiming for an elusive perfect state, it’s more pragmatic to measure adherence to SOLID principles using metrics. Tools and techniques can help quantify how well your code aligns with each principle, providing a percentage that reflects its current state. These metrics can include:
- Class Responsibility: Assessing the number of responsibilities each class has to evaluate adherence to SRP.
- Change Impact Analysis: Measuring the extent to which modifications to the code require changes in other parts of the system, reflecting adherence to OCP.
- Subtype Tests: Ensuring subclasses can replace their base classes without altering the correctness of the program, in line with LSP.
- Interface Utilization: Analyzing the usage of interfaces to ensure they are not overly broad, adhering to ISP.
- Dependency Metrics: Evaluating dependencies between high-level and low-level modules, supporting DIP.
By regularly measuring these metrics, developers can maintain a clear view of how their code is evolving in relation to SOLID principles. This approach allows for continuous improvement and helps teams prioritize refactoring efforts where they are most needed.
Embracing the Snapshot
Understanding that a perfectly SOLID state is a temporary snapshot can help developers maintain a healthy perspective. It’s crucial to strive for SOLID principles as a guiding star but also to accept that deviations are part of the journey. Regular refactoring sessions, continuous integration practices, and diligent code reviews are essential practices to frequently bring the code back to a SOLID state.
Conclusion
In conclusion, a SOLID state of source code is a valuable but ephemeral achievement, akin to reaching nirvana in the realm of software development. It represents a moment of perfection in the ongoing evolution of a software project. By recognizing this, developers can better manage their expectations and maintain a balance between striving for perfection and the practical realities of software development. Embrace the snapshot of SOLID perfection when it occurs, but also understand that the true measure of a healthy codebase is its ability to evolve while frequently realigning with these timeless principles, using metrics and percentages to guide the way.
by Joche Ojeda | Jun 5, 2024 | C#, Data Synchronization
Introduction
In modern API development, choosing the correct return type is crucial for performance, flexibility, and maintainability. In my SyncFramework server API, I opted to use strings as the return type. This decision stems from the need to serialize messages efficiently and flexibly, ensuring seamless communication between the server and client. This article explores the rationale behind this choice, specifically focusing on C# code with HttpClient and Web API on the server side.
The Problem
When building APIs, data serialization and deserialization are fundamental operations. Typically, APIs return objects that are automatically serialized into JSON or XML. While this approach is straightforward, it can introduce several challenges:
- Performance Overhead: Automatic serialization/deserialization can add unnecessary overhead, especially for large or complex data structures.
- Lack of Flexibility: Relying on default serialization mechanisms can limit control over the serialization process, making it difficult to customize data formats or handle specific serialization requirements.
- Interoperability Issues: Different clients may require different data formats. Sticking to a single format can lead to compatibility issues.
The Solution: Using Strings
To address these challenges, I decided to use strings as the return type for my API. Here’s why:
- Control Over Serialization: By returning a string, I can serialize the data myself, ensuring that the format meets specific requirements. This control is essential for optimizing the data format and ensuring compatibility with various clients.
- Performance Optimization: Custom serialization allows me to optimize the data structure, potentially reducing the size of the serialized data and improving transmission efficiency. For example, converting a complex object to a compressed byte array and then encoding it as a string can save bandwidth.
- Flexibility: Using strings enables me to easily switch between different serialization formats (e.g., JSON, XML, binary) based on the client’s needs without changing the API contract. This flexibility is crucial for maintaining backward compatibility and supporting multiple client types.
Implementation in C#
Here’s a practical example of how this approach is implemented using C#:
Server Side: Web API
using System;
using System.Text;
using System.Web.Http;
public class MyApiController : ApiController
{
[HttpGet]
[Route("api/getdata")]
public IHttpActionResult GetData()
{
var data = new MyData
{
Id = 1,
Name = "Sample Data"
};
// Custom serialization to JSON string
var serializedData = SerializeData(data);
return Ok(serializedData);
}
private string SerializeData(MyData data)
{
// Use custom serialization logic (e.g., JSON, XML, or binary)
return Newtonsoft.Json.JsonConvert.SerializeObject(data);
}
}
public class MyData
{
public int Id { get; set; }
public string Name { get; set; }
}
Client Side: HttpClient
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class ApiClient
{
private readonly HttpClient _httpClient;
public ApiClient()
{
_httpClient = new HttpClient();
}
public async Task GetDataAsync()
{
var response = await _httpClient.GetStringAsync("http://localhost/api/getdata");
// Custom deserialization from JSON string
return DeserializeData(response);
}
private MyData DeserializeData(string serializedData)
{
// Use custom deserialization logic (e.g., JSON, XML, or binary)
return Newtonsoft.Json.JsonConvert.DeserializeObject(serializedData);
}
}
public class MyData
{
public int Id { get; set; }
public string Name { get; set; }
}
Benefits Realized
By using strings as the return type, the SynFramework server API achieves several benefits:
- Enhanced Performance: Custom serialization reduces the payload size and improves response times.
- Greater Flexibility: The ability to easily switch serialization formats ensures compatibility with various clients.
- Better Control: Custom serialization allows fine-tuning of the data format, improving both performance and interoperability.
Conclusion
Choosing strings as the return type for the SyncFramework server API offers significant advantages in terms of performance, flexibility, and control over the serialization process. This approach simplifies the management of data formats, ensures efficient data transmission, and enhances compatibility with diverse clients. For developers working with C# and Web API, this strategy provides a robust solution for handling API responses effectively.
by Joche Ojeda | Jun 3, 2024 | C#, Data Synchronization
Writing Reusable Tests for SyncFramework Interfaces in C#
When creating a robust database synchronization framework like SyncFramework, ensuring that each component adheres to its defined interface is crucial. Reusable tests for interfaces are an essential aspect of this verification process. Here’s how you can approach writing reusable tests for your interfaces in C#:
1. Understand the Importance of Interface Testing
Interfaces define contracts that all implementing classes must follow. By testing these interfaces, you ensure that every implementation behaves as expected. This is especially important in frameworks like SyncFramework, where different components (e.g., IDeltaStore
) need to be interchangeable.
2. Create Base Test Classes
Create abstract test classes for each interface. These test classes should contain all the tests that verify the behavior defined by the interface.
using Microsoft.VisualStudio.TestTools.UnitTesting;
public abstract class BaseDeltaStoreTest
{
protected abstract IDeltaStore GetDeltaStore();
[TestMethod]
public void TestAddDelta()
{
var deltaStore = GetDeltaStore();
deltaStore.AddDelta("delta1");
Assert.IsTrue(deltaStore.ContainsDelta("delta1"));
}
[TestMethod]
public void TestRemoveDelta()
{
var deltaStore = GetDeltaStore();
deltaStore.AddDelta("delta2");
deltaStore.RemoveDelta("delta2");
Assert.IsFalse(deltaStore.ContainsDelta("delta2"));
}
// Add more tests to cover all methods in IDeltaStore
}
3. Implement Concrete Test Classes
For each implementation of the interface, create a concrete test class that inherits from the base test class and provides an implementation for the abstract method to instantiate the concrete class.
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class ConcreteDeltaStoreTest : BaseDeltaStoreTest
{
protected override IDeltaStore GetDeltaStore()
{
return new ConcreteDeltaStore();
}
}
4. Use a Testing Framework
Utilize a robust testing framework such as MSTest, NUnit, or xUnit to ensure all tests are run across all implementations.
5. Automate Testing
Integrate your tests into your CI/CD pipeline to ensure that every change is automatically tested across all implementations. This ensures that any new implementation or modification adheres to the interface contracts.
6. Document Your Tests
Clearly document your tests and the rationale behind reusable tests for interfaces. This will help other developers understand the importance of these tests and encourage them to add tests for new implementations.
Example of Full Implementation
// IDeltaStore Interface
public interface IDeltaStore
{
void AddDelta(string delta);
void RemoveDelta(string delta);
bool ContainsDelta(string delta);
}
// Base Test Class
using Microsoft.VisualStudio.TestTools.UnitTesting;
public abstract class BaseDeltaStoreTest
{
protected abstract IDeltaStore GetDeltaStore();
[TestMethod]
public void TestAddDelta()
{
var deltaStore = GetDeltaStore();
deltaStore.AddDelta("delta1");
Assert.IsTrue(deltaStore.ContainsDelta("delta1"));
}
[TestMethod]
public void TestRemoveDelta()
{
var deltaStore = GetDeltaStore();
deltaStore.AddDelta("delta2");
deltaStore.RemoveDelta("delta2");
Assert.IsFalse(deltaStore.ContainsDelta("delta2"));
}
// Add more tests to cover all methods in IDeltaStore
}
// Concrete Implementation
public class ConcreteDeltaStore : IDeltaStore
{
private readonly HashSet _deltas = new HashSet();
public void AddDelta(string delta)
{
_deltas.Add(delta);
}
public void RemoveDelta(string delta)
{
_deltas.Remove(delta);
}
public bool ContainsDelta(string delta)
{
return _deltas.Contains(delta);
}
}
// Concrete Implementation Test Class
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class ConcreteDeltaStoreTest : BaseDeltaStoreTest
{
protected override IDeltaStore GetDeltaStore()
{
return new ConcreteDeltaStore();
}
}
// Running the tests
// Ensure to use a test runner compatible with MSTest to execute the tests
by Joche Ojeda | May 15, 2024 | C#, dotnet, Linux, Ubuntu, WSL
Hello, dear readers! Today, we’re going to talk about something called the Windows Subsystem for Linux, or WSL for short. Now, don’t worry if you’re not a tech wizard – this guide is meant to be approachable for everyone!
What is WSL?
In simple terms, WSL is a feature in Windows that allows you to use Linux right within your Windows system. Think of it as having a little bit of Linux magic right in your Windows computer!
Why Should I Care?
Well, WSL is like having a Swiss Army knife on your computer. It can make certain tasks easier and faster, and it can even let you use tools that were previously only available on Linux.
Is It Hard to Use?
Not at all! If you’ve ever used the Command Prompt on your Windows computer, then you’re already halfway there. And even if you haven’t, there are plenty of easy-to-follow guides out there to help you get started.
Do I Need to Be a Computer Expert to Use It?
Absolutely not! While WSL is a powerful tool that many developers love to use, it’s also quite user-friendly. With a bit of curiosity and a dash of patience, anyone can start exploring the world of WSL.
As a DotNet developer, you might be wondering why there’s so much buzz around the Windows Subsystem for Linux (WSL). Let’s dive into the reasons why WSL could be a game-changer for you.
- Seamless Integration: WSL provides a full-fledged Linux environment right within your Windows system. This means you can run Linux commands and applications without needing a separate machine or dual-boot setup.
- Development Environment Consistency: With WSL, you can maintain consistency between your development and production environments, especially if your applications are deployed on Linux servers. This can significantly reduce the “it works on my machine” syndrome.
- Access to Linux-Only Tools: Some tools and utilities are only available or work better on Linux. WSL brings these tools to your Windows desktop, expanding your toolkit without additional overhead.
- Improved Performance: WSL 2, the latest version, runs a real Linux kernel inside a lightweight virtual machine (VM), which leads to faster file system performance and complete system call compatibility.
- Docker Support: WSL 2 provides full Docker support without requiring additional layers for translation between Windows and Linux, resulting in a more efficient and seamless Docker experience.
In conclusion, WSL is not just a fancy tool; it’s a powerful ally that can enhance your productivity and capabilities as a DotNet developer.