Is it normal to catch exception in concept of MVVM inside model? Or how should it be? How to improve this code?
This is simple application that allows user to drop .pfx certificate on the label. LoginModel
checks the password for validity and check the publisher. if the validation fails throws an exception that is catching in the LoginViewModel
;
Xaml:
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" AllowDrop="True" Content="{Binding UserCertificateResult}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<Command:EventToCommand Command="{Binding DropUserCertificateCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Label>
<TextBox Grid.Row="0" Grid.Column="1" Height="30" Text="{Binding UserPassword}"></TextBox>
<Button Grid.Row="2" Height="40" Content="Connect" Command="{Binding CreateConnectionCommand}"></Button>
</Grid>
Code:
namespace WpfApplication31
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new LoginViewModel();
}
}
public class LoginViewModel : ViewModelBase
{
LoginModel model = new LoginModel();
private string userCertificateResult = "place user certificate";
public string UserCertificateResult
{
get
{
return userCertificateResult;
}
set
{
userCertificateResult = value;
RaisePropertyChanged("UserCertificateResult");
}
}
public RelayCommand CreateConnectionCommand
{
get
{
return new RelayCommand(CreateConnection);
}
}
public RelayCommand<DragEventArgs> DropUserCertificateCommand
{
get
{
return new RelayCommand<DragEventArgs>(CheckUserCertificate);
}
}
private string userPassword = "123456";
public string UserPassword
{
get
{
return userPassword;
}
set
{
userPassword = value;
}
}
private X509Certificate2 UserCertificate;
private void CheckUserCertificate(DragEventArgs args)
{
try
{
string userCertificatePath = string.Join("", (string[])args.Data.GetData(DataFormats.FileDrop, true));
UserCertificate = model.IsUserCertificateValid(userCertificatePath, UserPassword);
UserCertificateResult = "Succesfully";
}
catch (Exception ex)
{
UserCertificateResult = ex.Message.ToString();
}
}
private void CreateConnection()
{
Service.Connect(UserCertificate);
}
}
public class LoginModel
{
public X509Certificate2 IsUserCertificateValid(string userCertificatePath, string password)
{
X509Certificate2 userCertificate;
try
{
userCertificate = new X509Certificate2(userCertificatePath, password);
if (userCertificate.Issuer != "CN=GO Certification Authority, OU=Certification, O=Test, L=Test, S=Test, C=Test")
{
throw new InvalidIssuerException("Wrong issuer");
}
}
catch (CryptographicException ex)
{
throw new CryptographicException(ex.Message.ToString());
}
return userCertificate;
}
}
public static class Service
{
public static void Connect(X509Certificate2 userCetificate)
{
}
}
public class InvalidIssuerException : Exception
{
public InvalidIssuerException(string message)
: base(message)
{
}
}
}
CryptographicException
only to immediately throw a newCryptographicException
with the exact same message as the caught one? \$\endgroup\$ToString()
, that is completely useless since theMessage
property in anException
is already astring
... \$\endgroup\$