i have senario for change Active Status property use . IsActive
.
i have problem with update entity in asp core and DomainDriven Desogn .
when i change the IsActive
to True
or False
it create a new record inctance a update that recoed .
i put here my code :
Step One :
this is my controller , in this controller i call the Command :
public async Task<ApiReturn> ChangeUserActiveStatus(long id)
{
var result = await dispatchers.SendAsync(new UserActiveStateCommand { id = id });
if(result.Success)
{
return Ok();
}
return BadRequest(result.Exception);
}
step Two : i send a request to the database and give The desired record :
public class UserActiveStateCommandHandler : ICommandHandler<UserActiveStateCommand, OperationResult<string>>, IScopedDepency
{
private readonly IDomainUnitOfWork unitOfWork;
private readonly IDispatchers dispatchers;
public UserActiveStateCommandHandler(IDomainUnitOfWork unitOfWork, IDispatchers dispatchers)
{
this.unitOfWork = unitOfWork;
this.dispatchers = dispatchers;
}
public async Task<OperationResult<string>> HandlerAsync(UserActiveStateCommand command)
{
var user = await dispatchers.QueryAsync(new GetUserByIdQuery { id = command.id });
if (user != null)
{
var u = new User();
var value = u.SetUser(user.Result.Id, user.Result.Email, user.Result.LastName, user.Result.Photo, user.Result.FirstName, user.Result.UserName, user.Result.PhoneNumber, !user.Result.IsActive);
await dispatchers.CallEvent(new UserActiveStateEvent { User = value });
return OperationResult<string>.BuildSuccessResult("success");
}
else
{
return OperationResult<string>.BuildFailure("fail");
}
}
}
}
this is that recoed and its correct :
"email": "kiadr9372@gmail.com",
"photo": null,
"lastName": "Dortaj",
"firstName": "Kianoush",
"userName": "kia9372",
"phoneNumber": "09159810616",
"securityStamp": "e7cd82a5-71f3-41a1-b239-4d705b6d5d35",
"isActive": false,
"phoneConfirm": false,
"emailConfirm": false,
"isLockOut": false,
"lockOutEnd": null,
"isFailurAccount": 0,
"userRoles": null,
"id": 1
step there :
i call Event for Update Record in database :
public class UserActiveStatusEventHandler : IEventHandler<UserActiveStateEvent> , IScopedDepency
{
private readonly IDomainUnitOfWork unitOfWork;
public UserActiveStatusEventHandler(IDomainUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public async Task HandelEventAsync(UserActiveStateEvent @event)
{
var state = await unitOfWork.UserRepository.UpdateUser(@event.User);
if (state.Success)
{
unitOfWork.Commit();
}
}
}
this is the update code in repository :
public async Task<OperationResult<bool>> UpdateUser(User user)
{
if (user != null)
{
try
{
context.Update(user);
return OperationResult<bool>.BuildSuccessResult(true);
}
catch (Exception ex)
{
return OperationResult<bool>.BuildFailure(ex);
}
}
return OperationResult<bool>.BuildFailure("user Cannot be null");
}
and this is the commit code :
public void Commit()
{
context.SaveChanges();
}
this is User
Aggregate :
public class User : AggregrateRoot, IAggregate
{
#region Backing Field
private string _email;
private string _photo;
private string _userName;
private string _phoneNumber;
private string _firstName;
private string _lastName;
private Guid _securityStamp;
private bool _isActive;
private bool _phoneConfirm;
private bool _emailConfirm;
private bool _isLockOut;
private DateTimeOffset? _lockOutEnd;
private int _isFailurAccount;
#endregion
#region Properies
public string Email
{
get
{
return _email;
}
private set
{
_email = value;
SetNotification();
}
}
public string Photo
{
get
{
return _photo;
}
set
{
_photo = value;
SetNotification();
}
}
public string LastName
{
get
{
return _lastName;
}
private set
{
_lastName = value;
SetNotification();
}
}
public string FirstName
{
get
{
return _firstName;
}
private set
{
_firstName = value;
SetNotification();
}
}
public string UserName
{
get
{
return _userName;
}
private set
{
_userName = value;
SetNotification();
}
}
public string PhoneNumber
{
get
{
return _phoneNumber;
}
private set
{
_phoneNumber = value;
SetNotification();
}
}
public Guid SecurityStamp
{
get
{
return _securityStamp;
}
private set
{
_securityStamp = value;
SetNotification();
}
}
public bool IsActive
{
get
{
return _isActive;
}
private set
{
_isActive = value;
SetNotification();
}
}
public bool PhoneConfirm
{
get
{
return _phoneConfirm;
}
private set
{
_phoneConfirm = value;
SetNotification();
}
}
public bool EmailConfirm
{
get
{
return _emailConfirm;
}
private set
{
_emailConfirm = value;
SetNotification();
}
}
public bool IsLockOut
{
get
{
return _isLockOut;
}
private set
{
_isLockOut = value;
SetNotification();
}
}
public DateTimeOffset? LockOutEnd
{
get
{
return _lockOutEnd;
}
private set
{
_lockOutEnd = value;
SetNotification();
}
}
public int IsFailurAccount
{
get
{
return _isFailurAccount;
}
private set
{
_isFailurAccount = value;
SetNotification();
}
}
public ICollection<UserRole> UserRoles { get; set; }
#endregion
public User()
{
}
#region Private Constructor
private User(long id,string Email, string LastName, string photo, string FirstName, string UserName, string PhoneNumber, bool isActive)
{
this.Email = Email;
this.Photo = photo;
this.LastName = LastName;
this.FirstName = FirstName;
this.UserName = UserName;
this.PhoneNumber = PhoneNumber;
this.SecurityStamp = Guid.NewGuid();
this.IsActive = isActive;
this.PhoneConfirm = false;
this.EmailConfirm = false;
this.IsLockOut = false;
this.LockOutEnd = null;
this.IsFailurAccount = 0;
}
#endregion
#region Methonds
public User SetUser(string email, string lastName, string photo, string firstName, string userName, string phoneNumber, bool isActive = false)
{
var user = new User(email, lastName, firstName, photo, userName, phoneNumber, isActive);
return user;
}
public UserRole AddUserRole(long userId, long roleId)
{
return new UserRole().AddUserRole(userId, roleId);
}
#endregion
}
but problem is here : i create a new recoed in datebase but i need update that recoed . how can i solve this problem ???
I guess your problem occurs in UserActiveStateCommandHandler class. You're fetching User from DB which should be tracked by EF by default. Later you're creating new User object instance and provide changes on new object which is not recognized by EF as existing one. You should update data directly on object returned from your query...