26
2017
04

3天学习完AngularJS基础内容小结

简介:AngularJS 是一个 JavaScript 框架。它是一个以 JavaScript 编写的库。

一、AngularJS大致功能模块

19
2017
01

动态醒目提示插件jquery.pulsate的自定义改造

1、页面引用 基础jquery库jquery.js

2、直接在jquery.pulsate.min.js文件中追加自定义内容,页面引用jquery.pulsate.min.js文件。

后面的三个方法是加的,前面的经过压缩的是插件原来的内容。

06
2017
01

单例模式的优化-双重锁定

三种单例模式写法:

    public class Singleton
    {
        private static Singleton instance;
        private static readonly object syncRoot = new object();
        private Singleton() { }
        public static Singleton GetInstance()
        {
            if (instance == null)//Double-Check Locking 双重锁定,比直接lock性能更好,最完美的模式(懒汉式单例)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
    public class Singleton1
    {
        private static Singleton1 instance;
        private static readonly object syncRoot = new object();
        private Singleton1() { }
        public static Singleton1 GetInstance()
        {
            lock (syncRoot)//每次调用GetInstance都需要lock,影响性能(懒汉式单例)
            {
                if (instance == null)
                {
                    instance = new Singleton1();
                }
            }
            return instance;
        }
    }
    public class Singleton2
    {
        private static Singleton2 instance=new Singleton2();//静态初始化方式(饿汉式单例),类一加载就实例化对象,一般这样用也没问题,缺点:提前占用系统资源
        private Singleton2() { }
        public static Singleton2 GetInstance()
        {
            return instance;
        }
    }

27
2016
12

.Net函数Math.Round你会用吗?

一直以为Math.Round就是四舍五入,谁知道没加参数,得到的结果就是有问题


测试代码:

void Main()
{
    string.Format("Round  {0} = {1}",2.4M,Math.Round(2.4M,0)).Dump();
    string.Format("Round  {0} = {1}",2.5M,Math.Round(2.5M,0)).Dump();
    string.Format("Round  {0} = {1}",2.6M,Math.Round(2.6M,0)).Dump();
    string.Format("Round  {0} = {1}",3.4M,Math.Round(3.4M,0)).Dump();
    string.Format("Round  {0} = {1}",3.5M,Math.Round(3.5M,0)).Dump();
    string.Format("Round  {0} = {1}",3.6M,Math.Round(3.6M,0)).Dump();
}

输出内容:

05
2016
12

JS遍历类、json对象属性、值,方便统一赋值

js遍历类、json对象属性、值, 
我们在页面,经常会接收后面或json返回的数据,要一个一个赋值,遍历json属性,可以方便给界面的控件赋值。

25
2016
11

.Net中偶尔需要使用异步的处理

我们知道程序中使用异步、多线程会提高程序的响应速度,但也不能无限使用多线程,这在高峰会造成系统cpu上升,系统卡顿,这就需要我们自己来控制开启的线程数,不多说看代码。

        private static int threadCountByOrderId = 0;
        private static int maxThreadCountByOrderId = 30;
        public bool dealorder(int OrderId)
        {
            var threadNumber = Interlocked.Exchange(ref threadCountByOrderId, threadCountByOrderId);
            if (threadCountByOrderId > maxThreadCountByOrderId)
            {
                return OrderChangePushToBigDataImpService.PushOrderToBigData(OrderId, Logger);
            }
            else
            {
                Task.Factory.StartNew(() =>
                {
                    Interlocked.Increment(ref threadCountByOrderId);
                    try
                    {
                        using (var context = MefInjectionProvider.CreateContext())
                        {
                            var NewOrderChangePushToBigDataImpService = context.Value.GetExport<IOrderChangePushToBigDataImpService>();
                            NewOrderChangePushToBigDataImpService.PushOrderToBigData(OrderId, Logger);
                        }
                    }
                    finally
                    {
                        Interlocked.Decrement(ref threadCountByOrderId);
                    }
                });
                return true;
            }
        }

这样就能控制线程数据了

11
2016
10

.Net中自动生成Model字段修改日志内容

直接上代码,传入新旧两个Model类


字段说明,要加display标签: 

代码如下:

    public static class EntityExtension
    {
        public static HashSet<Type> PrimitiveTypes = null;
        static EntityExtension()
        {
            PrimitiveTypes = new HashSet<Type>()
                {
                    typeof(String),
                    typeof(Byte[]),
                    typeof(Byte),
                    typeof(Int16),
                    typeof(Int32),
                    typeof(Int64),
                    typeof(Single),
                    typeof(Double),
                    typeof(Decimal),
                    typeof(DateTime),
                    typeof(Guid),
                    typeof(Boolean),
                    typeof(TimeSpan),
                    typeof(Byte?),
                    typeof(Int16?),
                    typeof(Int32?),
                    typeof(Int64?),
                    typeof(Single?),
                    typeof(Double?),
                    typeof(Decimal?),
                    typeof(DateTime?),
                    typeof(Guid?),
                    typeof(Boolean?),
                    typeof(TimeSpan?)
                };
        }
        public static string GetChangedFields<T>(this T newEntity, T oldEntity) where T : class
        {
            StringBuilder updatedFields = new StringBuilder();
            Type entityType = typeof(T);
            PropertyInfo[] properties = entityType.GetProperties().Where(o => o.CanWrite && PrimitiveTypes.Contains(o.PropertyType) && !o.GetCustomAttributes(false).OfType<NotMappedAttribute>().Any()).ToArray();
            foreach (var p in properties)
            {
                if (p.Name == "ModifiedDate" || p.Name == "ModifiedByName" || p.Name == "ModifiedById") continue;
                object oldValue = p.GetValue(oldEntity, null);
                object newValue = p.GetValue(newEntity, null);
                if ((oldValue == null && newValue == null))
                {
                    continue;
                }
                else if (oldValue == null && newValue != null || oldValue != null && newValue == null || !Eq(p.PropertyType, oldValue, newValue))
                {
                    string fieldName;
                    var display = p.GetCustomAttribute<DisplayAttribute>(false);
                    fieldName = display != null ? display.Name : p.Name;
                    updatedFields.AppendFormat("{0}:{1}->{2}; ", fieldName, oldValue ?? "NULL", newValue ?? "NULL");
                }
            }
            return updatedFields.ToString();
        }
        private static bool Eq(Type propertyType, object oldValue, object newValue)
        {
            if (propertyType == typeof(Decimal) || propertyType == typeof(Decimal?))
            {
                return decimal.Parse(oldValue.ToString()) == decimal.Parse(newValue.ToString());
            }
            else
            {
                return string.Equals(oldValue.ToString(), newValue.ToString());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity"></param>
        /// <param name="oldEntity"></param>
        /// <returns></returns>
        public static List<ValueModifiedModel> GetChangedFieldsExtent<T>(this T newEntity, T oldEntity) where T : class
        {
            List<ValueModifiedModel> valueModifiedList = new List<ValueModifiedModel>();
            Type entityType = typeof(T);
            PropertyInfo[] properties = entityType.GetProperties().Where(o => o.CanWrite && PrimitiveTypes.Contains(o.PropertyType) && !o.GetCustomAttributes(false).OfType<NotMappedAttribute>().Any()).ToArray();
            foreach (var p in properties)
            {
                if (ignoreColumnList.Contains(p.Name))
                    continue;
                object oldValue = p.GetValue(oldEntity, null);
                object newValue = p.GetValue(newEntity, null);
                if ((oldValue == null && newValue == null))
                {
                    continue;
                }
                else if (oldValue == null && newValue != null || oldValue != null && newValue == null || !Eq(p.PropertyType, oldValue, newValue))
                {
                    valueModifiedList.Add(new ValueModifiedModel()
                    {
                        ColumnName = p.Name,
                        ColumnChineseName = p.GetPropertyDisplayName(),
                        ChangeBefore = oldValue ?? "NULL",
                        ChangeAfter = newValue ?? "NULL"
                    }
                    );
                }
            }
            return valueModifiedList;
        }
        /// <summary>
        /// /
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pTargetObjSrc"></param>
        /// <param name="pTargetObjDest"></param>
        public static void EntityToEntity<T>(T pTargetObjSrc, T pTargetObjDest) where T : class
        {
            try
            {
                foreach (var mItem in typeof(T).GetProperties())
                {
                    mItem.SetValue(pTargetObjDest, mItem.GetValue(pTargetObjSrc, new object[] { }), null);
                }
            }
            catch (NullReferenceException NullEx)
            {
                throw NullEx;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        /// <summary>
        /// 忽略的列名列表
        /// </summary>
        public static List<string> ignoreColumnList = new List<string>()
        {
        };
    }


31
2016
08

.Net框架搭建之辅助模版代码生成工具


在.Net框架搭建讲解中,提到了代码自动生成来减少开发人员人工作量,比如数据库对应的Model、最基层的Repository类和接口等等,除了类名不一样,其他都一样,没必要再手写一遍。 

我在这里,介绍使用CodeSmith模版生成Model类,其他的都一样,拿到表名和各字段名、字段类型等等,想怎么生成就能怎么生成。


首先,在硬盘中,建立一个文件夹,用来放模版文件,比如: 

F:\pukuimin\InjectExample\ExampleCodeSmithTemplate 

30
2016
08

.Net框架搭建之2、SQL Server MEF依赖注入 MVC Repository框架_part3

UnitOfWorkContextBase.cs

30
2016
08

.Net框架搭建之2、SQL Server MEF依赖注入 MVC Repository框架_part2


EFRepositoryBase.cs