需要有关分层的帮助

时间:2013-05-01 12:56:15

标签: silverlight windows-phone-7 mvvm windows-phone-8 repository-pattern

我正在尝试分层我的WP应用程序&遵循MVVM模式。我有一个带有ICommand的VM,它在View上单击一个按钮时运行。点击按钮现在运行ICommand指向的方法,该方法使用linq从DB检索数据。

以下是我的VM的外观。

public class CategoryViewModel : INotifyPropertyChanged
{
    // Category type is a table in my DB. 
    private Category _currentCategory; 


    public Category CurrentCategory
        {
            get { return _currentCategory; }
            set
            {
                if (value != _currentCategory)
                {
                    _currentCategory = value;
                    OnPropertyChanged("CurrentCategory");
                }
            }
        }

// Helper method hooked with ICommand via RelayCommand class. 
// not posting RelayCommand class code here. 
private void GetCategory()
        {
            using (CategoryDBContext ctx = new CategoryDBContext(CategoryDBContext.ConnectionString))
            {
                CurrentCategory = ctx.Categories.FirstOrDefault();
            }
        }

}

以下是我的观看方式。

<TextBlock Text="{Binding CurrentCategory.CategoryName}" />
<Button Command="{Binding GetCategoryCommand}" Content="Click me"/>

我正在尝试实现一个实现通用存储库和一个工作单元类&amp;有点跟随this文章中提到的想法。如果你现在滚动到“创建一个通用存储库”,你会发现使用DbSet&lt; TEntity&gt;因为他们正在使用EF。什么相当于WP?

我如何在WP应用中做类似的事情?我不希望我的VM中有任何数据访问代码。它应该去哪里?此外,我想要实现通用存储库的原因是避免创建多个存储库类,如CategoryRepositoy,ProductRepositoy等.... 我已经在我的模型中安装了所有POCO课程。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望为所有获取/保存方法设置一个存储库?如果你可以用Windows Phone做到这一点?它在哪里因为你不想在VM中使用它?

使用get和set创建基本界面,这是一个很好的起点 http://www.remondo.net/repository-pattern-example-csharp/

存储库代码不需要直接在VM中,但您仍应该从虚拟机中调用它。

View = ui,Model = data,view model =其他所有内容,例如获取/设置/更新/操作数据。