posts - 930,  comments - 588,  views - 402万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

    假设您已经了解Entity framework, 在建立了Entity Data Model 后,我们创建WCF Data Services,类似这样的C#代码:

   1:      public class WcfDataService1 : DataService<Entities>
   2:      {
   3:          public static void InitializeService(DataServiceConfiguration config)
   4:          {
   5:              config.SetEntitySetAccessRule("Products", EntitySetRights.AllRead);
   6:   
   7:              config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
   8:          }
   9:      }

    Entites是我们的DataContext,注意到第5行代码使用字符串配置一个Entity的名称。我们在程序中如果写错了这个字符串,将带很多不必要的错误。
这就是HardCoding的弊端。

    让我们创建一个扩展方法来实现强类型的参数。

 ModelWCFDataService

  这个Class是这样的:

 public static class DataServiceConfigurationExtensions
    {
        public static void SetEntitySetAccessRule<DataSource>(this DataServiceConfiguration config,
           Expression<Func<DataSource, object>> expression, EntitySetRights rights) where DataSource : class
        {
            string entitySetName = GetEntitySetName(expression);
            config.SetEntitySetAccessRule(entitySetName, rights);
        }
 
        public static void SetEntitySetPageSize<DataSource>(this DataServiceConfiguration config,
          Expression<Func<DataSource, object>> expression,
          int pageSize) where DataSource : class
        {
            string entitySetName = GetEntitySetName(expression);
            config.SetEntitySetPageSize(entitySetName, pageSize);
        }
 
        private static string GetEntitySetName<DataSource>(Expression<Func<DataSource, object>> expression)
        {
            MemberExpression memberExpression = expression.Body as MemberExpression;
            if (memberExpression == null)
            {
                throw new ArgumentException("Must be a member expression");
            }
            return memberExpression.Member.Name;
        }
 
    }

    好的,接下来我们来重写上面那段代码:

   1:      public class WcfDataService1 : DataService<Entities>
   2:      {
   3:          public static void InitializeService(DataServiceConfiguration config)
   4:          {
   5:              config.SetEntitySetAccessRule<Entities>(e => e.Products, EntitySetRights.All);
   6:              config.SetEntitySetPageSize<Entities>(e => e.Products, 10);
   7:   
   8:              config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
   9:          }
  10:      }

    这样对那些Model中的实体就不容易写错了。
    希望这篇POST对您有帮助。


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

posted on   PetterLiu  阅读(823)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示