Skip to content

Commit

Permalink
Merge pull request #302 from hassanhabib/users/glhays/documentation-v…
Browse files Browse the repository at this point in the history
…2.10.3-abstractionrefinement

DOCUMENTATION: v2.10.3 Abstraction Refinement
  • Loading branch information
glhays committed Aug 15, 2024
2 parents beccd6b + 154a06d commit 23708e7
Show file tree
Hide file tree
Showing 3 changed files with 374 additions and 161 deletions.
124 changes: 62 additions & 62 deletions 1. Brokers/1. Brokers.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ Brokers must satisfy a local contract and implement a local interface to allow d
For instance, given that we have a local contract, `IStorageBroker` that requires an implementation for any given CRUD operation for a local model `Student` - the contract operation would be as follows:

```csharp
public partial interface IStorageBroker
{
ValueTask<IQueryable<Student>> SelectAllStudentsAsync();
}
public partial interface IStorageBroker
{
ValueTask<IQueryable<Student>> SelectAllStudentsAsync();
}
```

An implementation for a storage broker would be as follows:

```csharp
public partial class StorageBroker
{
public DbSet<Student> Students { get; set; }
public partial class StorageBroker
{
public DbSet<Student> Students { get; set; }

public async ValueTask<IQueryable<Student>> SelectAllStudentsAsync() =>
await SelectAllAsync<Student>();
}
public async ValueTask<IQueryable<Student>> SelectAllStudentsAsync() =>
await SelectAllAsync<Student>();
}
```
A local contract implementation can be replaced at any point, from utilizing the Entity Framework as shown in the previous example to using a completely different technology like Dapper or an entirely different infrastructure like an Oracle or PostgreSQL database.

Expand All @@ -57,8 +57,8 @@ Brokers should not have any form of flow control, such as if statements, while l
For instance, a broker method that retrieves a list of students from a database would look something like this:

```csharp
public async ValueTask<IQueryable<Student>> SelectAllStudentsAsync() =>
await SelectAllAsync<Student>();
public async ValueTask<IQueryable<Student>> SelectAllStudentsAsync() =>
await SelectAllAsync<Student>();
```
A simple function that calls the native EntityFramework `DbSet<T>` and returns a local model like `Student`.

Expand All @@ -73,26 +73,26 @@ Brokers are also required to handle their configurations - they may have a depen
For instance, connection strings in database communications are required to be retrieved and passed into the database client to establish a successful connection, as follows:

```csharp
public partial class StorageBroker : EFxceptionsContext, IStorageBroker
{
private readonly IConfiguration configuration;
public partial class StorageBroker : EFxceptionsContext, IStorageBroker
{
private readonly IConfiguration configuration;

public StorageBroker(IConfiguration configuration)
{
this.configuration = configuration;
this.Database.Migrate();
}
public StorageBroker(IConfiguration configuration)
{
this.configuration = configuration;
this.Database.Migrate();
}

...
...
...
...

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
string connectionString = this.configuration.GetConnectionString("DefaultConnection");
optionsBuilder.UseSqlServer(connectionString);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
string connectionString = this.configuration.GetConnectionString("DefaultConnection");
optionsBuilder.UseSqlServer(connectionString);
}
}
```

### 1.2.4 Natives from Primitives
Expand Down Expand Up @@ -128,8 +128,8 @@ If a broker supports an API endpoint, it shall follow the RESTFul semantics, suc

```csharp

public async ValueTask<Student> PostStudentAsync(Student student) =>
await this.PostAsync(RelativeUrl, student);
public async ValueTask<Student> PostStudentAsync(Student student) =>
await this.PostAsync(RelativeUrl, student);

```

Expand Down Expand Up @@ -343,60 +343,60 @@ namespace OtripleS.Web.Api.Brokers.Storages
#### For IDateTimeBroker.cs:
```csharp
public interface IDateTimeBroker
{
ValueTask<DateTimeOffset> GetCurrentDateTimeOffsetAsync();
}
{
ValueTask<DateTimeOffset> GetCurrentDateTimeOffsetAsync();
}
```

#### For DateTimeBroker.cs:
```csharp
public class DateTimeBroker : IDateTimeBroker
{
public async ValueTask<DateTimeOffset> GetCurrentDateTimeOffsetAsync() =>
DateTimeOffset.UtcNow;
}
{
public async ValueTask<DateTimeOffset> GetCurrentDateTimeOffsetAsync() =>
DateTimeOffset.UtcNow;
}
```

#### For ILoggingBroker.cs:
```csharp
public interface ILoggingBroker
{
ValueTask LogInformationAsync(string message);
ValueTask LogTraceAsync(string message);
ValueTask LogDebugAsync(string message);
ValueTask LogWarningAsync(string message);
ValueTask LogErrorAsync(Exception exception);
ValueTask LogCriticalAsync(Exception exception);
}
{
ValueTask LogInformationAsync(string message);
ValueTask LogTraceAsync(string message);
ValueTask LogDebugAsync(string message);
ValueTask LogWarningAsync(string message);
ValueTask LogErrorAsync(Exception exception);
ValueTask LogCriticalAsync(Exception exception);
}
```

#### For LoggingBroker.cs:
```csharp
public class LoggingBroker : ILoggingBroker
{
private readonly ILogger<LoggingBroker> logger;
{
private readonly ILogger<LoggingBroker> logger;

public LoggingBroker(ILogger<LoggingBroker> logger) =>
this.logger = logger;
public LoggingBroker(ILogger<LoggingBroker> logger) =>
this.logger = logger;

public async ValueTask LogInformationAsync(string message) =>
this.logger.LogInformation(message);
public async ValueTask LogInformationAsync(string message) =>
this.logger.LogInformation(message);

public async ValueTask LogTraceAsync(string message) =>
this.logger.LogTrace(message);
public async ValueTask LogTraceAsync(string message) =>
this.logger.LogTrace(message);

public async ValueTask LogDebugAsync(string message) =>
this.logger.LogDebug(message);
public async ValueTask LogDebugAsync(string message) =>
this.logger.LogDebug(message);

public async ValueTask LogWarningAsync(string message) =>
this.logger.LogWarning(message);
public async ValueTask LogWarningAsync(string message) =>
this.logger.LogWarning(message);

public async ValueTask LogErrorAsync(Exception exception) =>
this.logger.LogError(exception.Message, exception);
public async ValueTask LogErrorAsync(Exception exception) =>
this.logger.LogError(exception.Message, exception);

public async ValueTask LogCriticalAsync(Exception exception) =>
this.logger.LogCritical(exception, exception.Message);
}
public async ValueTask LogCriticalAsync(Exception exception) =>
this.logger.LogCritical(exception, exception.Message);
}
```

## 1.6 Summary
Expand Down
Loading

0 comments on commit 23708e7

Please sign in to comment.