简体   繁体   中英

WPF MVVM Entity Framework Layering

i have this architecture:

------(WPF APPLICATION)(XAML,ViewModels) (Knows Business Logic But not DAL)

------(Class Library)(Business Logic) (Knows DAL)

------(Class Library)(DAL - ENTITY FRAMEWORK (Model First))(DOES NOT REFERENCE ANYONE)

My Problem here is that my DAL is NOT AWARE of any business logic classes so in my DAL im returning IEnumerable if I get a List of say for example a Person. eg:

public static IEnumerable GetPersons()
  {
       using(StaffEntities context = new StaffEntities())
       {
            return context.Persons.ToList();
       }

  }

so when i get the result from by Business Logic Layer I have a class counterpart for every entities say for Person Entity in DAL, I Have clsPerson in Business Logic Layer. but my viewmodel is NOT AWARE of DAL its only aware of Business Logic Classes hence clsPerson so my code in my business logic becomes

eg:

 public static IEnumerable GetclsPersons()
{ return DAL.GetPersons(); }

My BIG Problem is that every time I get a List of Something, Save or delete Something I have to use Reflection in my ViewModel

so if I have a clsPerson Property Bound to my xaml:

public IEnumerable clsPersons { get; set; }
public ListCollectionView clsPersonList { get; set; }

clsPersons = BLL.GetclsPersons();
clsPersonList = new ListCollectionView((IList)clsPersons);

public clsPerson CurrentclsPerson { get; set; }

everytime I assign value to "CurrentclsPerson" i have to use reflection

CurrentclsPerson.Firstname = clsPersonList.CurrentItem.GetType().GetProperty("Firstname")
.GetValue(clsPersonList.CurrentItem,null).ToString();

I was hoping not to use reflection in my viewmodels I was thinking of Putting my business Logic and DAL in one Class Library so that i dont have to use IEnumerable

what do you guys use?

Is there anyway to avoid this?

Is there anyway around this?

Please Help.. Thank You.

One solution is to put your model objects in one dll and reference that everywhere. This is easy and good enough if you keep all logic out of your model objects.

On the other hand you might want to use binding in your GUI so you need the objects to implement some interfaces (like INotifyPropertyChanged). You can still have two separate entity branches and use mapping to convert between them. Automapper is one tool that can help you with this.

I'm using T4 templates to add ToDto and ToEntity methods to my classes for converting between them.

I would do the following in your business classes:

public static IEnumerable GetPersons()
{
   using(StaffEntities context = new StaffEntities())
   {
        return from person in context.Persons.ToList()
               select new Person()
               {
                   Firstname = person.Firstname
               }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM