Skip to content
This repository was archived by the owner on Jan 3, 2021. It is now read-only.

Add multiple ef dbcontexts and new filters #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/pages/extensibility/EntityRepositories.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,67 @@ public class AuthorizedArticleRepository
}
`}
/>

<ContentHeader>Multiple DbContexts</ContentHeader>
<Example
md={`
If you need to use multiple EF \`DbContext\`, first add each \`DbContext\` to the \`ContextGraphBuilder\`.

Then, create an implementation of \`IDbContextResolver\` for each context.

Register each of the new \`IDbContextResolver\` implementations in the \`Startup\`.

You can then create a general repository for each context and inject it per resource type.
This example shows a single \`DbContextARepository\` for all entities that are members of DbContextA.

Then inject the repository for the correct entity, in this case Foo is a member of DbContextA.
`}
code={`
// Startup.cs
services.AddJsonApi(options => {
options.BuildContextGraph((builder) =>
{
// Add both contexts using the builder
builder.AddDbContext<DbContextA>();
builder.AddDbContext<DbContextB>();
});
}, mvcBuilder);


public class DbContextAResolver : IDbContextResolver
{
private readonly DbContextA _context;

public DbContextAResolver(DbContextA context)
{
_context = context;
}

public DbContext GetContext() => _context;
}


// Startup.cs
services.AddScoped<DbContextAResolver>();
services.AddScoped<DbContextBResolver>();


public class DbContextARepository<TEntity>
: DefaultEntityRepository<TEntity> where TEntity : class, IIdentifiable<T>
{
public DbContextARepository(
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext,
DbContextAResolver contextResolver)
: base(loggerFactory, jsonApiContext, contextResolver)
{ }
}


// Startup.cs
services.AddScoped<IEntityRepository<Foo>, DbContextARepository<Foo>>();
`}
/>
</SplitPage>
);
};
2 changes: 2 additions & 0 deletions src/pages/usage/Filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ For operations other than equality, the query can be prefixed with an operation
`}
code={`
?filter[attribute]=eq:value
?filter[attribute]=ne:value
?filter[attribute]=lt:value
?filter[attribute]=gt:value
?filter[attribute]=le:value
?filter[attribute]=ge:value
?filter[attribute]=like:value
?filter[attribute]=in:value
`}
/>

Expand Down