Automapper ObservableCollection - 刷新无效

时间:2013-07-16 11:09:10

标签: automapper

我有小型WPF应用程序。解决方案中有5个项目。

我想要使用UI ENTITIES的单独DOMAIN类,我想使用AUTOMAPPER。

您可以在此处下载整个解决方案:TestWPFAutomapper.zip

具有UI实体(Entities.Destination.cs)的域类(Domain.Source.cs)具有相同的签名。

在Entities.Destination.cs中我想提出其他逻辑。

namespace DOMAIN
{
    public class Source
    {
        public int Id { get; set; }
        public int Position { get; set; }
    } 
}


using System.ComponentModel;

namespace ENITITIES
{
    public class Destination : INotifyPropertyChanged 
    {
        private int _id;
        private int _position;

        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                OnPropertyChanged("Id");
            }
        }

        public int Position
        {
            get { return _position; }
            set
            {
                _position = value;
                OnPropertyChanged("Position");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我的数据来自使用Entity Framework和CodeFirst的DAL.DataContext。这里我使用Source类。

using System.Data.Entity;
using DOMAIN;

namespace DAL
{
    public class DataContext : DbContext
    {
        public DbSet<Source> Sources { get; set; }
    }
}

映射在BL.MyAppLogic.cs中。在这个类中,我有属性Items,它是ObservableCollection。

将另一个项目放入DB for Source类集合后,刷新但是Destination不刷新。

using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using AutoMapper;
using DAL;
using DOMAIN;
using ENITITIES;

namespace BL
{
    public class MyAppLogic
    {
        private readonly DataContext _dataContext = new DataContext();
        public ObservableCollection<Source> Items { get; set; }
        //public ObservableCollection<Destination> Items { get; set; }

        public MyAppLogic()
        {
            Database.SetInitializer(new MyInitializer());
            Mapping();

            _dataContext.Sources.Load();
            Items = _dataContext.Sources.Local;
            //Items = Mapper.Map<ObservableCollection<Source>, ObservableCollection<Destination>>(_dataContext.Sources.Local);
        }

        private void Mapping()
        {
            Mapper.CreateMap<Source, Destination>().ReverseMap();
            // I tried also Mapper.CreateMap<ObservableCollection<Source>, ObservableCollection<Destination>>().ReverseMap();
        }

        public int GetLastItem()
        {
            return _dataContext.Database.SqlQuery<int>("select Position from Sources").ToList().LastOrDefault();
        }

        public void AddNewItem(Destination newItem)
        {
            _dataContext.Sources.Add(Mapper.Map<Destination, Source>(newItem));
            _dataContext.SaveChanges();
        }
    }
}

我的问题不在于映射,这是有效的,但在从db添加或删除项目后进行刷新收集。如果我使用DOMAIN.Source类一切正常,集合是令人耳目一新的。但是当我使用ENTITIES.Destination数据来自数据库时,我也可以将新的数据输入到数据库,但是刷新ObservableCollection不起作用。

请尝试评论BL.MyAppLogic.cs中的行(14&amp; 23)并取消注释(15&amp; 24),你就会明白我的意思。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我明白了,但我不知道是否正确。

Local有CollectionChanged事件

所以在构造函数中我放了这些行

public MyAppLogic()
        {
            Database.SetInitializer(new MyInitializer());
            Mapping();

            _dataContext.Sources.Load();

            _dataContext.Sources.Local.CollectionChanged += SourcesCollectionChanged;

            Items = Mapper.Map<ObservableCollection<Source>, ObservableCollection<Destination>>(_dataContext.Sources.Local);

        }

和处理程序看起来

private void SourcesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            var source = sender as ObservableCollection<Source>;

            Mapper.Map(source, Items);
        }

现在,当我在用户界面中将某些内容添加到数据库时,我的集合会自动刷新。 看起来像automapper没有把引用放到Items中,但是创建了新的实例。

相关问题