Exemplo n.º 1
0
        static void TestSnowflake()
        {
            Console.WriteLine(SnowflakeHelper.Instance().NextId());

            //HashSet<long> set = new HashSet<long>();
            //SnowflakeHelper idWorker1 = new SnowflakeHelper(0, 0);
            //SnowflakeHelper idWorker2 = new SnowflakeHelper(1, 0);
            //762884413578018816
            //762884520121729024
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            //for (int i = 0; i < 1; i++)
            //{
            //long id = idWorker1.nextId();
            //    set.Add(id);
            //    if (!set.Add(id))
            //    {
            //Console.WriteLine("duplicate:" + id);
            //}
            //    }
            //sw.Stop();
            //foreach (var item in set)
            //{
            //    Console.WriteLine("结果:" + item);
            //}
            //Console.WriteLine("时间:" + sw.ElapsedTicks);
        }
Exemplo n.º 2
0
        /// <summary>
        /// DenpendencyMiddleware
        /// </summary>
        /// <param name="services"></param>
        public static IServiceCollection AddDenpendencyServices(this IServiceCollection services)
        {
            var _Assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList().FindAll(s => s.FullName.StartsWith("HongMouer."));
            var _Types      = _Assemblies.SelectMany(a => a.DefinedTypes).Select(type => type.AsType())
                              .Where(x => x != typeof(IDenpendency) && typeof(IDenpendency).IsAssignableFrom(x)).ToArray();
            var _Context        = services.BuildServiceProvider().GetService <IRepository>();
            var _Plugin         = _Context.FindList <SystemPlugin>().ToList();
            var _ClassTypes     = _Types.Where(x => x.IsClass).ToArray();
            var _InterfaceTypes = _Types.Where(x => x.IsInterface).ToArray();

            foreach (var item in _ClassTypes)
            {
                var _Interface = _InterfaceTypes.FirstOrDefault(x => x.IsAssignableFrom(item));
                if (_Interface != null)
                {
                    object[] objParam = new object[item.GetConstructors().Length];

                    for (int i = 0; i < item.GetConstructors().Length; i++)
                    {
                        objParam[i] = null;
                    }

                    IDenpendency denpendency = (IDenpendency)Activator.CreateInstance(item, objParam);

                    if (!_Plugin.Exists(s => s.Status == EnumStatus.正常.GetHashCode()))
                    {
                        services.AddScoped(_Interface, item);
                    }
                    else if (_Plugin.Exists(s => s.Name == denpendency.Name && s.Status == EnumStatus.正常.GetHashCode()))
                    {
                        services.AddScoped(_Interface, item);
                    }
                    if (_Plugin == null || _Plugin.Count == 0 || !_Plugin.Exists(s => s.Name == denpendency.Name))
                    {
                        _Context.Insert(new SystemPlugin
                        {
                            Id                = SnowflakeHelper.NextId(),
                            GroupName         = denpendency.GroupName,
                            Version           = denpendency.Version,
                            Name              = denpendency.Name,
                            PluginFullName    = item.FullName,
                            InterfaceFullName = _Interface.FullName,
                            Status            = EnumStatus.停用.GetHashCode(),
                            Remark            = denpendency.Remark,
                            CreateTime        = DateTime.Now,
                            ModifyTime        = DateTime.Now,
                            RowVersion        = DateTime.Now.Ticks
                        });
                    }
                }
            }

            return(services);
        }
Exemplo n.º 3
0
        public static void TestToEnum()
        {
            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine(SnowflakeHelper.BuildId <Class1>());
            }

            Console.WriteLine("------------------------");
            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine(SnowflakeHelper.BuildId <Class2>());
            }
        }
Exemplo n.º 4
0
    {  /// <summary>
       /// 新增调用
       /// </summary>
        public virtual void Create()
        {
            //属性列表
            var properties = ReflectionHelper.GetProperties(this.GetType());
            //修改的键值
            var keys = GetDefaultForCreate();

            foreach (KeyValuePair <string, PropertyInfo> pair in properties)
            {
                //跳过自增列
                var identity_attributes = pair.Value.GetCustomAttributes(typeof(IdentityAttribute), true) as IdentityAttribute[];
                if (identity_attributes != null && identity_attributes.Length > 0)
                {
                    continue;
                }

                //设置主键id
                var attributes = pair.Value.GetCustomAttributes(typeof(PrimaryKeyAttribute), true) as Attribute[];
                if (attributes != null && attributes.Length > 0)
                {
                    if (pair.Value.PropertyType == typeof(string))
                    {
                        try
                        {
                            pair.Value.SetValue(this, Guid.NewGuid().ToString());
                        }
                        catch { }
                    }
                    else if (pair.Value.PropertyType == typeof(long) || pair.Value.PropertyType == typeof(long?))
                    {
                        if (pair.Value.GetValue(this) == null)
                        {
                            pair.Value.SetValue(this, SnowflakeHelper.NewId());
                        }
                        continue;
                    }
                }
                //设置字段默认值
                if (pair.Value.GetValue(this) == null)
                {
                    object vlue;
                    if (keys.TryGetValue(pair.Key.ToLower(), out vlue))
                    {
                        pair.Value.SetValue(this, vlue);
                    }
                }
            }
        }
Exemplo n.º 5
0
 public static SnowflakeHelper GetInstance()
 {
     if (_Singleton == null)
     {
         //加锁保护,在多线程下可以确保实例值被创建一次。
         //缺点是每 次获取单例,都要进行判断,涉及到的锁和解锁比较耗资源。
         lock (obj)
         {
             if (_Singleton == null)
             {
                 _Singleton = new SnowflakeHelper();
             }
         }
     }
     return(_Singleton);
 }
Exemplo n.º 6
0
        public bool Register(string userName, string password)
        {
            var usserBaseInfo = this.UserBaseInfoDal.Get(userName);

            if (usserBaseInfo != null)
            {
                return(false);
            }
            string encryptPassword = EncryptionHelper.GetMD5Hash(password);
            string id = SnowflakeHelper.Instance().GetString();

            usserBaseInfo = new Model.UserBaseInfo()
            {
                ID             = id,
                CreateDatetime = DateTime.Now,
                Password       = encryptPassword,
                UserName       = userName
            };
            return(this.UserBaseInfoDal.Insert(usserBaseInfo));
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            byte[] inputByteArray = Encoding.UTF8.GetBytes("1升等于多少斤");

            var _Aes = AesHelper.AESEncrypt(inputByteArray, Key, IV);

            string a = Convert.ToBase64String(_Aes);

            Console.WriteLine("SnowflakeHelper.NextId:" + SnowflakeHelper.NextId());

            Console.WriteLine(a);

            string _aes = StringHelper.byteToHexStr(_Aes);

            Console.WriteLine(StringHelper.byteToHexStr(_Aes));

            byte[] by = StringHelper.HexToByte(_aes);

            Console.WriteLine(Encoding.UTF8.GetString(AesHelper.AESDecrypt(by, Key, IV)));

            Console.WriteLine(AesHelper.AESDecryptHex(_aes, Key, IV));

            //for (long i = 0; i < long.MaxValue; i++)
            //{
            //Console.WriteLine("ObjectId.NewId:" + ObjectIdInt.NewId());
            //}
            //for (long i = 0; i < long.MaxValue; i++)
            //{
            //    Console.WriteLine("ObjectId.GenerateNewStringId:" + ObjectId.NewId());
            //}

            //for (long i = 0; i < 1000; i++)
            //{
            //    Console.WriteLine("TimestampId.GetInstance1:" + TimestampId.Instance.GetId());
            //    Console.WriteLine("TimestampId.GetInstance2:" + TimestampId.Instance.GetId());
            //    Console.WriteLine("TimestampId.GetInstance3:" + TimestampId.Instance.GetId());
            //    Console.WriteLine("TimestampId.GetInstance4:" + TimestampId.Instance.GetId());
            //}
            Console.WriteLine("Hello World!");
        }
Exemplo n.º 8
0
 public void NextIdTest()
 {
     Assert.IsTrue(SnowflakeHelper.Instance().NextId() > 0);
 }