当前位置: 首页>>代码示例>>C#>>正文


C# IServiceRequest.MustNotBeNull方法代码示例

本文整理汇总了C#中IServiceRequest.MustNotBeNull方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceRequest.MustNotBeNull方法的具体用法?C# IServiceRequest.MustNotBeNull怎么用?C# IServiceRequest.MustNotBeNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IServiceRequest的用法示例。


在下文中一共展示了IServiceRequest.MustNotBeNull方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InsertMyObject

        public Task<IServiceResponse<int>> InsertMyObject(IServiceRequest<MyObject> obj)
        {
            obj.MustNotBeNull("obj");

            return Task.Factory.StartNew(() =>
            {
                using (var repo = new ExampleRepo())
                {
                    var toInsert = Mapper.Map<MyEntity>(obj.Argument);
                    try
                    {
                        repo.MyEntities.Insert(toInsert);
                    }
                    catch (Exception ex)
                    {
                        //yes - catching all exceptions is not good - but this is just demonstrating how you might use the exception to
                        //generate a failed response that automatically has the exception on it.

                        //IN SOME CASES, your service layer operations will bubble their exceptions out, of course - it all depends
                        //on how you want to handle it.
                        return ex.AsFailedResponse<int>();
                    }

                    return toInsert.Id.AsSuccessfulResponse();
                }
            });
        }
开发者ID:LordZoltan,项目名称:WebAPIDemos,代码行数:27,代码来源:MyObjectService.cs

示例2: GetMyObject

 public async System.Threading.Tasks.Task<IServiceResponse<MyObject>> GetMyObject(IServiceRequest<int> id)
 {
     // so we use the same validation mechanism that's used over in the Direct service implementation in
     // ../WebAPIDemos.ServiceLayer.Direct/MyObjectService.cs
     id.MustNotBeNull("id");
     //note - I'm using await here to handle the implicit casting of ApiServiceResponse<T> to IServiceResponse<T>
     return await _requestManager.Get<ApiServiceResponse<MyObject>>(string.Format("api/MyObjects/{0}", id.Argument.ToString()), id);
 }
开发者ID:LordZoltan,项目名称:WebAPIDemos,代码行数:8,代码来源:MyObjectService.cs

示例3: GetMyObject

        //note - for testability - a better implementation of this service would accept an IServiceCache abstraction
        //(name is actually irrelevant - it'd be a proprietary type for this solution),
        //and then one implementation of that would operate over the HostingEnvironment.Cache; making it
        //possible to isolate this class in a unit test with a mocked IServiceCache implementation.

        public async System.Threading.Tasks.Task<IServiceResponse<MyObject>> GetMyObject(IServiceRequest<int> id)
        {
            id.MustNotBeNull("id");
            string cacheKey = MyObjectCacheKey(id.Argument);
            //do caching 
            MyObject cached = (MyObject)HostingEnvironment.Cache[cacheKey];
            if (cached != null)
                return cached.AsSuccessfulResponse();

            //try and retrieve the object from the inner service.  The logic here is if we get a successful response, then 
            //we will cache it.  Otherwise we will simply return the response.  So, crucially, failed lookups are never cached in
            //this implementation - which, in practise, might not be desirable.
            var response = await _inner.GetMyObject(id);

            if (response.Success)
                HostingEnvironment.Cache.Insert(cacheKey, response.Result, null, DateTime.UtcNow.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);

            return response;
        }
开发者ID:LordZoltan,项目名称:WebAPIDemos,代码行数:24,代码来源:MyCachingObjectService.cs

示例4: QueryMyObjects

        public async System.Threading.Tasks.Task<IServiceResponse<PagedResult<MyObject>>> QueryMyObjects(IServiceRequest<PagedQuery> query)
        {
            query.MustNotBeNull("query");
            
            //this method of query strings from object content can be refactored.  However, it will have to be a more complex solution
            //that is completely generic and handles nested objects in the same way that the server libraries expect to see.
            //for now, this merely demonstrates the core principle.
            Dictionary<string, string> queryKeyValues = new Dictionary<string, string>();
            //some system that relies on a similar mechanism to Model Binding should be used to convert to strings.  That uses
            //the TypeDescriptor.GetConverter mechanism, so we couuld do the same.
            queryKeyValues["Page"] = Convert.ToString(query.Argument.Page);
            queryKeyValues["PageSize"] = Convert.ToString(query.Argument.PageSize);
            string queryString = null;
            using (var tempContent = new FormUrlEncodedContent(queryKeyValues))
            {
                //why use this?  It handles the url encoding - and doesn't require System.Web (another popular
                //solution uses HttpUtility.ParseQueryString - but client code has no business referencing System.Web).
                queryString = await tempContent.ReadAsStringAsync();
            }

            return await _requestManager.Get<ApiServiceResponse<PagedResult<MyObject>>>(string.Concat("api/MyObjects?", queryString), query);
        }
开发者ID:LordZoltan,项目名称:WebAPIDemos,代码行数:22,代码来源:MyObjectService.cs


注:本文中的IServiceRequest.MustNotBeNull方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。