私は以下のクラスがあります。/非同期の流れと混同待つ機能
DataAccessFactory:
public class DataAccessFactory
{
public static IUserAccessLayer User() => new UserAccessLayer(new DataContext());
public static IAuthenticationAccessLayer Authentication() => new AuthenticationAccessLayer(new DataAccess.DataContext());
}
AuthenticationAccessLayer:
public class AuthenticationAccessLayer : IAuthenticationAccessLayer
{
private readonly DataContext context;
public AuthenticationAccessLayer(DataContext context)
{
this.context = context;
}
public async void RegisterAsync(UserRegisterModel model)
{
context.User.Add(new UserModel()
{
EmailAddress = model.Email,
PasswordHash = model.PasswordHash,
PasswordSalt = model.PasswordSalt
});
}
public async Task<bool> EmailExist(string email)
{
var user = await context.User.Where(x => x.EmailAddress.Equals(email)).FirstOrDefaultAsync();
if (user == null)
return false;
else
return true;
}
}
USERSTORE:
public class UserStore : ViewModelBase
{
public UserStore()
{
}
public UserStore(int userID)
{
this.UserID = userID;
}
#region Authentication
public async Task<bool> AuthenticateAsync(LoginModel model)
{
return await DataAccessFactory.Authentication().LoginAsync(model);
}
public async void RegisterUserAsync(UserRegisterModel model)
{
var store = DataAccessFactory.Authentication();
//check if unique email
if(await store.EmailExist(model.Email))
throw new ValidationException($"Email {model.Email} is already registered.");
store.RegisterAsync(model);
}
#endregion
}
をマイ質問は、ユーザーストアは関数で、EmailExist
関数が例外を返すか、例外をスローする前に、UserRegisterModel
がDBに追加されますか?
'AuthenticationAccessLayer.RegisterAsync'は実際には' async'ではありません。 'context.User.Add'は返されず、' Task'が返ってくることもありません。 –
[非同期voidメソッドを避ける](http://haacked.com/archive/2014/11/11/async -void-methods /) – stuartd