-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor OfX to make it better!
- Loading branch information
Showing
38 changed files
with
460 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=datas/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=laters/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=laters/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=responser/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,76 @@ | ||
# OfX-Nats | ||
# OfX.EntityFrameworkCore | ||
|
||
OfX-Nats is an extension package for OfX that leverages Nats for efficient data transportation. This package provides a high-performance, strongly-typed communication layer for OfX’s Attribute-based Data Mapping, enabling streamlined data retrieval across distributed systems. | ||
OfX.EntityFrameworkCore is an extension package for OfX that integrates with Entity Framework Core to simplify data fetching by leveraging attribute-based data mapping. This extension streamlines data retrieval using EF Core, reducing boilerplate code and improving maintainability. | ||
|
||
[Demo Project!](https://github.com/quyvu01/TestOfX-Demo) | ||
|
||
--- | ||
|
||
## Introduction | ||
|
||
Nats-based Transport: Implements Nats to handle data communication between services, providing a fast, secure, and scalable solution. | ||
OfX.EntityFrameworkCore extends the core OfX library by providing seamless integration with Entity Framework Core. This enables developers to automatically map and retrieve data directly from a database, leveraging the power of EF Core along with attribute-based data mapping. | ||
|
||
For example, suppose you have a `UserId` property in your model, and you want to fetch the corresponding `UserName` and `Email` fields from the database. By using OfX.EntityFrameworkCore, you can annotate your model with attributes, and the library will handle data fetching for you. | ||
|
||
--- | ||
|
||
## Installation | ||
|
||
To install the OfX-Nats package, use the following NuGet command: | ||
To install the OfX.EntityFrameworkCore package, use the following NuGet command: | ||
|
||
```bash | ||
dotnet add package OfX-Nats | ||
dotnet add package OfX-EFCore | ||
``` | ||
|
||
Or via the NuGet Package Manager: | ||
|
||
```bash | ||
Install-Package OfX-Nats | ||
Install-Package OfX-EFCore | ||
``` | ||
|
||
--- | ||
|
||
## How to Use | ||
|
||
### 1. Register OfX-Nats | ||
|
||
Add OfX-Nats to your service configuration during application startup: | ||
### 1. Register OfX.EntityFrameworkCore | ||
|
||
For Client: | ||
Add OfX.EntityFrameworkCore to your service configuration during application startup: | ||
|
||
```csharp | ||
builder.Services.AddOfXEntityFrameworkCore(cfg => | ||
{ | ||
cfg.AddContractsContainNamespaces(typeof(SomeContractAssemblyMarker).Assembly); | ||
cfg.AddAttributesContainNamespaces(typeof(WhereTheAttributeDefined).Assembly); | ||
cfg.AddHandlersFromNamespaceContaining<SomeHandlerAssemblyMarker>(); | ||
cfg.AddNats(config => config | ||
config.UseNats((context, bus) => | ||
{ | ||
bus.Host(host, c => | ||
{ | ||
c.Username(userName); | ||
c.Password(password); | ||
}); | ||
bus.ConfigureEndpoints(context); | ||
}); | ||
); | ||
|
||
}) | ||
.AddOfXEFCore(options => | ||
{ | ||
options.AddDbContexts(typeof(TestDbContext)); | ||
options.AddModelConfigurationsFromNamespaceContaining<SomeModelAssemblyMarker>(); | ||
}); | ||
``` | ||
That All, enjoy your moment! | ||
|
||
After installing the package OfX-EFCore, you can use the method `AddDbContexts()`, which takes `DbContext(s)` to executing. | ||
|
||
### 2. Mark the model you want to use with OfXAttribute | ||
Example: | ||
|
||
```csharp | ||
[OfXConfigFor<UserOfAttribute>(nameof(Id), nameof(Name))] | ||
public class User | ||
{ | ||
public string Id { get; set; } | ||
public string Name { get; set; } | ||
public string Email { get; set; } | ||
} | ||
``` | ||
That all! Let go to the moon! | ||
|
||
| Package Name | Description | .NET Version | Document | | ||
|----------------------------------------------------------|-------------------------------------------------------------------------------------------------|--------------|------------------------------------------------------------------------------------------| | ||
| [OfX](https://www.nuget.org/packages/OfX/) | OfX core | 8.0, 9.0 | [ReadMe](https://github.com/quyvu01/OfX/blob/main/README.md) | | ||
| [OfX-EFCore](https://www.nuget.org/packages/OfX-EFCore/) | This is the OfX extension package using EntityFramework to fetch data | 8.0, 9.0 | [ReadMe](https://github.com/quyvu01/OfX/blob/main/src/OfX.EntityFrameworkCore/README.md) | | ||
| [OfX-gRPC](https://www.nuget.org/packages/OfX-gRPC/) | OfX-gRPC is an extension package for OfX that leverages gRPC for efficient data transportation. | 8.0, 9.0 | [ReadMe](https://github.com/quyvu01/OfX/blob/main/src/OfX.Grpc/README.md) | | ||
| [OfX-Nats](https://www.nuget.org/packages/OfX-Nats/) | OfX-Nats is an extension package for OfX that leverages Nats for efficient data transportation. | 8.0, 9.0 | This Document | | ||
Note: In this release, Id is exclusively supported as a string. But hold tight—I'm gearing up to blow your mind with the next update! Stay tuned! | ||
|
||
| Package Name | Description | .NET Version | Document | | ||
|----------------------------------------------------------|-------------------------------------------------------------------------------------------------|--------------|-------------------------------------------------------------------------------| | ||
| [OfX](https://www.nuget.org/packages/OfX/) | OfX core | 8.0, 9.0 | [ReadMe](https://github.com/quyvu01/OfX/blob/main/README.md) | | ||
| [OfX-EFCore](https://www.nuget.org/packages/OfX-EFCore/) | This is the OfX extension package using EntityFramework to fetch data | 8.0, 9.0 | This Document | | ||
| [OfX-gRPC](https://www.nuget.org/packages/OfX-gRPC/) | OfX.gRPC is an extension package for OfX that leverages gRPC for efficient data transportation. | 8.0, 9.0 | [ReadMe](https://github.com/quyvu01/OfX/blob/main/src/OfX.Grpc/README.md) | | ||
| [OfX-Nats](https://www.nuget.org/packages/OfX-Nats/) | OfX-Nats is an extension package for OfX that leverages Nats for efficient data transportation. | 8.0, 9.0 | [ReadMe](https://github.com/quyvu01/OfX/blob/dev-nats/src/OfX.Nats/README.md) | | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using OfX.Abstractions; | ||
using OfX.Attributes; | ||
using OfX.Responses; | ||
|
||
namespace OfX.Nats.Abstractions; | ||
|
||
public interface INatsRequester<TAttribute> where TAttribute : OfXAttribute | ||
{ | ||
Task<ItemsResponse<OfXDataResponse>> RequestAsync(RequestContext<TAttribute> requestContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OfX.Abstractions; | ||
using OfX.Attributes; | ||
using OfX.Responses; | ||
|
||
namespace OfX.Nats.Abstractions; | ||
|
||
public interface IOfXNatsClient<TAttribute> : IMappableRequestHandler<TAttribute> where TAttribute : OfXAttribute | ||
{ | ||
IServiceProvider ServiceProvider { get; } | ||
|
||
async Task<ItemsResponse<OfXDataResponse>> IMappableRequestHandler<TAttribute>.RequestAsync( | ||
RequestContext<TAttribute> context) | ||
{ | ||
var natRequesterService = ServiceProvider.GetRequiredService<INatsRequester<TAttribute>>(); | ||
var result = await natRequesterService.RequestAsync(context); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace OfX.Nats.ApplicationModels; | ||
|
||
public class NatsClient | ||
{ | ||
public string NatsHost { get; private set; } | ||
public NatsCredential NatsCredential { get; } = new(); | ||
|
||
public void Host(string host, Action<NatsCredential> options = null) | ||
{ | ||
NatsHost = host; | ||
options?.Invoke(NatsCredential); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace OfX.Nats.ApplicationModels; | ||
|
||
public class NatsClientRegister | ||
{ | ||
public NatsClient NatsClient { get; } = new(); | ||
|
||
public void UseNats(Action<NatsClient> options) | ||
{ | ||
options.Invoke(NatsClient); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace OfX.Nats.ApplicationModels; | ||
|
||
public class NatsCredential | ||
{ | ||
public string NatsUserName { get; private set; } | ||
public string NatsPassword { get; private set; } | ||
public void UserName(string userName) => NatsUserName = userName; | ||
public void Password(string password) => NatsPassword = password; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NATS.Client; | ||
using OfX.Nats.Abstractions; | ||
using OfX.Nats.ApplicationModels; | ||
using OfX.Nats.Implementations; | ||
using OfX.Nats.Servers; | ||
using OfX.Registries; | ||
|
||
namespace OfX.Nats.Extensions; | ||
|
||
public static class NatsExtensions | ||
{ | ||
public static void AddNats(this OfXRegister ofXRegister, Action<NatsClientRegister> options) | ||
{ | ||
var newClientsRegister = new NatsClientRegister(); | ||
options.Invoke(newClientsRegister); | ||
// Register NATS connection as a singleton | ||
ofXRegister.ServiceCollection.AddSingleton(_ => | ||
{ | ||
var connectionFactory = new ConnectionFactory(); | ||
var natsOptions = ConnectionFactory.GetDefaultOptions(); | ||
natsOptions.AllowReconnect = true; | ||
natsOptions.MaxReconnect = -1; | ||
natsOptions.ReconnectWait = 2000; | ||
natsOptions.Timeout = 5000; | ||
natsOptions.Url = newClientsRegister.NatsClient.NatsHost; | ||
natsOptions.User = newClientsRegister.NatsClient.NatsCredential.NatsUserName; | ||
natsOptions.Password = newClientsRegister.NatsClient.NatsCredential.NatsPassword; | ||
return connectionFactory.CreateConnection(natsOptions); | ||
}); | ||
ClientsRegister(ofXRegister.ServiceCollection); | ||
Clients.ClientsInstaller.InstallMappableRequestHandlers(ofXRegister.ServiceCollection, | ||
typeof(IOfXNatsClient<>), [..ofXRegister.OfXAttributeTypes]); | ||
} | ||
|
||
private static void ClientsRegister(IServiceCollection serviceCollection) => | ||
serviceCollection.AddScoped(typeof(INatsRequester<>), typeof(NatsRequester<>)); | ||
|
||
public static void StartNatsServerAsync(this IServiceProvider serviceProvider) | ||
{ | ||
var serverListening = new NatsServersListening(serviceProvider); | ||
serverListening.StartAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using NATS.Client; | ||
using OfX.Abstractions; | ||
using OfX.Attributes; | ||
using OfX.Nats.Abstractions; | ||
using OfX.Nats.Messages; | ||
using OfX.Responses; | ||
|
||
namespace OfX.Nats.Implementations; | ||
|
||
public sealed class NatsRequester<TAttribute>(IConnection connection) | ||
: INatsRequester<TAttribute> where TAttribute : OfXAttribute | ||
{ | ||
public async Task<ItemsResponse<OfXDataResponse>> RequestAsync(RequestContext<TAttribute> requestContext) | ||
{ | ||
var natsMessageWrapped = new NatsMessageRequestWrapped<TAttribute> | ||
{ | ||
Query = requestContext.Query, | ||
Headers = requestContext.Headers | ||
}; | ||
var reply = await connection.RequestAsync(natsMessageWrapped.Subject, natsMessageWrapped.GetMessageSerialize(), | ||
requestContext.CancellationToken); | ||
var response = Encoding.UTF8.GetString(reply.Data); | ||
return JsonSerializer.Deserialize<ItemsResponse<OfXDataResponse>>(response); | ||
} | ||
} |
Oops, something went wrong.