Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置EF应用程序';s结构_C#_.net_Entity Framework_Architecture_Structure - Fatal编程技术网

C# 设置EF应用程序';s结构

C# 设置EF应用程序';s结构,c#,.net,entity-framework,architecture,structure,C#,.net,Entity Framework,Architecture,Structure,我正在使用POCOs开发一个原型EF应用程序。主要是作为对框架的介绍,我想知道一种在一个好的结构中设置应用程序的好方法。稍后,我计划将WCF纳入其中 我所做的工作如下: 1) 我创建了一个edmx文件,但将“代码生成”属性设置为“无”,并生成了我的数据库架构 2) 我创建了POCO,它们看起来都像: public class Person { public Person() { } public Person(string firstName, string

我正在使用POCOs开发一个原型EF应用程序。主要是作为对框架的介绍,我想知道一种在一个好的结构中设置应用程序的好方法。稍后,我计划将WCF纳入其中

我所做的工作如下:

1) 我创建了一个edmx文件,但将“代码生成”属性设置为“无”,并生成了我的数据库架构

2) 我创建了POCO,它们看起来都像:

public class Person
{
    public Person()
    { 
    }

    public Person(string firstName, string lastName)
    {        

        FirstName = firstName;
        LastName = lastName;
    }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
3) 我创建了一个上下文

public class PocoContext : ObjectContext, IPocoContext
{
    private IObjectSet<Person> persons;

    public PocoContext() : base("name=PocoContainer", "PocoContainer")
    {
        ContextOptions.LazyLoadingEnabled = true;
        persons= CreateObjectSet<Person>();
    }

    public IObjectSet<Person> Persons
    {
        get
        {
            return persons;
        }
    }

    public int Save()
    {
        return base.SaveChanges();
    }
}
公共类PocoContext:ObjectContext,iPocContext { 私人对象集人员; public PocoContext():base(“name=PocoContainer”,“PocoContainer”) { ContextOptions.LazyLoadingEnabled=true; persons=CreateObjectSet(); } 公共对象集人员 { 得到 { 返回人员; } } 公共整数保存() { 返回base.SaveChanges(); } } 界面如下所示:

public interface IPocoContext
{
    IObjectSet<Person> Persons { get; }

    int Save();
}
using (var context = new PocoContext())
{   
    PersonRepository prep = new PersonRepository();

    List<Person> pers = prep.GetAll();
}
公共接口ipocontext
{
IObjectSet人员{get;}
int Save();
}
4) 最后,我创建了一个存储库,实现了一个接口:

public class PersonRepository : IEntityRepository<Person>
{
    private IPocoContext context;

    public PersonRepository()
    {
        context = new PocoContext();
    }

    public PersonRepository(IPocoContext context)
    {
        this.context = context;
    }

    // other methods from IEntityRepository<T>
}

public interface IEntityRepository<T>
{   
    void Add(T entity);
    List<T> GetAll();
    T GetById(int id);
    void Delete(T entity);

}
公共类PersonRepository:IEntityRepository
{
私人语境;
公共PersonRepository()
{
上下文=新的PocoContext();
}
公共PersonRepository(IPocontext上下文)
{
this.context=上下文;
}
//IEntityRepository的其他方法
}
公共接口智能存储库
{   
无效添加(T实体);
List GetAll();
T GetById(int-id);
无效删除(T实体);
}
现在,当我开始处理这个问题时,这种设计要求我在每次需要获取或修改某些数据时都要实例化一个存储库,如下所示:

public interface IPocoContext
{
    IObjectSet<Person> Persons { get; }

    int Save();
}
using (var context = new PocoContext())
{   
    PersonRepository prep = new PersonRepository();

    List<Person> pers = prep.GetAll();
}
使用(var context=new PocoContext())
{   
PersonRepository prep=新建PersonRepository();
List pers=prep.GetAll();
}
从另一方面来说,这感觉是错误和有缺陷的,只是在派生上下文中实例化每个存储库也感觉不太好,因为可能会实例化我根本不需要的对象


关于如何使这种设计听起来更合理,有什么建议吗?我应该让它这样吗?一般来说,在执行此操作时,我应该添加或避免哪些内容?

如果使用POCO创建数据库模型,请先尝试EF代码?首先使用代码比在designer中创建EDMX模型更清楚。

我不理解这一部分:

using (var context = new PocoContext())
{   
    PersonRepository prep = new PersonRepository();

    List<Person> pers = prep.GetAll();
}
但我不会把它放在上下文中。我可能会在一些数据访问工厂中使用它,因为它应该在上下文之外,将单个工厂作为注入传递给使用多个存储库的类/方法更简单


顺便说一句,你会从使用存储库中得到什么

通过提供每请求对象上下文,使用Castle Windsor、AutoFac等任何容器使用依赖项注入。

它是什么类型的应用程序。Webservice、WPF应用程序,还有其他什么?在这种状态下,它只是一个控制台应用程序,因为它只是一个简单的原型。我问的原因是,您如何处理您的上下文受到应用程序类型的严重影响。例如,在wpf应用程序中,每个表单有一个上下文,在web应用程序中每个http请求有一个上下文,在web服务中每个方法调用有一个上下文,这是很常见的。这个练习的最终目标是了解EF和WCF,所以在我正确设置之后,我会将其重构为以WCF为中心的应用程序。是的,但是在这种情况下,我也可以选择一些关于如何设置edmx的东西。实际上,遵循这个建议,首先使用代码。首先,你没有理由和EDMX捣乱。如果你真的想看看edmx是什么样子,有一个选项可以从代码优先类生成它。我在读了你的文章后考虑了这个问题,希望你能对我将要说的话发表评论。首先,这个项目的灵感主要来自于一个教程,在今天下午的午餐中,我问了自己和这个主题的发起者一样的问题。存储库只是添加了一个额外的层,我称之为AddObject(或其他什么)。也就是说,想想看,我真的认为我不知道;我现在不能从中受益。。。