[英]In MVVM, with Entity Framework, what populates the view model?
[英]MVVM Model with Entity Framework
我有一個使用WPF + MVVM + PRISM + ENTITY FRAMEWORK的Prototype
問題是如果我使用ENTITY FRAMEWORK實體作為MVVM模式的模型,我會非常困惑。 我有一個業務邏輯層,我在這一層上使用映射器時遇到了問題,因為我對轉換很不滿意( Map問題 )。
我可以做些什么來簡化代碼,使用真實模型而不是Entitie對象(對我來說使用Entitie作為模型在前端是不正確的),考慮到MVVM模式......並保持良好的未來變化,它將在最終版本上擁有200多個實體......
這是我的圖層...(請忘記Mapping,因為我把它放在ViewModel上,但圖像表示正確的圖層)
我也沒有使用存儲庫,因為我只能在BLL上添加更改。
查看模型:我當前的原型做了一個getall,把它放在一個網格上,並且在網格的selectchanged上我把選中的項放在文本框上,然后保存按鈕更新這個更改到數據庫。
public class CadastroClienteViewModel : BindableBase, ICadastroClienteViewModel
{
private readonly IClienteBLL _clienteService;
#region Model
//public Cliente ObCliente { get; private set; }
public int ClienteID
{
get { return ((Cliente)cliItems.CurrentItem).ClienteID; }
set
{
((Cliente)cliItems.CurrentItem).ClienteID = value;
OnPropertyChanged("ClienteID");
}
}
public string Nome
{
get { return ((Cliente)cliItems.CurrentItem).Nome; }
set
{
((Cliente)cliItems.CurrentItem).Nome = value;
OnPropertyChanged("Nome");
}
}
#endregion
public CadastroClienteViewModel(IClienteBLL ServiceCliente)
{
//ObCliente = new Cliente();
_clienteService = ServiceCliente;
this.SaveCommand = new DelegateCommand(ExecuteMethodSave);
this.RefreshCommand = new DelegateCommand(ExecuteMethodRefresh, CanExecuteMethodRefresh);
RefreshCommand.Execute(null);
}
private void ExecuteMethodSave()
{
_clienteService.ClienteBLL_Update(((Cliente)cliItems.CurrentItem));
RefreshCommand.Execute(null);
}
private bool CanExecuteMethodRefresh()
{
return true;
}
private void ExecuteMethodRefresh()
{
var personViewModels = _clienteService.ClienteBLL_GetAll();
//cliente = new ObservableCollection<Cliente>(personViewModels);
cliItems = new ListCollectionView(personViewModels.ToList());
cliItems.CurrentChanged += CliItemsOnCurrentChanged;
//OnPropertyChanged("cliente");
OnPropertyChanged("cliItems");
}
private void CliItemsOnCurrentChanged(object sender, EventArgs eventArgs)
{
//OnPropertyChanged("ObCliente");
}
public ICommand SaveCommand { get; private set; }
public ICommand RefreshCommand { get; private set; }
//public ObservableCollection<Cliente> cliente { get; private set; }
public ICollectionView cliItems { get; private set; }
}
MODEL(我不使用它...但我想):
public class MCliente
{
public int ClienteID { get; set; }
public string Nome { get; set; }
}
EF Entitie:
namespace Sistema.DataEntities.Models
{
public class Cliente
{
public Cliente()
{
}
public int ClienteID { get; set; }
public string Nome { get; set; }
}
BLL:
public class ClienteBLL : IClienteBLL
{
readonly ISistemaContext _context;
public ClienteBLL(ISistemaContext context)
{
_context = context;
}
public IEnumerable<Cliente> ClienteBLL_GetAll()
{
return _context.Cliente.AsEnumerable();
}
public Cliente ClienteBLL_GetByID(int id)
{
return _context.Cliente.Find(id);
}
public bool ClienteBLL_Adicionar(Cliente Obcliente)
{
_context.Cliente.Add(Obcliente);
return _context.SaveChanges() > 0;
}
public bool ClienteBLL_Update(Cliente Obcliente)
{
_context.Cliente.Attach(Obcliente);
_context.Entry(Obcliente).State = EntityState.Modified;
return _context.SaveChanges() > 0;
}
public bool ClienteBLL_Delete(int id)
{
var clubMember = _context.Cliente.Find(id);
_context.Cliente.Remove(clubMember);
return _context.SaveChanges() > 0;
}
即使它不是你問題的最終答案(因為它是基於意見的),我也會將此作為答案(不是評論)添加,但它不適合作為評論。 這就是我要為需要數據庫的WPF應用程序做的事情。
我完全放棄了將WPF應用程序直接連接到數據庫的想法。 我將構建一個3層架構,即我將創建一個無狀態的Web服務,它可以完成服務器端的所有工作。
所以你會:
這種方法的一些好處:
5年后 ,互聯網成為公司項目的最大焦點,所有新項目都通過無狀態API和SPA前端完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.