Skip to content

convert sync dependy creation to async#35

Open
AlexScigalszky wants to merge 9 commits intoElectNewt:mainfrom
AlexScigalszky:feat/16_Create_shared.awaiter
Open

convert sync dependy creation to async#35
AlexScigalszky wants to merge 9 commits intoElectNewt:mainfrom
AlexScigalszky:feat/16_Create_shared.awaiter

Conversation

@AlexScigalszky
Copy link
Contributor

No description provided.

@AlexScigalszky AlexScigalszky marked this pull request as ready for review January 31, 2024 23:45
Copy link
Owner

@ElectNewt ElectNewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think overall is good, it might look confusing that sometimes we need to use await and sometimes we dont when defining services, but I think it is okey.

Then is the comment about mongo which is the only real thing that is concerning me.

finally this, when i do dotnet build on my machine i am getting an error, which it is very likely cause by some verision mismatch or similar, I have no time to investigate it, but if when you review the changes it is not happening to you, please let me konw and i will try to fix it on my side.

this is the error:

CSC : error AD0001: Analyzer 'Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer' threw an exception of
type 'System.InvalidOperationException' with message 'Failed to resolve well-known type 'Microsoft.AspNetCore.Builder.E
ndpointRouteBuilderExtensions'.'. [C:\Repos\Distribt\src\Shared\Distribt.Shared.Setup\Distribt.Shared.Setup.csproj]
CSC : error AD0001: Analyzer 'Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer' threw an exception of
type 'System.InvalidOperationException' with message 'Failed to resolve well-known type 'Microsoft.AspNetCore.Builder.E
ndpointRouteBuilderExtensions'.'. [C:\Repos\Distribt\src\Shared\Distribt.Shared.Setup\Distribt.Shared.Setup.csproj]

serviceCollection.AddScoped<IProductRepository, ProductRepository>();
await serviceCollection.AddDistribtMongoDbConnectionProvider(configuration, "productStore");
serviceCollection.AddScoped<IProductRepository>( (IServiceProvider serviceProvider) =>
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so there are a couple of things here that are not correct.

1 - as the productRepository needs mongo, you have to do all this dirty, that will apply to any mongo-related service, which makes it a bad idea.
2 - ideally it should be abstrated away. (like the mongoprovider inside AddDistribtMongoDbConnectionProvider)

But the most important part is that the repository must be injected and thats it, if the way mongo is configured is the issue, the solution is to change the mongo implementation, not to change all the services that use the current implementation.

that is one of the reasons why the ProductRepository uses the provider instead of the mongoobject.

@AlexScigalszky
Copy link
Contributor Author

There is an option that is to add the suffix "Async" to all those methods that need to be asynchronous. I'm not well-oriented to that approach but it's an option to respond to your concern about "await" uses.

Speaking of your issue with dotnet build looks like you need to run dotnet restore. At least I couldn't reproduce that error. Maybe if you tell more details I can fix it

@ElectNewt
Copy link
Owner

There is an option that is to add the suffix "Async" to all those methods that need to be asynchronous. I'm not well-oriented to that approach but it's an option to respond to your concern about "await" uses.

Speaking of your issue with dotnet build looks like you need to run dotnet restore. At least I couldn't reproduce that error. Maybe if you tell more details I can fix it

nah, forget about using async, the task one gives an error if it does not get awaited, that should be enough.

Let me know when you finish and I will review again.

@AlexScigalszky
Copy link
Contributor Author

@ElectNewt I finish and it runs completely on my machine 😄

Copy link
Owner

@ElectNewt ElectNewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of comments.
I cloned again the repo and I still having the issues only in this branch, so please rebase it from main as I added github workflows to build and run a test (just to see if it is my pc or whats the story here)
thanks.

public ProductsReadStore(IMongoDbConnectionProvider mongoDbConnectionProvider, IOptions<DatabaseConfiguration> databaseConfiguration)
{
_mongoClient = new MongoClient(mongoDbConnectionProvider.GetMongoUrl());
var mongoUrl = mongoDbConnectionProvider.GetMongoUrl().GetAwaiter().GetResult();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this getmongo is used always with .GetAwaiter().GetResult(); instead of do this all the time, i think is better to have the sync version even if it is in the interface MongoUrl GetMongoUrlSync(); and do the getawaiter.getresult on that method

Or even use it outside of the constructor 🤔 but for that we will need a wrapper around the mongoclient, and something like

public class DistribtMongoClient
{
 private readonly provider;
 
 public async Task<MongoClient> CreateClient(){
    return new MongoClient(await provider.getMongoURL())
 }
}

what do you think?

serviceCollection.AddTransient<IEventStoreManager>((IServiceProvider serviceProvider) =>
{
var mongoDbConnectionProvider = serviceProvider.GetService<IMongoDbConnectionProvider>()
?? throw new ArgumentNullException(nameof(IMongoDbConnectionProvider));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate exceptions, and in this case is not an exception 😂;

you can do getrequiredservice instead of getservice and it will break if it is not there;

this said, i think we can remove all of this getservices thingy, and think about the wrapper on the mongoclient as that will fix the issue of passing the mongourl to the mongoEventStoreManager too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants