Ejemplo n.º 1
0
        /// <summary>
        /// 扫描Business特性的类,并创建实例。
        /// </summary>
        /// <param name="dlls">指定的要被扫描的dll。</param>
        private void ScanBusinessAttribute(string[] dlls)
        {
            foreach (string dll in dlls)
            {
                Assembly assembly = Assembly.LoadFile(dll);
                Type[]   types    = assembly.GetTypes();
                foreach (Type clazz in types)
                {
                    if (clazz.IsClass)
                    {
                        //1.扫描类的Business特性
                        //获取属性
                        Attribute attributeBusinessAttribute = clazz.GetCustomAttribute(typeof(BusinessAttribute));
                        if (attributeBusinessAttribute == null)//如果该类没有Business特性
                        {
                            continue;
                        }
                        BusinessAttribute businessAttribute = (BusinessAttribute)attributeBusinessAttribute;
                        string            bussinessName     = clazz.Name.Substring(0, 1).ToLower() + (clazz.Name.Length > 1 ? clazz.Name.Substring(1) : string.Empty);
                        if (!string.IsNullOrWhiteSpace(businessAttribute.Name))//如果设置了BusinessAttribute的Name值
                        {
                            bussinessName = businessAttribute.Name;
                        }
                        if (BeanContainer.ContainsKey(bussinessName))
                        {
                            throw new BeanCreationException(string.Format("不能创建‘{0}’,已经存在该实例。", bussinessName));
                        }

                        //添加实例到容器中
                        object clazzInstance = Activator.CreateInstance(clazz);//创建类的实例
                        BeanContainer.Add(bussinessName, new BeanInfo()
                        {
                            Bean = clazzInstance, AtrributeType = AtrributeType.Business
                        });
                    } //end : if (clazz.IsClass)
                }     //end : foreach (Type clazz in types)
            }         //end : foreach (string dll in dlls)
        }
Ejemplo n.º 2
0
        public void BusinessAttribute(BusinessAttribute reg)
        {
            TimeStamp stamp = new TimeStamp();

            reg.Day            = stamp.Day;
            reg.Hour           = stamp.Hour;
            reg.Minute         = stamp.Minute;
            reg.Month          = stamp.Month;
            reg.Quarter        = stamp.Quarter;
            reg.Year           = stamp.Year;
            reg.Week           = stamp.Week;
            reg.Timestamp      = stamp.Stamp;
            reg.Session        = APIContext.Current.Session;
            reg.WidgetAPIKeyId = APIContext.Current.WidgetToken != null ? APIContext.Current.WidgetToken.APIKeyId : (long?)null;

            Task.Factory.StartNew(() =>
            {
                using (var context = ContextFactory.AnalyticsContext)
                {
                    context.BusinessAttributes.AddObject(reg);
                    context.SaveChanges();
                }
            });
        }