问题
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?
回答1:
T4 Templates are your best friends here. You almost can't avoid them Option 1 - Modify your existing T4 templates to implement INotifyPropertyChanged
- Create a base class that implements INotifyPropertyChanged
- Modify the getter and setters in your T4 templates to notify of their property changes
Option 2 - Introduce DTOs/ViewModels and use AutoMapper
- Add a new folder to your project (or create another project)
- Add a new POCO generation T4 template
- Modify it slightly to conform to your view model of choice
- 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)#>
<#
来源:https://stackoverflow.com/questions/25861917/ef6-poco-inotifypropertychanged-without-viewmodels