• IoC, DI,Spring.net


    IoC : Inversion of Control , 控制反转,就是创建对象(实例)的权利由开发人员自己控制New转到了由容器来控制。实现了解耦。

    DI: Dependency Injection, 依赖注入,就是通过容器来创建对象的时候,在对象初始化时可以给一些属性、构造方法的参数等注入默认值

     

    Aop: 面向切面的编程,相当于MVC里面的过滤器

    Unity:   .net平台下的开源项目,用来实现IoC和DI

    Spring.net:  开源项目, IoC、DI是此项目的两个重要的特点, 是从java中的spring移植过来的。使用时,需要参考Spring.net 文档

     参考

     

     

     属性设置

     代码示例:

    http://git.oschina.net/rocky132/sprint.net_ioc_di

    http://git.oschina.net/rocky132/heima8_oa

     日志并发处理

     代码:

    1)模拟异常

    public ActionResult Test()
            {
                int a = 2;
                int b = 0;
                int c = a / b;
                return Content(c.ToString());
            }

    2) 自定义异常过滤器

    public class MyExceptionAttribute:HandleErrorAttribute
        {
            public static Queue<Exception> ExceptionQueue = new Queue<Exception>();
            public override void OnException(ExceptionContext filterContext)
            {
                base.OnException(filterContext);
                ExceptionQueue.Enqueue(filterContext.Exception);//将异常信息添加到队列中。
                filterContext.HttpContext.Response.Redirect("/Error.html");
            }
        }

    3) 注册过滤器

    filters.Add(new MyExceptionAttribute());

    4) 设置程序启动后 启动线程循环 从队列中取出异常 写日志

    string fileLogPath = Server.MapPath("/Log/");
                //WaitCallback
                ThreadPool.QueueUserWorkItem((a) =>
                {
                    while (true)
                    {
                        if (MyExceptionAttribute.ExceptionQueue.Count > 0)
                        {
                            Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue();//出队
                            string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                            File.AppendAllText(fileLogPath + fileName, ex.ToString(), System.Text.Encoding.Default);
                            //ILog logger = LogManager.GetLogger("errorMsg");
                            //logger.Error(ex.ToString());
                        }
                        else
                        {
                            Thread.Sleep(3000);//如果队列中没有数据,休息避免造成CPU的空转.
                        }
                    }
                }, fileLogPath);

     代码示例:

    http://git.oschina.net/rocky132/logwrite_queue

  • 相关阅读:
    [Angular 2] Share a Service Across Angular 2 Components and Modules
    [Angular 2] How To Debug An Angular 2 Application
    [Angular 2] Create Shareable Angular 2 Components
    [Angular 2] Import custom module
    [Angular 2] Understanding Pure & Impure pipe
    [Javascript] Manipulate the DOM with the classList API
    [Angular 2] Understanding OpaqueToken
    [Angular 2] Value Providers & @Inject
    [Angular 2] Understanding @Injectable
    [Angular 2] Factory Provider with dependencies
  • 原文地址:https://www.cnblogs.com/rockywood/p/6554639.html
Copyright © 2020-2023  润新知