Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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
Entity framework WCF数据协定中的EF导航属性_Entity Framework_Wcf - Fatal编程技术网

Entity framework WCF数据协定中的EF导航属性

Entity framework WCF数据协定中的EF导航属性,entity-framework,wcf,Entity Framework,Wcf,我试图学习一些使用WCF合同的最佳实践。我有一个POCO实体类,如下所示: public class Job : IOwnerEntity<int>, ILoggableEntity { public int JobID { get; set; } public int JobtypeCodeID { get; set; } public string JobName { get; set; } public int CustomerID { get;

我试图学习一些使用WCF合同的最佳实践。我有一个POCO实体类,如下所示:

public class Job : IOwnerEntity<int>, ILoggableEntity
{
    public int JobID { get; set; }
    public int JobtypeCodeID { get; set; }
    public string JobName { get; set; }
    public int CustomerID { get; set; }
    public int JobStatusID { get; set; }
    public DateTime CreationDate { get; set; }
    public DateTime ModifiedDate { get; set; }

    public virtual Customer Customer { get; set; }
    public virtual ICollection<JobUserAssignment> JobUsers { get; set; }

    public int OwnerEntityID
    {
        get
        {
            return JobID;
        }
        set
        {
            JobID = value;
        }
    }
}

提前感谢,


Pradeep

将服务层绑定到数据模型不是一个好的做法。您遇到的问题是,随着数据模型的发展(新列、验证等),您的服务将需要与服务的使用者重新同步

相反,分离数据模型,并在WCF层公开一组不同的对象

以下示例已将您的版本修改为:

  • 表示数据库中的表的类
  • 表示从WCF发送的数据的类
以下是这些类的代码:

// Data Layer ---------------------
// In namespace Company.Data.Models

public class Job : IOwnerEntity<int>, ILoggableEntity
{
    public int JobID { get; set; }
    public int JobtypeCodeID { get; set; }
    public string JobName { get; set; }

    /* ... others here .... */
}

// -----------------------------------

// Services Layer --------------------    
// In namespace Company.Services.Contracts
public interface IJobService 
{
   Company.Services.Contracts.Job GetJobBy(int id);
}

// In namespace Company.Services.Contracts
[DataContract]
public class Job
{
   [DataMember]
   public int ID { get; set; }

   [DataMember]
   public string Name { get; set; }
}
一如往常:视情况而定

如果您检索一份
工作
,您是否总是(或几乎总是)需要客户和该工作的任务?然后使用总是返回所有内容的方法#1

如果您偶尔只需要客户和任务(仅在10%和20%的情况下),并且如果检索这些信息非常昂贵/低效,那么我会使用方法2,仅在真正需要时加载它们

如果您始终需要这些信息,那么在一次调用中获取所有信息是首选的,让您的用户在每次需要时调用三种方法来获取所有信息是毫无意义的

但是,如果只需要少量的额外信息,仅在少数情况下-那么在每次检索
作业时执行所有工作都没有意义-在这种情况下,让用户决定何时需要额外信息(或不需要)

[ServiceContract]
interface IJobService
{
    Job GetJob(int id);//Gets onlyjob 
    Customer GetCustome(int jobid);
    JobUserAssignment[] GetUsers(int jobid);
}
// Data Layer ---------------------
// In namespace Company.Data.Models

public class Job : IOwnerEntity<int>, ILoggableEntity
{
    public int JobID { get; set; }
    public int JobtypeCodeID { get; set; }
    public string JobName { get; set; }

    /* ... others here .... */
}

// -----------------------------------

// Services Layer --------------------    
// In namespace Company.Services.Contracts
public interface IJobService 
{
   Company.Services.Contracts.Job GetJobBy(int id);
}

// In namespace Company.Services.Contracts
[DataContract]
public class Job
{
   [DataMember]
   public int ID { get; set; }

   [DataMember]
   public string Name { get; set; }
}
// In namespace Company.Services.Impl

using Company.Data.Models;
using Company.Services.Contracts;

public class WcfJobService : IJobService
{
    public Company.Services.Contracts.Job GetJobBy(int id)
    {
        EFDataContext dc = new EFDataContext();
        Company.Data.Models.Job dc = dc.Jobs.FirstOrDefault(x=> s.JobID = id);

        if(dc == null) { return null; }

        return new Company.Services.Contracts.Job { ID = dc.JobID, Name = dc.JobName };
    }
}