Beispiel #1
0
 /// <summary>
 /// Gets the repository name and repository type for the specified assembly.
 /// </summary>
 /// <param name="assembly">The assembly that has a <see cref="T:log4net.Config.RepositoryAttribute" />.</param>
 /// <param name="repositoryName">in/out param to hold the repository name to use for the assembly, caller should set this to the default value before calling.</param>
 /// <param name="repositoryType">in/out param to hold the type of the repository to create for the assembly, caller should set this to the default value before calling.</param>
 /// <exception cref="T:System.ArgumentNullException"><paramref name="assembly" /> is <see langword="null" />.</exception>
 private void GetInfoForAssembly(Assembly assembly, ref string repositoryName, ref Type repositoryType)
 {
     if ((object)assembly == null)
     {
         throw new ArgumentNullException("assembly");
     }
     try
     {
         LogLog.Debug(declaringType, "Assembly [" + assembly.FullName + "] Loaded From [" + SystemInfo.AssemblyLocationInfo(assembly) + "]");
     }
     catch
     {
     }
     try
     {
         object[] array = assembly.GetCustomAttributes(typeof(RepositoryAttribute)).ToArray();
         if (array == null || array.Length == 0)
         {
             LogLog.Debug(declaringType, "Assembly [" + assembly + "] does not have a RepositoryAttribute specified.");
         }
         else
         {
             if (array.Length > 1)
             {
                 LogLog.Error(declaringType, "Assembly [" + assembly + "] has multiple log4net.Config.RepositoryAttribute assembly attributes. Only using first occurrence.");
             }
             RepositoryAttribute repositoryAttribute = array[0] as RepositoryAttribute;
             if (repositoryAttribute == null)
             {
                 LogLog.Error(declaringType, "Assembly [" + assembly + "] has a RepositoryAttribute but it does not!.");
             }
             else
             {
                 if (repositoryAttribute.Name != null)
                 {
                     repositoryName = repositoryAttribute.Name;
                 }
                 if ((object)repositoryAttribute.RepositoryType != null)
                 {
                     if (CompatibilityExtensions.IsAssignableFrom(typeof(ILoggerRepository), repositoryAttribute.RepositoryType))
                     {
                         repositoryType = repositoryAttribute.RepositoryType;
                     }
                     else
                     {
                         LogLog.Error(declaringType, "DefaultRepositorySelector: Repository Type [" + repositoryAttribute.RepositoryType + "] must implement the ILoggerRepository interface.");
                     }
                 }
             }
         }
     }
     catch (Exception exception)
     {
         LogLog.Error(declaringType, "Unhandled exception in GetInfoForAssembly", exception);
     }
 }
Beispiel #2
0
        private static object HasMany(this IActiveRecord wrapper, FieldAttribute relationshipAttribute)
        {
            if (!(relationshipAttribute is RelationshipAttribute))
            {
                throw new ActiveRecordAttributeException(wrapper.GetType(),
                                                         "O atributo não é do tipo HasMany. Não foi possível retornar os objetos relacionados.");
            }

            HasManyAttribute hasMany = relationshipAttribute as HasManyAttribute;

            object[] customAttributes = hasMany.ClassType.GetCustomAttributes(typeof(RepositoryAttribute), true);
            if (customAttributes.Length != 1)
            {
                throw new ActiveRecordAttributeException(hasMany.ClassType, "Não foi possível encontrar o controlador da classe.");
            }

            RepositoryAttribute controlled = (RepositoryAttribute)customAttributes[0];
            object relatedController       = Activator.CreateInstance(controlled.ControllerType);

            if (relatedController == null)
            {
                throw new ActiveRecordAttributeException(controlled.ControllerType, "Não foi possível instanciar o controller.");
            }

            PrimaryFieldAttribute pkAtt = wrapper.GetPrimaryKeyDefinition();

            if (pkAtt == null)
            {
                throw new ActiveRecordAttributeException(wrapper.GetType(), "Não foi possível encontrar a chave primária da classe.");
            }

            IQueryFilter filter = new QueryFilterClass();

            filter.WhereClause = String.Format(pkAtt.QuoteValue ? "{0} = '{1}'" : "{0} = {1}", hasMany.FieldName, wrapper.UnderlyingObject.get_Value(pkAtt.Index));

            if (!String.IsNullOrEmpty(hasMany.OrderBy))
            {
                IQueryFilterDefinition definition = filter as IQueryFilterDefinition;
                definition.PostfixClause = hasMany.OrderBy;
            }

            // prepare the method for invoke
            // check if the method is lazy or not
            MethodInfo filterMethod = null;

            filterMethod = controlled.ControllerType.GetMethod(hasMany.Lazy ? "FilterLazy" : "Filter");

            var parameters = new object[1];

            parameters[0] = filter;

            // invoke and return
            return(filterMethod.Invoke(relatedController, parameters));
        }
Beispiel #3
0
        /// <summary>
        /// Gets the Owner of a specific IActiveRecord
        /// </summary>
        /// <param name="wrapper">IActiveRecord</param>
        /// <param name="propertyName">string</param>
        /// <returns>object</returns>
        private static object Belongs(this IActiveRecord wrapper, FieldAttribute relationshipAttribute)
        {
            if (!(relationshipAttribute is RelationshipAttribute))
            {
                throw new ActiveRecordAttributeException(wrapper.GetType(),
                                                         "O atributo não é do tipo HasMany. Não foi possível retornar os objetos relacionados.");
            }

            var attribute = relationshipAttribute as BelongsToAttribute;

            object[] customAttributes = attribute.ClassType.GetCustomAttributes(typeof(RepositoryAttribute), true);
            if (customAttributes.Length != 1)
            {
                throw new ActiveRecordAttributeException(attribute.ClassType, "Não foi possível encontrar o controlador da classe.");
            }

            RepositoryAttribute controlled = (RepositoryAttribute)customAttributes[0];
            object relatedController       = Activator.CreateInstance(controlled.ControllerType);

            if (relatedController == null)
            {
                throw new ActiveRecordAttributeException(controlled.ControllerType, "Não foi possível instanciar o controller.");
            }

            IQueryFilter filter = new QueryFilterClass();

            filter.WhereClause = String.Format(attribute.QuoteValue ? "{0} = '{1}'" : "{0} = {1}", attribute.FieldName, wrapper.UnderlyingObject.get_Value(attribute.ParentValueFieldIndex));

            // prepare the method for invoke
            // check if the method is lazy or not
            MethodInfo singleMethod = null;

            singleMethod = controlled.ControllerType.GetMethod("SingleByPrimaryKey");

            var parameters = new object[1];

            parameters[0] = wrapper.UnderlyingObject.get_Value(attribute.ParentValueFieldIndex);
            //case not exist value in foreign key, return null
            if (parameters[0] == null || parameters[0].ToString().Equals(""))
            {
                return(null);
            }

            // invoke and return
            return(singleMethod.Invoke(relatedController, parameters));
        }
        public string GetName <T>()
        {
            RepositoryAttribute attribute = typeof(T).GetCustomAttributes(typeof(RepositoryAttribute), false).FirstOrDefault() as RepositoryAttribute;

            if (attribute == null)
            {
                return(typeof(T).Name);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(attribute.Name) || attribute.Name.Length > 20)
                {
                    throw new RepositoryAttributeIsInvalidException();
                }
                else
                {
                    return(attribute.Name);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// This method is responsible for fetching lazy IObjects;
        /// </summary>
        /// <exception cref="ActiveRecordAttributeException"></exception>
        /// <exception cref="ArgumentException"></exception>
        private static void FetchUnderlyingObject(this IActiveRecord wrapper)
        {
            // we can only fetch this object if it's a proxy
            if (!wrapper.IsProxy && wrapper.UnderlyingObject != null)
            {
                return;
            }

            if (wrapper.ObjectId == 0)
            {
                return;
            }

            // find out which controller is responsible for this type of object
            RepositoryAttribute controlledAtt = null;
            var customAtts = wrapper.GetType().GetCustomAttributes(typeof(RepositoryAttribute), true);

            // test if out attribute is configured properly
            if (customAtts.Length == 1)
            {
                controlledAtt = (RepositoryAttribute)customAtts[0];
            }

            if (controlledAtt == null)
            {
                throw new ActiveRecordAttributeException(wrapper.GetType(), "Não foi possível localizar o atributo controller.");
            }

            var controllerType = controlledAtt.ControllerType;
            var controller     = Activator.CreateInstance(controllerType);

            // lets get the method we need and set our parameters
            var singleMethod = controllerType.GetMethod("SingleByOID");
            var parameters   = new object[1];

            parameters[0] = wrapper.ObjectId;

            // finally invoke the method and set our underlyingObject.
            wrapper.UnderlyingObject =
                ((IActiveRecord)singleMethod.Invoke(controller, parameters)).UnderlyingObject;
        }
 public void Constructor_StateSet_Again()
 {
     RepositoryAttribute att = new RepositoryAttribute(DataLayerState.Testing, typeof(int));
     Assert.AreEqual(DataLayerState.Testing, att.State, "The State Property of the attribute was not set correctly.");
 }
 public void Constructor_RepositoryTypeSet_Again()
 {
     RepositoryAttribute att = new RepositoryAttribute(DataLayerState.Live, typeof(int));
     Assert.AreEqual(typeof(int), att.RepositoryType, "The RepositoryType property of the attribute was not set correctly.");
 }
        /// <summary>
        /// Gets the repository name and repository type for the specified assembly.
        /// </summary>
        /// <param name="assembly">The assembly that has a <see cref="RepositoryAttribute"/>.</param>
        /// <param name="repositoryName">in/out param to hold the repository name to use for the assembly, caller should set this to the default value before calling.</param>
        /// <param name="repositoryType">in/out param to hold the type of the repository to create for the assembly, caller should set this to the default value before calling.</param>
        /// <exception cref="ArgumentNullException"><paramref name="assembly" /> is <see langword="null" />.</exception>
        private void GetInfoForAssembly(Assembly assembly, ref string repositoryName, ref Type repositoryType)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            try
            {
                LogLog.Debug(declaringType, "Assembly [" + assembly.FullName + "] Loaded From [" + SystemInfo.AssemblyLocationInfo(assembly) + "]");
            }
            catch
            {
                // Ignore exception from debug call
            }

            try
            {
                // Look for the RepositoryAttribute on the assembly
                object[] repositoryAttributes = Attribute.GetCustomAttributes(assembly, typeof(RepositoryAttribute), false);
                if (repositoryAttributes == null || repositoryAttributes.Length == 0)
                {
                    // This is not a problem, but its nice to know what is going on.
                    LogLog.Debug(declaringType, "Assembly [" + assembly + "] does not have a RepositoryAttribute specified.");
                }
                else
                {
                    if (repositoryAttributes.Length > 1)
                    {
                        LogLog.Error(declaringType, "Assembly [" + assembly + "] has multiple RepositoryAttribute assembly attributes. Only using first occurrence.");
                    }

                    RepositoryAttribute domAttr = repositoryAttributes[0] as RepositoryAttribute;

                    if (domAttr == null)
                    {
                        LogLog.Error(declaringType, "Assembly [" + assembly + "] has a RepositoryAttribute but it does not!.");
                    }
                    else
                    {
                        // If the Name property is set then override the default
                        if (domAttr.Name != null)
                        {
                            repositoryName = domAttr.Name;
                        }

                        // If the RepositoryType property is set then override the default
                        if (domAttr.RepositoryType != null)
                        {
                            // Check that the type is a repository
                            if (typeof(ILoggerRepository).IsAssignableFrom(domAttr.RepositoryType))
                            {
                                repositoryType = domAttr.RepositoryType;
                            }
                            else
                            {
                                LogLog.Error(declaringType, "DefaultRepositorySelector: Repository Type [" + domAttr.RepositoryType + "] must implement the ILoggerRepository interface.");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogLog.Error(declaringType, "Unhandled exception in GetInfoForAssembly", ex);
            }
        }