I have a WPF MVVM Application. I have a Customer Model which is generated by an entity framework (Database first approach) and one CustomerViewModel
where I created an instance of a Customer Model and used Model members inside the ViewModel
property get and set.
Here is my Customer Model:
public partial class TblCustomer
{
public TblCustomer()
{
this.TblSalesInvoices = new HashSet<TblSalesInvoice>();
this.TblSalesOrders = new HashSet<TblSalesOrder>();
}
public int CustomerId { get; set; } //Primary key Identity
public string CustomerName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public virtual TblUser TblUser { get; set; }
public virtual ICollection<TblSalesInvoice> TblSalesInvoices { get; set; }
public virtual ICollection<TblSalesOrder> TblSalesOrders { get; set; }
}
Here is my CustomerViewModel
:
private TblCustomer_customerModel;
public TblCustomer CustomerModel
{
get
{
return _customerModel?? (_customerModel= new TblCustomer());
}
set { _customerModel= value; }
}
#region Bindable Properties
public string CustomerName
{
get
{
return CustomerModel.CustomerName;
}
set
{
CustomerModel.CustomerName= value;
RaisePropertyChanged("CustomerName");
}
}
public string Email
{
get
{
return CustomerModel.Email;
}
set
{
CustomerModel.Email = value;
RaisePropertyChanged("Email");
}
}
private ICommand _saveCommand;
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new RelayCommand(() => Save());
}
return _saveCommand;
}
}
#Endregion Bindable Properties
private void SaveCustomer()
{
using(var context=new R_MaizeEntities())
{
context.TblCustomers.Add(CustomerModel);
context.SaveChanges();
}
}
Is this a correct way create a Model class instance in ViewModel
class and set and get model class members inside the ViewModel
Property get and set? By doing so, does the ViewModel
class have a dependency on the Model
class? Is there any other efficient way to handle this situation?