Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context">The Ninject <see cref="IContext"/> instance.</param>
        /// <returns>The requested <see cref="IRestPortal{DataTransferValue}"/> implementation.</returns>
        private static IRestPortal <DataTransferObject> PortalFactory(IContext context)
        {
            if (context.Request.Parameters.Count == 0)
            {
                new ArgumentException("Parameter is missing.");
            }

            object parameter = (context.Request.Parameters.ToList()[0].GetValue(context, null));

            if (default(object) == parameter)
            {
                throw new ArgumentException("The generic type parameter is missing.");
            }

            string resource = parameter as string;

            if (default(string) == resource)
            {
                throw new ArgumentException("The resource parameter is NULL.");
            }

            Type constructedType;

            if (!ConfigurationModule.PORTAL_TYPES.TryGetValue(resource, out constructedType))
            {
                Type resourceType =
                    context
                    .Kernel.GetBindings(typeof(DataTransferObject))
                    .Where(b => b.Metadata.Get <string>("resource") == resource.ToLowerInvariant())
                    .Single()
                    .GetProvider(context)
                    .Type;

                Type   portalType = typeof(IRestPortal <>);
                Type[] typeArgs   = { resourceType };
                constructedType = ConfigurationModule.PORTAL_TYPES[resource] = portalType.MakeGenericType(typeArgs);
            }

            IRestPortal <DataTransferObject> portal = null;

            if (constructedType != null)
            {
                portal = (IRestPortal <DataTransferObject>)context.Kernel.Get(constructedType);
            }

            return(portal);
        }
		public async Task<JsonResult> GetSet(string resource)
		{
			ServiceResponse response = default(ServiceResponse);

			using (Session session = ApplicationModel.Current.CreateSession(new SecurityToken(this.HttpContext.Request.Url.Host, this.HttpContext.User.Identity.Name)))
			{
				try
				{
					IDictionary<string, string> parameters = this.GetParameters();

					IRestPortal<DataTransferObject> portal = DependencyInjection.Get<IRestPortal<DataTransferObject>>(InjectionParameter.Create("resource", resource));
					IEnumerable<DataTransferObject> set = portal.Get(parameters);

					ResponseStatus status = ResponseStatus.OK;
					response = new ServiceDataResponse(RestVersion0100Controller.API_VERSION, status, set);
				}
				catch (Exception ex)
				{
					response = new ServiceErrorResponse(RestVersion0100Controller.API_VERSION, ResponseStatus.ERROR, new ServiceError(ex));
				}
			}

			return this.Json(response, JsonRequestBehavior.AllowGet);
		}
		public async Task<JsonResult> Get(string resource, Guid identifier)
		{
			ServiceResponse response = default(ServiceResponse);

			using (Session session = ApplicationModel.Current.CreateSession(new SecurityToken(this.HttpContext.Request.Url.Host, this.HttpContext.User.Identity.Name)))
			{
				try
				{
					IRestPortal<DataTransferObject> portal = DependencyInjection.Get<IRestPortal<DataTransferObject>>(InjectionParameter.Create("resource", resource));
					DataTransferObject value = portal.Get(identifier);

					if (default(DataTransferObject) == value) HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;

					ResponseStatus status = default(DataTransferObject) != value ? ResponseStatus.OK : ResponseStatus.NO_DATA;
					response = new ServiceDataResponse(RestVersion0100Controller.API_VERSION, status, value);
				}
				catch (Exception ex)
				{
					response = new ServiceErrorResponse(RestVersion0100Controller.API_VERSION, ResponseStatus.ERROR, new ServiceError(ex));
				}
			}

			return this.Json(response, JsonRequestBehavior.AllowGet);
		}