Невозможно добавить второй объект при добавлении его в команду CQRS.C#

Место общения программистов C#
Ответить
Anonymous
 Невозможно добавить второй объект при добавлении его в команду CQRS.

Сообщение Anonymous »

Я пытаюсь добавить объект в свой класс команд CQRS, и пока никаких проблем не возникает. Единственная проблема заключается в том, что я пытаюсь добавить второй объект до его завершения. для выполнения.
вот мои объекты и их репозитории:

Код: Выделить всё

ApplicationUser

Код: Выделить всё

    public class ApplicationUser : IdentityUser
{

public string FirstName { get; set; }
public string LastName { get; set; }
public IList UserRewards { get; set; }
}

Код: Выделить всё

    public class Reward
{
public Guid RewardId { get; set; }
public string Name { get; set; }
public IList UserRewards { get; set; }
}

Код: Выделить всё

RewardRepository : BaseRepository

Код: Выделить всё

    public class BaseRepository : IAsyncRepository where T : class
{
private readonly AppDbContext context;

public BaseRepository(AppDbContext context)
{
this.context = context;
}
public async Task AddAsync(T entity)
{
await context.Set().AddAsync(entity);
await context.SaveChangesAsync();

return entity;
}
}

Код: Выделить всё

UserReward

Код: Выделить всё

    public class UserReward
{
public Guid ApplicationUserId { get; set; }
public ApplicationUser User { get; set; }
public Guid RewardId { get; set; }
public Reward Reward { get; set; }
}

Код: Выделить всё

UserRewardRepository : BaseRepository

Код: Выделить всё

    public class BaseRepository : IAsyncRepository where T : class
{
private readonly AppDbContext context;

public BaseRepository(AppDbContext context)
{
this.context = context;
}
public async Task AddAsync(T entity)
{
await context.Set().AddAsync(entity);
await context.SaveChangesAsync();

return entity;
}
}

Код: Выделить всё

AppDbContext

Код: Выделить всё

public class AppDbContext : IdentityDbContext
{
public AppDbContext(DbContextOptions options)
: base(options)
{
}

// This is used to return the UTC time when queried, knowing
// that SQL has an issue returning UTC time by not adding the "z"
// suffix character that tells JS that the time is UTC.
// It's added to entity configurations using .HasConversion(AppDbContext.utcConverter).
public static ValueConverter utcConverter = new ValueConverter(
toDb => toDb,
fromDb =>
DateTime.SpecifyKind(fromDb, DateTimeKind.Utc));

public DbSet Categories { get; set; }
public DbSet Rewards { get; set; }
public DbSet UserReward { get; set;  }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ApplicationUserConfiguration());
modelBuilder.ApplyConfiguration(new CategoryConfiguration());
modelBuilder.ApplyConfiguration(new RewardConfiguration());
modelBuilder.ApplyConfiguration(new UserRewardConfiguration());
}
Контейнер внедрения внедрения служб

Код: Выделить всё

public static IServiceCollection AddInfrastructure(this IServiceCollection services,
IConfiguration configuration,
WebApplicationBuilder builder)
{
services.AddEntityFrameworkNpgsql().AddDbContext(opt =>
opt.UseNpgsql(configuration.GetConnectionString("PostgreSQLServerConnectionString")));

// Used for .NET Aspire container DB
//builder.AddSqlServerDbContext("SomethingDb");

// Add EF Core Identity
services.AddIdentity()
.AddEntityFrameworkStores().AddDefaultTokenProviders();

services.AddScoped(typeof(IAsyncRepository), typeof(BaseRepository));

services.AddTransient();

// Repositories
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();

// Reward services
services.AddScoped();

services.AddSingleton();

services.AddHttpContextAccessor();

services.AddLazyResolution();

Return services
}
}
}
Я пытаюсь это сделать:

Код: Выделить всё

AddRewardCommand

Код: Выделить всё

    public class AddRewardCommand : IRequest
{
public string Name { get; set; }
}

Код: Выделить всё

AddRewardCommandHandler

Код: Выделить всё

    public class AddRewardCommandHandler
: IRequestHandler
{
private readonly IMapper mapper;
private readonly IRewardRepository repository;
private readonly IUserRewardRepository userRewardRepository;
private readonly IAuthenticationService authenticationService;

public AddRewardCommandHandler(IMapper mapper,
IRewardRepository repository,
IUserRewardRepository userRewardRepository,
IAuthenticationService authenticationService)
{
this.mapper = mapper
?? throw new ArgumentNullException(nameof(mapper));
this.repository = repository
?? throw new ArgumentNullException(nameof(repository));
this.userRewardRepository = userRewardRepository
?? throw new ArgumentNullException(nameof(userRewardRepository));
this.authenticationService = authenticationService
?? throw new ArgumentNullException(nameof(authenticationService));
}

public async Task  Handle(AddRewardCommand request, CancellationToken cancellationToken)
{
Domain.Entities.Reward reward;

// Get all users
var userList= await authenticationService.GetAllUsersWithPhoneConfirmedAsync();

// Create a new reward with the HasBeenUsed property false
var newReward = new Domain.Entities.Reward()
{
RewardId = Guid.NewGuid(),
Name = request.Name
};

// Foreach user in the DB, add this new reward to their account
foreach (GetUserResponse user in userList)
{
var userReward = new Domain.Entities.UserReward()
{
ApplicationUserId = user.UserId,
RewardId = newReward.RewardId
};

// /!\
// THIS IS WHERE IT DOESN'T WORK
var t = await userRewardRepository.AddAsync(userReward);
}

try
{
reward = mapper.Map(request);
}
catch (AutoMapperMappingException autoMapperException)
{
throw autoMapperException.InnerException;
}

reward = await repository.AddAsync(reward);

return reward.RewardId;
}
}
Эта строка в цикле foreach почему-то не работает:

Код: Выделить всё

var t = await userRewardRepository.AddAsync(userReward); 
Исключения не создаются, даже при отладке

Подробнее здесь: https://stackoverflow.com/questions/791 ... rs-command
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»