Entity Framework 4 and WPF

后端 未结 3 2044
我寻月下人不归
我寻月下人不归 2021-02-02 01:54

I am writing a WPF application, using an MVVM design with Entity Framework 4 as the ORM. I have collection properties in my view model that will contain collections of entities

3条回答
  •  梦毁少年i
    2021-02-02 02:19

    I would probably use a factory pattern to achieve a level of abstraction in your viewModel if you want it.

    The downside is that you will have to limit yourself to calling the factory every time you create one of your collections.

    so if your factory had an API like this(which you could switch with whatever you wanted):

    public static class ObjectBuilder
    {
      static Factory;
      SetFactory(IFactory factory) { Factory = factory; };
      T CreateObject() { return factory.Create();};
      TCollection CreateObject>()
      {
        return Factory.Create();
      }      
      TCollection CreateObject>(TCollection items)
      {
        return Factory.Create(TCollection items);
      }
    }
    

    so now you would:

    • just implement IFactory to return your EdmObservableCollection whenever TCollection is ObservableCollection
    • whenever you app initializes call ObjectBuilder.SetFactory()
    • and now in your viewmodels wherever you want this you simply call ObjectBuilder.Create();

    also if/whenever you would need to change your ORM backend you simply implement a new IFactory and call ObjectBuilder.SetFactory(factory)

提交回复
热议问题