|
1 | | -# PosInformatique.Foundations.EmailAddresses.EntityFramework |
| 1 | +# PosInformatique.Foundations.MediaTypes.EntityFramework |
2 | 2 |
|
3 | | -[](https://www.nuget.org/packages/PosInformatique.Foundations.EmailAddresses.EntityFramework/) |
4 | | -[](https://www.nuget.org/packages/PosInformatique.Foundations.EmailAddresses.EntityFramework/) |
| 3 | +[](https://www.nuget.org/packages/PosInformatique.Foundations.MediaTypes.EntityFramework/) |
| 4 | +[](https://www.nuget.org/packages/PosInformatique.Foundations.MediaTypes.EntityFramework/) |
5 | 5 |
|
6 | 6 | ## Introduction |
7 | | -Provides **Entity Framework Core** integration for the `EmailAddress` value object from |
8 | | -[PosInformatique.Foundations.EmailAddresses](https://www.nuget.org/packages/PosInformatique.Foundations.EmailAddresses/). |
9 | | -This package enables seamless mapping of RFC 5322 compliant email addresses as strongly-typed properties in Entity Framework Core entities. |
10 | 7 |
|
11 | | -It ensures proper SQL type mapping, validation, and conversion to `VARCHAR` when persisted to the database. |
| 8 | +Provides **Entity Framework Core** integration for the `MimeType` value object from |
| 9 | +[PosInformatique.Foundations.MediaTypes](https://www.nuget.org/packages/PosInformatique.Foundations.MediaTypes/). |
| 10 | +This package enables seamless mapping of MIME types as strongly-typed properties in Entity Framework Core entities. |
| 11 | + |
| 12 | +It ensures proper SQL type mapping, validation, and conversion to `VARCHAR(128)` when persisted to the database. |
12 | 13 |
|
13 | 14 | ## Install |
| 15 | + |
14 | 16 | You can install the package from NuGet: |
15 | 17 |
|
16 | 18 | ```powershell |
17 | | -dotnet add package PosInformatique.Foundations.EmailAddresses.EntityFramework |
| 19 | +dotnet add package PosInformatique.Foundations.MediaTypes.EntityFramework |
18 | 20 | ``` |
19 | 21 |
|
20 | | -This package depends on the base package [PosInformatique.Foundations.EmailAddresses](https://www.nuget.org/packages/PosInformatique.Foundations.EmailAddresses/). |
| 22 | +This package depends on the base package [PosInformatique.Foundations.MediaTypes](https://www.nuget.org/packages/PosInformatique.Foundations.MediaTypes/). |
21 | 23 |
|
22 | 24 | ## Features |
23 | | -- Provides an extension method `IsEmailAddress()` to configure EF Core properties for `EmailAddress`. |
24 | | -- Maps to `VARCHAR(320)` database columns using the SQL type `EmailAddress` (you must define the SQL type `EmailAddress` mapped to `VARCHAR(320)` in your database). |
25 | | -- Ensures validation, normalization, and safe conversion to/from database fields. |
26 | | -- Built on top of the core `EmailAddress` value object. |
| 25 | + |
| 26 | +- Provides an extension method `IsMimeType()` to configure EF Core properties for `MimeType`. |
| 27 | +- Maps to `VARCHAR(128)` database columns using the SQL type `MimeType` (you must define the SQL type `MimeType` mapped to `VARCHAR(128)` in your database). |
| 28 | +- Ensures validation and safe conversion to/from database fields. |
| 29 | +- Built on top of the core `MimeType` value object. |
27 | 30 |
|
28 | 31 | ## Use cases |
29 | | -- **Entity mapping**: enforce strong typing for email addresses at the persistence layer. |
30 | | -- **Consistency**: ensure the same validation rules are applied in your entities and database. |
31 | | -- **Safety**: prevent invalid strings being stored in your database |
| 32 | + |
| 33 | +- **Entity mapping**: enforce strong typing for MIME types at the persistence layer. |
| 34 | +- **Consistency**: ensure the same rules are applied in your entities and database. |
| 35 | +- **Safety**: prevent invalid or malformed MIME type strings being stored in your database. |
32 | 36 |
|
33 | 37 | ## Examples |
34 | 38 |
|
35 | | -> ⚠️ To use `IsEmailAddress()`, you must first define the SQL type `EmailAddress` mapped to `VARCHAR(320)` in your database. |
36 | | -For SQL Server, you can create it with: |
| 39 | +> ⚠️ To use `IsMimeType()`, you must first define the SQL type `MimeType` mapped to `VARCHAR(128)` in your database. |
| 40 | +> For SQL Server, you can create it with: |
37 | 41 |
|
38 | 42 | ```sql |
39 | | -CREATE TYPE EmailAddress FROM VARCHAR(320) NOT NULL; |
| 43 | +CREATE TYPE MimeType FROM VARCHAR(128) NOT NULL; |
40 | 44 | ``` |
41 | 45 |
|
42 | 46 | ### Example: Configure an entity |
| 47 | + |
43 | 48 | ```csharp |
44 | 49 | using Microsoft.EntityFrameworkCore; |
45 | | -using PosInformatique.Foundations; |
| 50 | +using PosInformatique.Foundations.MediaTypes; |
46 | 51 |
|
47 | | -public class User |
| 52 | +public class Document |
48 | 53 | { |
49 | 54 | public int Id { get; set; } |
50 | | - public EmailAddress Email { get; set; } |
| 55 | + |
| 56 | + public MimeType ContentType { get; set; } |
51 | 57 | } |
52 | 58 |
|
53 | 59 | public class ApplicationDbContext : DbContext |
54 | 60 | { |
55 | | - public DbSet<User> Users => Set<User>(); |
| 61 | + public DbSet<Document> Documents => Set<Document>(); |
56 | 62 |
|
57 | 63 | protected override void OnModelCreating(ModelBuilder modelBuilder) |
58 | 64 | { |
59 | | - modelBuilder.Entity<User>() |
60 | | - .Property(u => u.Email) |
61 | | - .IsEmailAddress(); |
| 65 | + modelBuilder.Entity<Document>() |
| 66 | + .Property(d => d.ContentType) |
| 67 | + .IsMimeType(); |
62 | 68 | } |
63 | 69 | } |
64 | 70 | ``` |
65 | 71 |
|
66 | | -This will configure the `Email` property of the `User` entity with: |
67 | | -- `VARCHAR(320)` (Non-unicode) column length |
68 | | -- SQL column type `EmailAddress` |
| 72 | +This will configure the `ContentType` property of the `Document` entity with: |
| 73 | + |
| 74 | +- `VARCHAR(128)` (non-unicode) column length |
| 75 | +- SQL column type `MimeType` |
| 76 | +- Proper conversion between `MimeType` and `string` |
69 | 77 |
|
70 | 78 | ## Links |
71 | | -- [NuGet package: EmailAddresses.EntityFramework](https://www.nuget.org/packages/PosInformatique.Foundations.EmailAddresses.EntityFramework/) |
72 | | -- [NuGet package: EmailAddresses (core library)](https://www.nuget.org/packages/PosInformatique.Foundations.EmailAddresses/) |
73 | | -- [Source code](https://github.com/PosInformatique/PosInformatique.Foundations) |
| 79 | + |
| 80 | +- [NuGet package: MediaTypes.EntityFramework](https://www.nuget.org/packages/PosInformatique.Foundations.MediaTypes.EntityFramework/) |
| 81 | +- [NuGet package: MediaTypes (core library)](https://www.nuget.org/packages/PosInformatique.Foundations.MediaTypes/) |
| 82 | +- [Source code](https://github.com/PosInformatique/PosInformatique.Foundations) |
0 commit comments