Class DomainContext
A domain context that can be used to retrieve aggregates and save changes with entity framework.
Inherited Members
Namespace: IRM.EntityFrameworkCore
Assembly: IRM.EntityFrameworkCore.dll
Syntax
public class DomainContext : DbContext, IInfrastructure<IServiceProvider>, IDbContextDependencies, IDbSetCache, IDbQueryCache, IDbContextPoolable, IDomainContext, IDbContext, IDisposable
Constructors
DomainContext(ICallContext)
Creates a new instance of DomainContext.
Declaration
protected DomainContext(ICallContext callContext)
Parameters
ICallContext
callContext
The service used to access the current call context with information about tenant and user. |
DomainContext(ICallContext, DbContextOptions)
Creates a new instance of DomainContext using the specified options.
Declaration
public DomainContext(ICallContext callContext, DbContextOptions options)
Parameters
ICallContext
callContext
The service used to access the current call context with information about tenant and user. |
Microsoft.EntityFrameworkCore.DbContextOptions
options
The options for this context. |
Properties
CallContext
Gets the ICallContext used to access information about tenant and user.
Declaration
protected ICallContext CallContext { get; }
Property Value
ICallContext
|
EventSource
Gets or sets an IEventSourceRepository used to store events.
Declaration
public virtual IEventSourceRepository EventSource { get; set; }
Property Value
IEventSourceRepository
|
Remarks
This must be set if events should be stored through the DomainContext.
When set in a cloud environment the connection string for the IEventSourceRepository is required to be exactly the same as for this DomainContext.
IsolationLevel
Gets or sets the IsolationLevel used for the transaction, when saving both data and events.
Declaration
public IsolationLevel IsolationLevel { get; set; }
Property Value
System.Transactions.IsolationLevel
|
MetadataProviders
The IMetadataProvider used when saving events for an aggregate in the EventSource.
Declaration
public virtual List<IMetadataProvider> MetadataProviders { get; }
Property Value
List<IMetadataProvider>
|
Remarks
By default a single AggregateMetadataProvider is configured.
TransactionTime
Gets the current transaction time. Default to System.DateTime.Now, but override it to set time in UTC.
Declaration
protected virtual DateTime TransactionTime { get; }
Property Value
System.DateTime
|
Methods
Add<T>(T, Boolean)
Adds an aggregate to the context so it can be saved in the database by calling SaveChanges(Boolean).
Declaration
public void Add<T>(T aggregateRoot, bool autoSave = true)
where T : class, IAggregateRoot
Parameters
T
aggregateRoot
The aggregate that should be added to the database. |
Boolean
autoSave
true if SaveChanges should be called automatically by Add; otherwise false. |
Type Parameters
T
The type of aggregate. |
Implements
Delete<T>(T, Boolean)
Deletes (removes) an aggregate in the context so it can be deleted in the database by calling SaveChanges(Boolean).
Declaration
public void Delete<T>(T aggregateRoot, bool autoSave = true)
where T : class, IAggregateRoot
Parameters
T
aggregateRoot
The aggregate that should be deleted in the database. |
Boolean
autoSave
true if SaveChanges should be called automatically by Delete; otherwise false. |
Type Parameters
T
The type of aggregate. |
Implements
GetById<T>(Guid)
Gets an aggregate by unique identity from the database.
Declaration
public T GetById<T>(Guid id)
where T : AggregateRoot
Parameters
System.Guid
id
The unique identifier of the aggregate. |
Returns
T
Returns an aggregate. |
Type Parameters
T
The type of aggregate. |
Implements
ProcessEntityBeforeSave(EntityEntry<IEntity>, DateTime)
Extensibility point for additional processesing of an entity before the changes is persisted in the database.
Declaration
protected virtual void ProcessEntityBeforeSave(EntityEntry<IEntity> entityEntry, DateTime timeOfTransaction)
Parameters
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<IEntity>
entityEntry
The Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> with change tracking information to process. |
System.DateTime
timeOfTransaction
The System.DateTime used for all entities in the current transaction. |
Remarks
This method is called after the infrastrucutre code has ensured correct values of SenastÄndrad, SenastÄndradAv, Version and OwnerId. The method is called before events is saved.
ProcessEntityBeforeSaveAsync(EntityEntry<IEntity>, DateTime)
Extensibility point for additional processesing of an entity before the changes is persisted in the database.
Declaration
protected virtual Task ProcessEntityBeforeSaveAsync(EntityEntry<IEntity> entityEntry, DateTime timeOfTransaction)
Parameters
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<IEntity>
entityEntry
The Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> with change tracking information to process. |
System.DateTime
timeOfTransaction
The System.DateTime used for all entities in the current transaction. |
Returns
System.Threading.Tasks.Task
|
Remarks
This method is called after the infrastrucutre code has ensured correct values of SenastÄndrad, SenastÄndradAv, Version and OwnerId. The method is called before events is saved.
SaveChanges(Boolean)
Declaration
public override int SaveChanges(bool acceptAllChangesOnSuccess)
Parameters
Boolean
acceptAllChangesOnSuccess
|
Returns
System.Int32
|
Overrides
SaveChangesAsync(Boolean, CancellationToken)
Declaration
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Boolean
acceptAllChangesOnSuccess
|
System.Threading.CancellationToken
cancellationToken
|
Returns
System.Threading.Tasks.Task<System.Int32>
|
Overrides
SaveChangesCore(Boolean)
Saves all changes made in this context to the database.
Declaration
public virtual int SaveChangesCore(bool acceptAllChangesOnSuccess)
Parameters
Boolean
acceptAllChangesOnSuccess
Indicates whether AcceptAllChanges() is called after the changes have been sent successfully to the database. |
Returns
System.Int32
|
Remarks
SaveChanges(Boolean) uses the configured Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy to call this method. If you need to control the Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy yourself you should call this method instead of SaveChanges(Boolean).
SaveChangesCoreAsync(Boolean, CancellationToken)
Asynchronously saves all changes made in this context to the database.
Declaration
public virtual async Task<int> SaveChangesCoreAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Boolean
acceptAllChangesOnSuccess
Indicates whether AcceptAllChanges() is called after the changes have been sent successfully to the database. |
System.Threading.CancellationToken
cancellationToken
A System.Threading.CancellationToken to observe while waiting for the task to complete. |
Returns
System.Threading.Tasks.Task<System.Int32>
|
Remarks
SaveChanges(Boolean) uses the configured Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy to call this method. If you need to control the Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy yourself you should call this method instead of SaveChanges(Boolean).
SetSomÄndrad(IEntity, DateTime)
Declaration
protected virtual void SetSomÄndrad(IEntity entity, DateTime transaktionsTillfälle)
Parameters
IEntity
entity
The IEntity. |
System.DateTime
transaktionsTillfälle
The System.DateTime representing this transaction. |
Update<T>(T, Boolean)
Updates an aggregate in the context so it can be saved in the database by calling SaveChanges(Boolean)
Declaration
public void Update<T>(T aggregateRoot, bool autoSave = true)
where T : class, IAggregateRoot
Parameters
T
aggregateRoot
The aggregate that should be changed in the database. |
Boolean
autoSave
true if SaveChanges should be called automatically by Update; otherwise false. |
Type Parameters
T
The type of aggregate. |