جهت ثبت و دریافت دادهها من از MediateR استفاده کردم.
در مرحله اول context را مطابق الوی گفته شده ایجاد کردم:
services.AddDbContextFactory<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
services.AddScoped<IunitOfWork>(serviceProvider =>
serviceProvider.GetRequiredService<IDbContextFactory<ApplicationDbContext>>().CreateDbContext());
سپس با توجه به اینکه سرویسها و IunitOfwork هم داخل هندلرها تعریف میشوند تنها اینترفیس Idisposable به IunitOfwork
نسبت داده شده است: public interface IUnitOfWork : IDisposable
و سپس
@inherits OwningComponentBase<IMediator>
تا به حال مشکلی در اجرا دیده نشد تا اینکه در حالت OnParameterAsync که دو مرتبه پشت سر هم اجرا میشود با خطاهای زیر روبرو شد:
A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext.
For more information on how to avoid threading issues with DbContext
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.
در صورتی که اگر با یک فلگ کنترل شود که در مرتبههای بعدی اجرا نشود مشکلی نیست.
در این حالت چند سوال مطرح میباشد:
1- آیا استفاده از MediateR به این شکل صحیح است؟ و اینکه استفاده از OwningComponentBase برای آن تاثیری دارد یا خیر اینکه مستقیم هم Inject شود کفایت میکند.
2- به جهت اینکه Onparameter دوبار صدا زده میشود. نحوه واکشی دیتا در این حالت چگونه است؟