convert sync dependy creation to async#35
convert sync dependy creation to async#35AlexScigalszky wants to merge 9 commits intoElectNewt:mainfrom
Conversation
ElectNewt
left a comment
There was a problem hiding this comment.
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) => | ||
| { |
There was a problem hiding this comment.
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.
src/Services/Products/Distribt.Services.Products.Api.Read/Program.cs
Outdated
Show resolved
Hide resolved
|
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 |
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. |
|
@ElectNewt I finish and it runs completely on my machine 😄 |
ElectNewt
left a comment
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
No description provided.