使用实体框架 [英] Using Entity Framework

查看:74
本文介绍了使用实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用实体框架和它创造,我可以用它来获取所有我从中所需要的数据的上下文类。但我对我应该怎么安排我的code,通过观看演示面临的一个问题,该人士只是采用C上的控制台应用程序$ CS一切的框架和$。什么是使用实体框架的最佳方式,它看起来干净?我的意思请问这是什么...现在使用aspx页面,我可以只使用aspx.cs获取数据或保存数据。但我不想这样,我想它是更加有组织虽然实体框架创建的对象等做了几乎所有的东西..但是,我仍然需要使用之类的东西。

I just started using Entity Framework and it created a Context class which I can use to get all the data i need from it. But I am facing an issue on how I should organize my code, by watching the demos, the person just uses the framework and codes everything on a console application. What is the best way to use Entity Framework and that it looks clean?, what I mean by this is...right now using aspx pages, I could just use the aspx.cs to get the data or save the data. But I do not want this, I would like it to be more organized although the Entity Framework did almost everything by creating the objects etc.. but still, I need to use things like

using(var myobject = new MyContextData())
{
    blah blah..
}

你会说,这将是更好的编写将包装这些调用类?我真的AP preciate任何投入,因为这将真正使用实体框架,使我成为一个更好的程序员。

would you say that it would be nicer to write classes that would wrap these calls?. I would really appreciate any inputs as it would really make me a better programmer using the entity framework.

问候

推荐答案

编辑:

我认为,通用Repository是反面模式。但我不明白@TomTom评论。

I think that Generic Repository is Anti Pattern. But I do not understand @TomTom comment.

原来的答复:

由于拉迪姆·克勒提到你需要执行的工作模式库和单位
但他在我看来,所提供的文章是不完全正确。

As Radim Köhler mentioned you need to implement Repository and Unit of Work patterns But the article he provided in my opinion is not fully correct.

在我目前的工作,我用下面的实现这些模式。
例如,我们有三种类型的实体:的人,好和秩序。我创建了人员信息库。在普通情况下,库必须不能通用。它必须含有重新present特定查询本实体的方法。因此,通过查看资料库的界面,你可以告诉什么样的实体(人,例如)执行的查询。正如你将看到我创建DTO的人叫 PersonWrap 。从PersonWrap从Person创建PersonWrap和更新的人,你可以用它代替AutoMapper PersonWrap()构造和更新()方法。由于的EntityFramework 的DbContext 工具工作单位模式,你只需要提供创建的DbContext到存储库的方法。如果存储库的方法是单独行动,你并不需要的DbContext这个方法,你可以创建之外,它处理这个​​方法里面。

At my current job I use following implementation of these patterns. For example, we have three types of entities: Person, Good and Order. I created repository for Persons. In common case Repository must not be generic. It must contain methods which represent specific queries for this entity. So by looking at the interface of repository you can tell what kinds of queries executed for entity (Person, e.g.). As you will see I created DTO for Person called PersonWrap. For creating PersonWrap from Person and updating Person from PersonWrap you can use AutoMapper instead of PersonWrap() constructor and Update() method. Because EntityFramework DbContext implements Unit of Work pattern, you just need to provide created DbContext to repository methods. If repository method is a separate action and you do not need DbContext outside of this method you can create and dispose it inside this method.

public class Person {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public DateTime RegistrationDate { get; set; }
    public List<Order> Orders { get; set; } 
}

public class Good {
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public class Order {
    public int Id { get; set; }
    public Person Person { get; set; }
    public Good Good { get; set; }
    public int Count { get; set; }
}

public class MyDbContext: DbContext
{
    public IDbSet<Person> Persons { get { return Set<Person>(); }}
    public IDbSet<Good> Goods { get { return Set<Good>(); }}
    public IDbSet<Order> Orders { get { return Set<Order>(); }} 
}

public class PersonRepository {
    public IEnumerable<Person> GetAll() {
        using (var context = new MyDbContext()) {
            return context.Persons.ToList();
        }
    }

    public IEnumerable<Person> GetLastWeekPersons() {
        using (var context = new MyDbContext()) {
            return context.Persons.Where(p => p.RegistrationDate > new DateTime().AddDays(-7)).ToList();
        }
    } 

    public Person GetById(int id, MyDbContext context) {
        return context.Persons.Include(p => p.Orders).FirstOrDefault(p => p.Id == id);
    }

    public Person GetById(int id) {
        using (var context = new MyDbContext()) {
            return GetById(id, context);
        }
    }
}

public class PersonWrap {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public int OrderCount { get; set; }

    public PersonWrap(Person person) {
        Id = person.Id;
        FirstName = person.FirstName;
        SecondName = person.SecondName;
        OrderCount = person.Orders.Count;
    }

    public void Update(Person person) {
        person.FirstName = FirstName;
        person.SecondName = SecondName;
    }
}

public class PersonDetailsViewController {
    public PersonWrap Person { get; protected set; }

    public PersonDetailsViewController(int personId) {
        var person = new PersonRepository().GetById(personId);
        if (person != null) {
            Person = new PersonWrap(person);
        }
    }

    public void Save() {
        using (var context = new MyDbContext()) {
            var person = new PersonRepository().GetById(Person.Id, context);
            Person.Update(person);
            context.SaveChanges();
        }
    }
}

这篇关于使用实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆