Hi,
My WPF application uses an SQlite database which exposes is functionality by an OpenAccess ORM Domain Model, and is then wrapped in an MVVM schema. One of the tables in the database is called ValidUsers, I have created a Metadata class called ValidUsersMetadata as per the guidance in the OpenAccess documentation and is shown below:
The base ValidUser definition is in the class VisorDataModel which is generated by the OpenAccess Domain Model wizard.
The rest of the MVVM definitions for ValidUsers are shown below:
One of the functions of the application is to display/add/delete/edit ValidUser records. This is accomplished by a RadDataGrid and a pop-up RadDataForm. The RadDataForm uses custom fields, one of which is defined as a DataFormComboBoxField.
All of this works fine but I now want to use the DataAnnotations which I have added in the ValidUserMetadata class. So far I have not found how to do this.
Any help gratefully received.
Thanks
My WPF application uses an SQlite database which exposes is functionality by an OpenAccess ORM Domain Model, and is then wrapped in an MVVM schema. One of the tables in the database is called ValidUsers, I have created a Metadata class called ValidUsersMetadata as per the guidance in the OpenAccess documentation and is shown below:
using
System;
using
System.Linq;
using
System.ComponentModel;
using
System.ComponentModel.DataAnnotations;
namespace
VisorDataModel
{
[MetadataTypeAttribute(
typeof
(ValidUser.ValidUserMetadata))]
public
partial
class
ValidUser : IEditableObject
{
internal
sealed
class
ValidUserMetadata
{
public
ValidUserMetadata()
{
}
[Required(ErrorMessage =
"User ID is required"
)]
public
int
UserID
{
get
;
set
;
}
[Required(ErrorMessage =
"User Name is required."
)]
[StringLength(6,ErrorMessage=
"User name must be at least 6 characters"
)]
public
string
UserName
{
get
;
set
;
}
[Required(ErrorMessage =
"Password is required."
)]
[StringLength(6, ErrorMessage =
"Password must be at least 6 characters"
)]
public
string
UserPassword
{
get
;
set
;
}
[Required(ErrorMessage =
"User Class is required."
)]
public
int
UserClass
{
get
;
set
;
}
}
}
}
The base ValidUser definition is in the class VisorDataModel which is generated by the OpenAccess Domain Model wizard.
The rest of the MVVM definitions for ValidUsers are shown below:
using
System;
using
System.Collections.Generic;
using
System.Linq;
namespace
VisorDataModel
{
public
interface
IValidUsersModel
{
IEnumerable<ValidUser> LoadValidUsers();
IEnumerable<UserClass> LoadUserClasses();
ValidUser NewValidUser();
void
AddValidUser(ValidUser validUser);
void
DeleteValidUser(ValidUser validUser);
void
SaveChanges();
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
namespace
VisorDataModel
{
/// <summary>
/// Represents a source of ValidUsers
/// </summary>
public
class
ValidUsersModel : IValidUsersModel
{
private
EntitiesModel dbContext =
new
EntitiesModel();
public
IEnumerable<ValidUser> LoadValidUsers()
{
return
dbContext.ValidUsers.ToList();
}
public
IEnumerable<UserClass> LoadUserClasses()
{
return
dbContext.UserClasses.ToList();
}
public
ValidUser NewValidUser()
{
ValidUser newValidUser =
new
ValidUser { UserName =
"New User"
, UserPassword =
"New Password"
, UserClass = 0 };
int
newUserId = 0;
try
{
newUserId = (from vu
in
dbContext.ValidUsers
select (vu.UserID)).Max() +1;
}
catch
{
newUserId = 0;
}
newValidUser.UserID = newUserId;
return
newValidUser;
}
public
void
AddValidUser(ValidUser validUser)
{
dbContext.Add(validUser);
dbContext.SaveChanges();
}
public
void
DeleteValidUser(ValidUser validUser)
{
dbContext.Delete(validUser);
dbContext.SaveChanges();
}
public
void
SaveChanges()
{
dbContext.SaveChanges();
}
}
}
using
System;
using
System.Linq;
using
System.Collections.ObjectModel;
using
System.ComponentModel;
namespace
VisorDataModel
{
public
class
ValidUsersViewModel : INotifyPropertyChanged
{
private
readonly
IValidUsersModel dataModel;
private
ValidUser _selectedValidUser;
private
DelegateCommand _addValidUserCommand;
private
DelegateCommand _deleteValidUserCommand;
private
DelegateCommand _saveChangesCommand;
private
ObservableCollection<ValidUser> _validUsers;
private
ObservableCollection<UserClass> _userClasses;
public
event
PropertyChangedEventHandler PropertyChanged;
private
ObservableCollection<ValidUser> _originalValidUsers;
#region initialisation
public
ValidUsersViewModel()
{
this
.dataModel =
new
ValidUsersModel();
}
#endregion
public
ObservableCollection<ValidUser> ValidUsers
{
get
{
if
(
this
._validUsers ==
null
)
{
this
._validUsers =
new
ObservableCollection<ValidUser>();
if
(
this
._originalValidUsers ==
null
)
{
this
._originalValidUsers =
this
._validUsers;
}
LoadValidUsers();
}
return
this
._validUsers;
}
}
public
ObservableCollection<UserClass> UserClasses
{
get
{
if
(
this
._userClasses ==
null
)
{
this
._userClasses =
new
ObservableCollection<UserClass>();
LoadUserClasses();
}
return
this
._userClasses;
}
}
#region SelectValidUser property
public
ValidUser SelectedValidUser
{
get
{
return
_selectedValidUser;
}
set
{
if
(
this
._selectedValidUser == value)
return
;
this
._selectedValidUser = value;
this
.OnPropertyChanged(
"SelectedValidUser"
);
}
}
#endregion
public
DelegateCommand AddValidUserCommand
{
get
{
if
(
this
._addValidUserCommand ==
null
)
this
._addValidUserCommand =
new
DelegateCommand(
this
.OnAddCommandExecute);
return
_addValidUserCommand;
}
}
public
DelegateCommand DeleteValidUserCommand
{
get
{
if
(
this
._deleteValidUserCommand ==
null
)
this
._deleteValidUserCommand =
new
DelegateCommand(
this
.OnDeleteCommandExecute);
return
_deleteValidUserCommand;
}
}
public
DelegateCommand SaveChangesCommand
{
get
{
if
(
this
._saveChangesCommand ==
null
)
this
._saveChangesCommand =
new
DelegateCommand(
this
.OnSaveCommandExecute);
return
_saveChangesCommand;
}
}
private
void
LoadValidUsers()
{
this
._validUsers.Clear();
foreach
(var item
in
this
.dataModel.LoadValidUsers())
{
this
._validUsers.Add(item);
}
}
private
void
LoadUserClasses()
{
this
._userClasses.Clear();
foreach
(var item
in
this
.dataModel.LoadUserClasses())
{
this
._userClasses.Add(item);
}
}
private
void
OnAddCommandExecute(
object
parameter)
{
ValidUser newValidUser =
this
.dataModel.NewValidUser();
this
.ValidUsers.Add(newValidUser);
this
.SelectedValidUser = newValidUser;
this
.dataModel.AddValidUser(newValidUser);
}
private
void
OnDeleteCommandExecute(
object
parameter)
{
if
(
this
.SelectedValidUser !=
null
)
{
this
.dataModel.DeleteValidUser(
this
.SelectedValidUser);
this
.ValidUsers.Remove(
this
.SelectedValidUser);
}
}
private
void
OnSaveCommandExecute(
object
parameter)
{
this
.dataModel.SaveChanges();
}
protected
virtual
void
OnPropertyChanged(
string
info)
{
if
(
this
.PropertyChanged !=
null
)
this
.PropertyChanged(
this
,
new
PropertyChangedEventArgs(info));
}
}
}
One of the functions of the application is to display/add/delete/edit ValidUser records. This is accomplished by a RadDataGrid and a pop-up RadDataForm. The RadDataForm uses custom fields, one of which is defined as a DataFormComboBoxField.
All of this works fine but I now want to use the DataAnnotations which I have added in the ValidUserMetadata class. So far I have not found how to do this.
Any help gratefully received.
Thanks