WCF与实体框架代码第一 [英] WCF with Entity Framework Code First

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

问题描述

我想在我的WCF服务之后使用EF来获取数据并将其显示给客户端。我需要以下建议:


  1. 我需要为所有的观点(例如学生,老师等)拥有相同的界面)或者我需要为每个表格(或视图)提供不同的接口和服务。


  2. 我需要在我的服务内生成数据库调用( .svc )还是其他一些架构是首选?

      public Student [] GetAllStudents()
    {
    //这里的数据库生成代码
    }


  3. 如何使用EF代码优先方法生成数据库。我知道,对于MVC应用程序,您需要在 Global.asax web.config 中设置初始化程序,但是我不知道在这种情况下怎么叫。我的模型看起来像这样:

      [DataContract] 
    public class Student
    {
    [ DataMember]
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get;组; }
    [DataMember]
    public string Type {get;组; }
    [DataMember]
    public string Subject {get;组; }
    [DataMember]
    public string描述{get;组;
    }



解决方案

你真正应该做的是将你的系统分解成更多的分层。而不是直接查询数据库的WCF调用,而是创建一个buisness logic层,将WCF调用的信息转换为EF调用需要知道的信息。这被称为 N-Tier应用程序

  public class SchoolAPI:ISchoolAPI 
{
private DataAccessLayer _dal = new DataAccessLayer();

public Student [] GetAllStudents()
{
return _dal.GetStudents(null,null);
}

public Student [] GetAllScienceStudents()
{
return _dal.GetStudents(null,DataAccessLayer.ScienceStudentType);
}
}

私有类DataAccessLayer
{
public static readonly ScienceStudentType = // ...

public Student [] GetStudents(string subject,string type)
{
using(var ctx = new SchoolContext())
{
IQueryable< Student> studentQuery = ctx.Students;

if(subject!= null)
studentQuery = studentQuery.Where(s => s.Subject == subject);

if(type!= null)
studentQuery = studentQuery.Where(s => s.Type == type);

return studentQuery.ToArray();
}
}
}

WCF调用者调用者不需要知道什么字符串 ScienceStudentType ,所有它关心的是它获得科学学生。通过从数据库中分离业务逻辑,呼叫您的服务的呼叫者不再需要知道。



对于EF,它将在第一次将框架初始化为触摸数据库,并检测到它不在那里,如果它设置为这样做。这是在 SchoolContext 的构造函数中完成的,但是对于这个答案来说,这一点太广泛了。我建议在EF上找到一个教程,并在没有WCF的简单测试环境中工作(也许一个简单的控制台应用程序,只需调用 GetStudents(),然后移入WCF环境。


I want to use EF behind my WCF service to fetch data and display it to the client. I need the following suggestions:

  1. Do I need to have the same interface for all the views (e.g. students, teachers etc.) or do I need to have a different interface and service for every table (or view)

  2. Do I need to generate the database calls within my service (.svc) or some other architecture is preferred?

    public Student[] GetAllStudents()
    {
       //database generation code here
    }
    

  3. How can I use EF code-first approach to generate database. I know that for an MVC app, you need to set the initializer in Global.asax or in web.config but I am not sure how it's called in this case. My model looks like this:

    [DataContract]
    public class Student
    {
        [DataMember]
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [DataMember]
        public string Type { get; set; }
        [DataMember]
        public string Subject { get; set; }
        [DataMember]
        public string Description { get; set; }
    }
    

解决方案

What you really should do is break up your system in to more separate layers. Instead of having a WCF call that directly queries the database, create a "buisness logic" layer that translates the information that the WCF call provides you to what the EF call needs to know. This is called a N-Tier application

public class SchoolAPI : ISchoolAPI
{
    private DataAccessLayer _dal = new DataAccessLayer();

    public Student[] GetAllStudents()
    {
        return _dal.GetStudents(null, null);
    }

    public Student[] GetAllScienceStudents()
    {
        return _dal.GetStudents(null, DataAccessLayer.ScienceStudentType);
    }
}

private class DataAccessLayer
{
    public static readonly ScienceStudentType = //...

    public Student[] GetStudents(string subject, string type)
    {
        using(var ctx = new SchoolContext())
        {
            IQueryable<Student> studentQuery = ctx.Students;

            if(subject != null)
                studentQuery = studentQuery.Where(s=>s.Subject == subject);

            if(type != null)
                studentQuery = studentQuery.Where(s=>s.Type == type);

            return studentQuery.ToArray();
        }
    }
}

The caller of the WCF call does not need to know what the string ScienceStudentType is, all it cares about is that it gets the science students. By seperating the business logic from the database call the caller of your service no longer needs to know.

For EF it will initialize on the first time the framework goes out to "touch" the database and detects that it is not there if it is set up to do so. This is done in the constructor of SchoolContext but is getting a little too broad for this answer. I recommend finding a tutorial on EF and get it working in a simple test enviorment without WCF (maybe a simple console app that just calls GetStudents() then move in in to a WCF environment.

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

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