⚠️ Preview Release: RoomSharp is currently in Preview. Full source code will be published after the Preview stage. Learn more →

Introduction to RoomSharp

A high-performance Room-style ORM for modern .NET (net8/net9/net10) powered by Source Generators.

What is RoomSharp?

RoomSharp is a Room-inspired data layer for modern .NET that combines a Roslyn source generator, attribute-driven models, and lightweight database providers to produce zero-reflection DAOs, migrations, and schema builders. It supports SQLite, SQL Server, PostgreSQL, and MySQL while still letting you drop down to raw ADO.NET when needed.

🎯 Key Philosophy: Compile-time code generation instead of runtime reflection. RoomSharp analyzes your entity classes and DAO interfaces at build time to generate optimized, type-safe data access code.

Why RoomSharp?

Source-Generated DAOs

RoomSharp uses Roslyn source generators to emit fully compiled data access implementations at build time. This means:

  • Zero runtime reflection overhead
  • Type-safe query methods
  • IDE auto-completion and IntelliSense support
  • Compile-time error detection

Multi-Provider Runtime

Common abstractions over multiple database engines with tuned dialect helpers:

  • SQLite - Lightweight, embedded database
  • SQL Server - Enterprise-grade relational database
  • PostgreSQL - Advanced open-source database
  • MySQL/MariaDB - Popular open-source databases

High Performance

  • IL-Based Mapping: Ultra-fast data reader to object mapping using IL generation
  • Zero Allocations: Hot loops designed for minimal memory pressure
  • Batch Insert Engine: Optimized bulk operations with prepared statement reuse
  • 2-5x Faster: Benchmark results show significant performance improvements over alternatives

Rich Annotations

Express your database schema with intuitive attributes:

  • [Entity] - Define table mappings
  • [PrimaryKey] - Specify primary keys with auto-generation
  • [Index] - Create database indexes
  • [ForeignKey] - Define relationships
  • [Embedded] & [Relation] - Handle complex object graphs
  • [TypeConverter] - Custom type conversions

Core Architecture

RoomSharp consists of three main layers:

1. Attribute Layer

You define entities and DAOs using attributes like [Entity], [Dao], [Query], etc. These attributes drive the source generator.

2. Source Generator

At compile-time, the Roslyn-based generator analyzes your code and emits:

  • DAO implementations (*DaoImpl)
  • Database implementations (*DatabaseImpl)
  • IL-based object mappers
  • Schema builders and migration helpers

3. Runtime Layer

The runtime provides:

  • Database provider abstractions
  • Connection management and pooling
  • Transaction coordination
  • Migration execution
  • Type converter registry

Quick Example

Here's a minimal example to get you started:

C#
// 1. Define your entity
[Entity(TableName = "users")]
public class User
{
    [PrimaryKey(AutoGenerate = true)]
    public long Id { get; set; }
    
    [Unique]
    public required string Email { get; set; }
    public string? Name { get; set; }
}

// 2. Define your DAO
[Dao]
public interface IUserDao
{
    [Insert]
    long Insert(User user);
    
    [Query("SELECT * FROM users WHERE Email = :email")]
    Task<User?> FindByEmailAsync(string email);
}

// 3. Define your database
[Database(Version = 1, Entities = [typeof(User)])]
public abstract class AppDatabase(IDatabaseProvider provider) 
    : RoomDatabase(provider)
{
    public abstract IUserDao UserDao { get; }
}

// 4. Use it
var db = RoomDatabase.Builder<AppDatabaseImpl>()
    .UseSqlite("app.db")
    .Build();

var userId = db.UserDao.Insert(new User 
{ 
    Email = "john@example.com", 
    Name = "John" 
});

var user = await db.UserDao.FindByEmailAsync("john@example.com");
✨ What just happened? The source generator created AppDatabaseImpl and IUserDaoImpl with fully optimized, type-safe implementations. No reflection, no runtime overhead.

Next Steps

Now that you understand what RoomSharp is, let's get it installed and start building: