Skip to content

Commit

Permalink
Document StandaloneEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
ascott18 committed Oct 15, 2020
1 parent 1a52bc3 commit ea8f2ca
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 13 deletions.
15 changes: 9 additions & 6 deletions docs/pages/modeling/model-components/behaviors.rst
Expand Up @@ -82,7 +82,7 @@ All behaviors are instantiated using dependency injection and your application's
Standard Behaviors
------------------

The standard behaviors, :csharp:`IntelliTect.Coalesce.StandardBehaviors<T, TContext>`, contains a significant number of properties and methods that can be utilized and/or overridden at your leisure.
The standard behaviors, :csharp:`IntelliTect.Coalesce.StandardBehaviors<T>` and its EntityFramework-supporting sibling :csharp:`IntelliTect.Coalesce.StandardBehaviors<T, TContext>`, contain a significant number of properties and methods that can be utilized and/or overridden at your leisure.

Properties
''''''''''
Expand Down Expand Up @@ -118,15 +118,18 @@ These methods often call one another, so overriding one method may cause some ot
.. code-block:: none
SaveAsync
DetermineSaveKind
DetermineSaveKindAsync
GetDbSet
ValidateDto
MapIncomingDto
BeforeSave
BeforeSaveAsync
BeforeSave
ExecuteSaveAsync
AfterSave
DeleteAsync
BeforeDelete
BeforeDeleteAsync
BeforeDelete
ExecuteDeleteAsync
GetDbSet
AfterDelete
Expand All @@ -140,7 +143,7 @@ All of the methods outlined above can be overridden. A description of each of th
:csharp:`SaveAsync`
Save the given item. This is the main entry point for saving, and takes a DTO as a parameter. This method is responsible for performing mapping to your EF models and ultimately saving to your database. If it is required that you access properties from the incoming DTO in this method, a set of extension methods :csharp:`GetValue` and :csharp:`GetObject` are available on the DTO for accessing properties that are mapped 1:1 with your EF models.

:csharp:`DetermineSaveKind`
:csharp:`DetermineSaveKindAsync`
Given the incoming DTO on which Save has been called, examine its properties to determine if the operation is meant to be a create or an update operation. Return this distinction along with the key that was used to make the distinction.

This method is called outside of the standard data source by the base API controller to perform role-based security on saves at the controller level.
Expand All @@ -154,7 +157,7 @@ All of the methods outlined above can be overridden. A description of each of th
:csharp:`MapIncomingDto`
Map the properties of the incoming DTO to the model that will be saved to the database. By default, this will call the :csharp:`MapTo` method on the DTO, but if more precise control is needed, the :csharp:`IClassDto<T>` extension methods or a cast to a known type can be used to get specific values. If all else fails, the DTO can be reflected upon.

:csharp:`BeforeSave`
:csharp:`BeforeSaveAsync`/:csharp:`BeforeSave`
Provides an easy way for derived classes to intercept a save attempt and either reject it by returning an unsuccessful result, or approve it by returning success. The incoming item can also be modified at will in this method to override changes that the client made as desired.

:csharp:`AfterSave`
Expand Down
15 changes: 9 additions & 6 deletions docs/pages/modeling/model-components/data-sources.rst
Expand Up @@ -167,7 +167,7 @@ You can setup :ref:`TypeScriptListViewModels` to automatically reload from the s
Standard Data Source
....................

The standard data source, :csharp:`IntelliTect.Coalesce.StandardDataSource<T, TContext>`, contains a significant number of properties and methods that can be utilized and/or overridden at your leisure.
The standard data sources, :csharp:`IntelliTect.Coalesce.StandardDataSource<T>` and its EntityFramework-supporting sibling :csharp:`IntelliTect.Coalesce.StandardDataSource<T, TContext>`, contain a significant number of properties and methods that can be utilized and/or overridden at your leisure.



Expand Down Expand Up @@ -215,13 +215,15 @@ These methods often call one another, so overriding one method may cause some ot
GetMappedItemAsync
GetItemAsync
GetQuery
GetQueryAsync
GetQuery
GetIncludeTree
TransformResults
GetMappedListAsync
GetListAsync
GetQuery
GetQueryAsync
GetQuery
ApplyListFiltering
ApplyListPropertyFilters
ApplyListPropertyFilter
Expand All @@ -236,7 +238,8 @@ These methods often call one another, so overriding one method may cause some ot
TransformResults
GetCountAsync
GetQuery
GetQueryAsync
GetQuery
ApplyListFiltering
ApplyListPropertyFilters
ApplyListPropertyFilter
Expand All @@ -248,8 +251,8 @@ Method Details

All of the methods outlined above can be overridden. A description of each of the non-interface inner methods is as follows:

:csharp:`GetQuery`
The method is the one that you will most commonly be override in order to implement custom query logic. From this method, you could:
:csharp:`GetQuery`/:csharp:`GetQueryAsync`
The method is the one that you will most commonly be override in order to implement custom query logic. The default implementation of GetQueryAsync simply calls GetQuery - be aware of this in cases of complex overrides/inheritance. From this method, you could:

- Specify additional query filtering such as row-level security or soft-delete logic. Or, restrict the data source entirely for users or whole roles by returning an empty query.
- Include additional data using EF's :csharp:`.Include()` and :csharp:`.ThenInclude()`.
Expand Down
58 changes: 58 additions & 0 deletions docs/pages/modeling/model-types/entities.rst
Expand Up @@ -62,3 +62,61 @@ Behaviors

Behaviors in Coalesce are to mutating data as data sources are to reading data. Defining a behaviors class for a model allows complete control over the way that Coalesce will create, update, and delete your application's data in response to requests made through its generated API. Read :ref:`Behaviors` to learn more.


Standalone Entities
-------------------

In Coalesce, Standalone Entities are entity types that are not based on Entity Framework. For these types, you must define at least one custom :ref:`Data Source <DataSources>`, and optionally a :ref:`Behaviors` class as well. These entities are discovered by Coalesce by annotating them with :csharp:`[Coalesce, StandaloneEntity]`.

To define data sources and behaviors for Standalone Entities, it is recommended you inherit from :cs:`StandardDataSource<T>` and :cs:`StandardBehaviors<T>`, respectively. For example:

.. code-block:: c#
[Coalesce, StandaloneEntity]
public class StandaloneExample
{
public int Id { get; set; }
[Search(SearchMethod = SearchAttribute.SearchMethods.Contains), ListText]
public string Name { get; set; } = "";
[DefaultOrderBy]
public DateTimeOffset Date { get; set; }
private static int nextId = 0;
private static ConcurrentDictionary<int, StandaloneExample> backingStore = new ConcurrentDictionary<int, StandaloneExample>();
public class DefaultSource : StandardDataSource<StandaloneExample>
{
public DefaultSource(CrudContext context) : base(context) { }
public override Task<IQueryable<StandaloneExample>> GetQueryAsync(IDataSourceParameters parameters)
=> Task.FromResult(backingStore.Values.AsQueryable());
}
public class Behaviors : StandardBehaviors<StandaloneExample>
{
public Behaviors(CrudContext context) : base(context) { }
public override Task ExecuteDeleteAsync(StandaloneExample item)
{
backingStore.TryRemove(item.Id, out _);
return Task.CompletedTask;
}
public override Task ExecuteSaveAsync(SaveKind kind, StandaloneExample? oldItem, StandaloneExample item)
{
if (kind == SaveKind.Create)
{
item.Id = Interlocked.Increment(ref nextId);
backingStore.TryAdd(item.Id, item);
}
else
{
backingStore.TryRemove(item.Id, out _);
backingStore.TryAdd(item.Id, item);
}
return Task.CompletedTask;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Coalesce.Web.Vue/src/metadata.g.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ea8f2ca

Please sign in to comment.