add support for Dynamic Entity Registration in DbContext by Provides IXEntityRegisterar interface and XBaseEntityRegisterar as an abstract Implementation ...
all other requirements handled inside Module.
This commit is contained in:
@@ -94,8 +94,18 @@ public class XSaherElmDbContext : XDbContext {
|
||||
|
||||
public XSaherElmDbContext(
|
||||
DbContextOptions options,
|
||||
XDataServiceConfiguration config
|
||||
) : base(options, config) { }
|
||||
XDataServiceConfiguration config,
|
||||
XEntityRegisterarHandlerService dynamicEntityHandlers
|
||||
) : base(
|
||||
// Database nae in Data Service Configurations ...
|
||||
"xApiDb",
|
||||
options,
|
||||
config,
|
||||
dynamicEntityHandlers,
|
||||
// a List allowed Dynamic Entity Registerars in this Context ...
|
||||
new string[] { nameof(XStringEntityRegisterar) }
|
||||
)
|
||||
{ }
|
||||
|
||||
//
|
||||
public override void OnXModelCreating(ModelBuilder modelBuilder)
|
||||
@@ -107,6 +117,70 @@ public class XSaherElmDbContext : XDbContext {
|
||||
}
|
||||
```
|
||||
|
||||
#### Dynamic Entities
|
||||
|
||||
**xDataService** provides a solution for Dynamic Entity Registration in it's Contexts out of box.
|
||||
this is useful when some modules needs to presist data.
|
||||
|
||||
**IXEntityRegisterar**: an interface for Describe how to Provides Dynamic Entities.
|
||||
**XBaseEntityRegisterar**: ab abstract of Implementing Dynamic Entity Providers.
|
||||
|
||||
```C#
|
||||
public interface IXEntityRegisterar
|
||||
{}
|
||||
|
||||
public abstract class XBaseEntityRegisterar : IXEntityRegisterar
|
||||
{}
|
||||
```
|
||||
|
||||
for exampe we have **xStringService** which it is a module for Provides String resources in an Application. this module required to has Data Persist using Specified Entities in Implemented Application Contexts.
|
||||
|
||||
```C#
|
||||
public class XString : XBaseIntIDEntity
|
||||
{
|
||||
[Required]
|
||||
[StringLength (10)]
|
||||
public string Language { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength (255)]
|
||||
public string ResourceTitle { get; set; }
|
||||
|
||||
[Required]
|
||||
public string TranslatedValue { get; set; }
|
||||
}
|
||||
|
||||
public class XStringEntityConfiguration : XBaseEntityTypeConfiguration<XString, int>
|
||||
{
|
||||
public override void Configure(EntityTypeBuilder<XString> builder)
|
||||
{
|
||||
builder.ToTable("Strings");
|
||||
}
|
||||
}
|
||||
|
||||
public class XStringEntityRegisterar : XBaseEntityRegisterar, IXEntityRegisterar
|
||||
{
|
||||
public XStringEntityRegisterar()
|
||||
: base(nameof(XStringEntityRegisterar))
|
||||
{
|
||||
//
|
||||
// Add XString Entity ...
|
||||
AddEntity<XString, int>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configure Entities ...
|
||||
/// </summary>
|
||||
/// <param name="modelBuilder"></param>
|
||||
public override void ConfigureEntities(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.ConfigureEntities(modelBuilder);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
by this mechanism, provided Entity Dynamically Registered on Application DbContext automatically.
|
||||
|
||||
#### Repository Pattern
|
||||
|
||||
by this feature you can manipulate specified Entity using Repository Pttern implementation.
|
||||
|
||||
Reference in New Issue
Block a user