【问题标题】:Accessing custom objects in DomainService from client从客户端访问 DomainService 中的自定义对象
【发布时间】:2012-01-16 07:26:24
【问题描述】:

我正在使用域服务从 Silverlight 客户端的数据库中获取数据。

在 DomainService1.cs 中,我添加了以下内容:

[EnableClientAccess()]
public class Product
{
    public int productID;
    public string productName;        
    public List<Part> Parts = new List<Part>(); //Part is already present in Model designer
}

在 DomainService1 类中,我添加了一个新方法来检索自定义类对象的集合:

[EnableClientAccess()]
 public class DomainService1 : LinqToEntitiesDomainService<HELPERDBNEWEntities1>
 {
     ...
        public List<Product> GetProductsList(...)
        {
            List<Product> resultProducts = new List<Product>();
            ...
            return resultProducts;
        }
 }

我正在尝试从 silverlight 客户端访问该方法:

DomainService1 ds1 = new DomainService1();
var allproductList = ds1.GetProductsList(...);
ds1.Load<SLProduct>(allproductList).Completed += new EventHandler(Load_Completed); //Not correct usage

但是调用新方法不是正确的方法。我在 DomainServices.cs 中添加新类 Product 的原因是为了进行有效的分组。使用实体框架自动生成的模型类,我无法达到同样的效果。

如何从客户端调用新方法?

【问题讨论】:

  • 不正确的方式是什么意思,这是唯一的方式。

标签: c# asp.net silverlight entity-framework


【解决方案1】:

我相信这里有一个类似的问题有答案:

Can a DomainService return a single custom type?

另外,这里是关于在域服务中添加自定义方法的整体问题的一些讨论:

http://forums.silverlight.net/t/159292.aspx/1

【讨论】:

    【解决方案2】:

    虽然我不知道您所说的“这不是调用新方法的正确方法”是什么意思,或者如果您遇到任何错误,我认为发布一些工作代码可能会有所帮助。

    我的 POCO

        public class GraphPointWithMeta
    {
        [Key]
        public Guid PK { get; set; }
        public string SeriesName { get; set; } 
        public string EntityName { get; set; }
        public double Amount { get; set; }
    
        public GraphPointWithMeta(string seriesName, string entityName, double amount)
        {
            PK = Guid.NewGuid();
            SeriesName = seriesName;
            EntityName = entityName;
            Amount = amount;
        }
    
        // Default ctor required.
        public GraphPointWithMeta()
        {
            PK = Guid.NewGuid();
        }
    }
    

    领域服务中的一个方法(EnableClientAccess 修饰类)

            public IEnumerable<GraphPointWithMeta> CallingActivityByCommercial()
        {
            List<GraphPointWithMeta> gps = new List<GraphPointWithMeta>();
            // ...
            return gps;
        }
    

    从 Silverlight 客户端调用,例如

    ctx1.Load(ctx1.CallingActivityByCommercialQuery(), CallingActivityCompleted, null);
    

    客户端回调方法

            private void CallingActivityCompleted(LoadOperation<GraphPointWithMeta> lo)
        {
            // lo.Entities is an IEnumerable<GraphPointWithMeta>            
        }
    

    【讨论】:

      【解决方案3】:

      我不确定您的 Product 类是否是实际实体。从它的定义方式来看,它似乎不是一个实体。我的回答是假设它不是一个实体。您将需要为您的产品属性应用 DataMemberAttribute,并且您不会加载产品列表 - 加载用于实体查询(服务端的 IQueryable)。您只需像这样调用它(客户端):

      void GetProductList( Action<InvokeOperation<List<Product>>> callback)
      {
          DomainService ds1 = new DomainService();
          ds1.GetProductsList(callback, null);//invoke operation call
      }
      

      域服务的(服务器端)方法需要 InvokeAttribute,看起来像这样:

      [EnableClientAccess]
      public class MyDomainService
      {
          [Invoke]
          public List<Product> GetProductList()
          {
              var list = new List<Product>();
              ...
              return list;
          }
      }
      

      下面是如何定义您的 Product 类(如果它不是实体):

      public class Product
      {
         [DataMember]
         public int productID;
         [DataMember]
         public string productName;        
         [DataMember]
         public List<Part> Parts = new List<Part>(); // you might have some trouble here.
                                 //not sure if any other attributes are needed for Parts,
                                 //since you said this is an entity; also not sure if you 
                                 //can even have a list of entities or it needs to be an
                                 //entity collection or what it needs to be.  You might
                                 //have to make two separate calls - one to get the products
                                 //and then one to get the parts.
      }
      

      就像我说的,我不确定 Product 继承自...希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-08
        • 2013-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-02
        • 1970-01-01
        • 2019-02-21
        相关资源
        最近更新 更多