102 lines
1.8 KiB
Markdown
102 lines
1.8 KiB
Markdown
# Mapping and Entities
|
|
|
|
DynamORM supports attribute-driven mapping and entity lifecycle helpers.
|
|
|
|
## Mapping Attributes
|
|
|
|
## `TableAttribute`
|
|
|
|
```csharp
|
|
[Table(Name = "users", Owner = "dbo", Override = true)]
|
|
public class User
|
|
{
|
|
// ...
|
|
}
|
|
```
|
|
|
|
- `Name`: table name override.
|
|
- `Owner`: schema/owner segment.
|
|
- `Override`: prefer attribute schema info over provider schema.
|
|
|
|
## `ColumnAttribute`
|
|
|
|
```csharp
|
|
public class User
|
|
{
|
|
[Column("id", isKey: true)]
|
|
public int Id { get; set; }
|
|
|
|
[Column("email")]
|
|
public string Email { get; set; }
|
|
}
|
|
```
|
|
|
|
Important flags:
|
|
|
|
- `IsKey`
|
|
- `AllowNull`
|
|
- `IsNoInsert`
|
|
- `IsNoUpdate`
|
|
- `Type`, `Size`, `Precision`, `Scale`
|
|
|
|
## Ignore Fields
|
|
|
|
Use `IgnoreAttribute` to skip properties in mapper-driven workflows.
|
|
|
|
## Validation
|
|
|
|
`RequiredAttribute` can define value rules.
|
|
|
|
```csharp
|
|
public class Profile
|
|
{
|
|
[Required(1f, 10f)]
|
|
public int Rank { get; set; }
|
|
|
|
[Required(2, true)]
|
|
[Required(7, 18, ElementRequirement = true)]
|
|
public decimal[] Scores { get; set; }
|
|
}
|
|
|
|
var issues = DynamicMapperCache.GetMapper<Profile>().ValidateObject(profile);
|
|
```
|
|
|
|
Validation scenarios are verified in `DynamORM.Tests/Helpers/Validation/ObjectValidationTest.cs`.
|
|
|
|
## `DynamicEntityBase`
|
|
|
|
`DynamicEntityBase` tracks state and changed fields.
|
|
|
|
```csharp
|
|
public class UserEntity : DynamicEntityBase
|
|
{
|
|
[Column("id", true)]
|
|
public int Id { get; set; }
|
|
|
|
[Column("first")]
|
|
public string First { get; set; }
|
|
}
|
|
```
|
|
|
|
Available behaviors:
|
|
|
|
- `Validate()`
|
|
- `Insert(db)`
|
|
- `Update(db)`
|
|
- `Delete(db)`
|
|
- `Refresh(db)`
|
|
- `Save(db)` driven by `DynamicEntityState`
|
|
|
|
## Repository Base
|
|
|
|
`DynamicRepositoryBase<T>` provides common operations:
|
|
|
|
- `GetAll()`
|
|
- `GetByQuery(...)`
|
|
- `Insert(...)`
|
|
- `Update(...)`
|
|
- `Delete(...)`
|
|
- `Save(...)`
|
|
|
|
It ensures query/table compatibility for `T` unless explicitly bypassed.
|