簡體   English   中英

具有實體框架的MVVM模型

[英]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服務,它可以完成服務器端的所有工作。

所以你會:

  • 數據庫
  • 連接到數據庫的webservice(使用WCF),為你做所有的數據(我甚至會讓它對業務負責)
  • WPF應用程序,連接到Web服務:
    • View層是您的XAML +您的代碼隱藏
    • ViewModel層就是你的ViewModel(超出了你的問題范圍,但隨時可以詢問你是否對該層有任何疑問)。 ViewModels異步調用Web服務
    • Model是客戶端WCF代理

這種方法的一些好處:

  • 取決於硬件/網絡架構, 僅對服務器進行一次調用而不是N次調用可能是一個巨大的性能優勢 (假設數據庫和Web服務之間的延遲(兩者都在“服務器端”)低於兩者之間的延遲) WPF應用程序和數據庫)
  • 更具可擴展性
  • 無狀態方法的所有好處 :每個Web服務請求一個實體框架上下文實例化,更容易處理並發問題(如果您同時運行N個WPF實例)
  • 更容易維護(層之間的松散耦合)
  • 更容易測試(假設您實際構建測試)
  • 更好的安全性(無需通過網絡公開對數據庫的直接訪問)

更新:

5年后 ,互聯網成為公司項目的最大焦點,所有新項目都通過無狀態API和SPA前端完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM