MightyOrm / Mighty

A new, small, dynamic micro-ORM. Highly compatible with Massive, but with many essential new features.
BSD 3-Clause "New" or "Revised" License
101 stars 20 forks source link

Complex C# types processing #42

Closed Konohomaru closed 2 years ago

Konohomaru commented 2 years ago

Can MightyORM process complex types?

I have following types in my C# project:

public class Profile { public Guid Id {get;set;} }
public class Account { public Guid Id {get;set;} }
public class ProfileAccount { public Guid Id {get;set;} public Profile Member {get;set;} public Account Account {get;set;} }

Also in database I have following tables:

profiles (row_id uuid)
accounts (row_id uuid)
profile_accounts (row_id uuid, member_row_id uuid, account_row_id uuid)

I removed mapping attributes to save place and also removed other properties and columns to simplify example.

I tried to create profile_account, but Mighty throws an exception. Can it create rows like this?

I tried this code to create needed row:

public new async Task<ProfileAccount> Create(ProfileAccount entity)
{
    await _accounts.Create(entity.Account);
    return await base.Create(entity);
}

Profile already exists in database.

mikebeaton commented 2 years ago

Hi @Konohomaru -

No, in micro-ORM style Mighty does not automatically handle table relationships.

You can ofc write a SELECT statement which does the joins you need, and then use Mighty to read from that and hydrate (hmm, I rather dislike that word, in this context!) either a typed object or a dynamic object with flat fields matching your SELECT.

In the main work project in which I use Mighty, I use db access objects which are flat (ids only) and then have some helper code to fetch and 'hydrate' (again!) the required nested objects to be exposed on the API. These are often objects from reasonably short fixed length lists, which can be pre-loaded once.

But again, as a micro-ORM, I am afraid Mighty does not handle this for you. FYI Dapper I believe has code to support one level of joins, which if it happens to match exactly what you need might be helpful.

Mike