Esempio n. 1
0
        /// <summary>
        /// Create a new business object.
        /// </summary>
        /// <param name="objectType">Type of business object to create.</param>
        /// <param name="criteria">Criteria object describing business object.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> object passed to the server.
        /// </param>
        public DataPortalResult Create(
            Type objectType, object criteria, DataPortalContext context)
        {
            try
            {
                SetContext(context);

                Authorize(new AuthorizeRequest(objectType, criteria, DataPortalOperations.Create));

                DataPortalResult result;

                DataPortalMethodInfo method = DataPortalMethodCache.GetCreateMethod(objectType, criteria);

                IDataPortalServer portal;
                switch (method.TransactionalType)
                {
                case TransactionalTypes.EnterpriseServices:
                    portal = new ServicedDataPortal();
                    try
                    {
                        result = portal.Create(objectType, criteria, context);
                    }
                    finally
                    {
                        ((ServicedDataPortal)portal).Dispose();
                    }

                    break;

                case TransactionalTypes.TransactionScope:

                    portal = new TransactionalDataPortal();
                    result = portal.Create(objectType, criteria, context);

                    break;

                default:
                    portal = new DataPortalSelector();
                    result = portal.Create(objectType, criteria, context);
                    break;
                }
                return(result);
            }
            catch (YYT.Server.DataPortalException ex)
            {
                Exception tmp = ex;
                throw;
            }
            catch (Exception ex)
            {
                throw new DataPortalException(
                          "DataPortal.Create " + Resources.FailedOnServer,
                          ex, new DataPortalResult());
            }
            finally
            {
                ClearContext(context);
            }
        }
        public static DataPortalMethodInfo GetMethodInfo(Type objectType, string methodName, params object[] parameters)
        {
            var key = new MethodCacheKey(objectType.Name, methodName, MethodCaller.GetParameterTypes(parameters));
            DataPortalMethodInfo result = null;

            if (!_cache.TryGetValue(key, out result))
            {
                lock (_cache)
                {
                    if (!_cache.TryGetValue(key, out result))
                    {
                        result = new DataPortalMethodInfo(MethodCaller.GetMethod(objectType, methodName, parameters));
                        _cache.Add(key, result);
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// Gets a reference to the DataPortal_Fetch method for
        /// the specified business object type.
        /// </summary>
        /// <param name="objectType">Type of the business object.</param>
        /// <param name="criteria">Criteria parameter value.</param>
        /// <remarks>
        /// If the criteria parameter value is an integer, that is a special
        /// flag indicating that the parameter should be considered missing
        /// (not Nothing/null - just not there).
        /// </remarks>
        internal static DataPortalMethodInfo GetFetchMethod(Type objectType, object criteria)
        {
            // an "Integer" criteria is a special flag indicating
            // that criteria is empty and should not be used
            DataPortalMethodInfo method = null;
            var factoryInfo             = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType);

            if (factoryInfo == null)
            {
                if (criteria is int)
                {
                    method = GetMethodInfo(objectType, "DataPortal_Fetch");
                }
                else
                {
                    method = GetMethodInfo(objectType, "DataPortal_Fetch", criteria);
                }
            }
            else
            {
                var factoryType = FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName);
                if (factoryType != null)
                {
                    if (criteria is int)
                    {
                        method = GetMethodInfo(
                            factoryType,
                            factoryInfo.FetchMethodName);
                    }
                    else
                    {
                        method = GetMethodInfo(
                            FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName),
                            factoryInfo.FetchMethodName,
                            criteria);
                    }
                }
                else
                {
                    method = new DataPortalMethodInfo();
                }
            }
            return(method);
        }