EF6 POCO INotifyPropertyChanged没有viewmodels [英] EF6 POCO INotifyPropertyChanged without viewmodels

查看:299
本文介绍了EF6 POCO INotifyPropertyChanged没有viewmodels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在WPF应用程序中直接绑定到模型类(并且跳过创建单独的viewmodel类)。



现在,在切换到EF6和DBContext之后,我遇到了生成的EF POCO类的问题,因为它看起来有点棘手,甚至不建议尝试使INotifyPropertyChanged接口直接连接到这些类。



目前:




  • 我不想回到ObjectContext。

  • 我也不想改变T4太多。网络上关于更改T4以实现
    的建议INotifyPropertyChanged对我来说看起来太容易出错。

  • 现在为每个类创建视图模型,并纯粹到MVVM
    可能是最好的,但现在很多时间来实现,因为模型
    是巨大的。



我有没有任何选项获得EF6 POCO类自动生成属性以通知其更改?

解决方案

T4模板是您最好的朋友。你几乎无法避免他们
选项1 - 修改现有的T4模板来实现INotifyPropertyChanged


  1. 创建一个实现INotifyPropertyChanged

  2. 修改您的T4模板中的getter和setter以通知其
    属性更改

选项2 - 介绍DTO / ViewModels并使用AutoMapper


  1. 向项目添加一个新文件夹(或创建另一个项目)

  2. 添加新的POCO生成T4模板

  3. 稍微修改以符合您的查看模式选择

  4. 使用AutoMapper将这些Dto / ViewModels映射到实体

选项3 - 实现使用面向对象编程的Postsharp来实现 INotifyPropertyChanged 每个类的一行属性 - 再次,你必须添加几行到您的T4模板



编辑 - 示例
这是一个与我的实体的T4模板,我将[DataContract]属性添加到允许POCO序列化。

  foreach(typeMapper.GetItemsToGenerate< EntityType>(itemCollection)中的var实体)
{
fileManager.StartNewFile(entity.Name +.cs);
BeginNamespace(code);
#>
<#= codeStringGenerator.UsingDirectives(inHeader:false)#>
使用System.Runtime.Serialization;
[DataContract]
<#= codeStringGenerator.EntityClassOpening(entity)#>
{

//再进一步下来
var simpleProperties = typeMapper.GetSimpleProperties(entity);
if(simpleProperties.Any())
{
foreach(var edmProperty in simpleProperties)
{
#>
[DataMember]
<#= codeStringGenerator.Property(edmProperty)#>
<#


I have been binding in WPF application directly to the model classes (and skipping creating individual viewmodel classes).

Now, after switching to EF6 and DBContext, I face an issue with the generated EF POCO classes since it looks its either kind of tricky or not even recommended trying to make INotifyPropertyChanged interface implemented directly to those classes.

Currently:

  • I don't want to go back to ObjectContext.
  • I don't want to change T4 too much either. The suggestions on the web for changing T4 to achieve INotifyPropertyChanged looks too error-prone for me.
  • Creating viewmodels for each class now and going purely to MVVM would probably be best but takes now lot of time to implement since the model is huge.

Do I have any options left to get EF6 POCO class autogenerated properties to notify of their changes?

解决方案

T4 Templates are your best friends here. You almost can't avoid them Option 1 - Modify your existing T4 templates to implement INotifyPropertyChanged

  1. Create a base class that implements INotifyPropertyChanged
  2. Modify the getter and setters in your T4 templates to notify of their property changes

Option 2 - Introduce DTOs/ViewModels and use AutoMapper

  1. Add a new folder to your project (or create another project)
  2. Add a new POCO generation T4 template
  3. Modify it slightly to conform to your view model of choice
  4. Use AutoMapper to map these Dto/ViewModels to the entities

Option 3 - Implement Postsharp which uses aspect oriented programming to implement INotifyPropertyChanged with a one line attribute per class - again, you'll have to add a couple of lines to your T4 template

EDIT - examples Here's a T4 template with my entities, that I added the [DataContract] attributes to allow the POCOs to be serialized.

foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    fileManager.StartNewFile(entity.Name + ".cs");
    BeginNamespace(code);
#>
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
    using System.Runtime.Serialization;
[DataContract]
<#=codeStringGenerator.EntityClassOpening(entity)#>
{

// Then further down
    var simpleProperties = typeMapper.GetSimpleProperties(entity);
    if (simpleProperties.Any())
    {
        foreach (var edmProperty in simpleProperties)
        {
#>
    [DataMember]
    <#=codeStringGenerator.Property(edmProperty)#>
<#

这篇关于EF6 POCO INotifyPropertyChanged没有viewmodels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆