Search code examples
wpfentity-frameworkmvvmdata-bindingpoco

Meaning of POCO classes in MVVM and Entity Framework


I am trying to understand how WPF binding works in junction with MVVM and Entity Framework. So far I understand databinding as a concept related to properties. However, when it gets to EF I lose understanding of which objects to use to define the Model for database. For instance, I have a Model class for a Category:

 public class Category : INotifyPropertyChanged
    {
        string _CategoryId;
        public string CategoryId
        {
            get
            {
                return _CategoryId;
            }
            set
            {
                if (_CategoryId != value)
                {
                    _CategoryId = value;
                    RaisePropertyChanged("CategoryId");
                }
            }
        }

        string _CategoryName;
        public string CategoryName
        {
            get
            {
                return _CategoryName;
            }
            set
            {
                if (_CategoryName != value)
                {
                    _CategoryName = value;
                    RaisePropertyChanged("CategoryName");
                }
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="prop"></param>
        void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
        }
        public event PropertyChangedEventHandler PropertyChanged;

    }

and POCO version:

public class CategoryPoco
{
    public CategoryPoco() { }

    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
}

Properties of the non Poco class in my understanding can be then used in databinding. However, I also need to build the database context model:

public DbSet<Category> Categories { get; set; }

Now here is where I lose my understanding, do I use Poco or non-Poco classes when building the context model?

How do I match the two classes when I start exchanging data with the Database?


Solution

  • You use the "POCO version" to build the context model for your database. If you will, POCO is just defined as

    Plain Old CLR Object. Just a normal class, no attributes describing infrastructure concerns or other responsibilities that your domain objects shouldn't have.

    so technically, your Category is also considered as POCO. POCO doesn't have a different meaning when used with MVVM or EF. EF just uses those objects to map it back to the database.

    In your Category class, I normally don't create another Model class just to have that INotifyPropertyChanged. It's more flexible and clear that your Category class should be called CategoryViewModel.

    If I read your code and I see an INotifyPropertyChanged interface where WPF uses it also for DataBinding, then I would be against it as you are now using Model -> View pattern without the ViewModel as your middleman. (Assuming you use category as your binding source)

    If you decided that you need to extend Category class, then I would suggest to use T4 template to generate your POCO Classes as a partial class, and create another partial class that implements INotifyPropertyChanged or add more properties that are not in the columns of a given table e.g., CategoryStatus, CategoryDescription and mark those properties with [NotMapped] attribute.

    That way you don't have to do matching between the two classes and mostly your Model is already setup in the ViewModel to communicate it back with EF. You also have the flexibility to add more functionality to the object, which complies with the Open-Closed design principle.