当前位置: 移动技术网 > IT编程>开发语言>.net > 扩展ASP.NET MVC三层框架且使用StructureMap实现依赖注入1-Model层

扩展ASP.NET MVC三层框架且使用StructureMap实现依赖注入1-Model层

2017年12月12日  | 移动技术网IT编程  | 我要评论
本篇文章将向大家介绍如何添加service和repository层并且使用structuremap把service层注入到controller,把repository注入到

本篇文章将向大家介绍如何添加service和repository层并且使用structuremap把service层注入到controller,把repository注入到service层。service层主要是我们的业务逻辑层,这一层不和底层的database打交道,和database打交道的是repository数据持久层。本篇文章通过使用structuremap依赖注入使controller,service,repository三层的耦合度降到最低。

本系统使用northwind开源数据,并且使用entityframework5.0实现对数据库的object映射。

开始正题之前先来看一下成型的框架结构,我们将围绕这个截图进行展开。

mvc-customized-framework

 

首先我们看tystudiodemo.models这个project里面的内容

mvc-customized-framework

这里面有我们的entityframwork的edmx文件,northwind的数据库表映射的对象集合。这里建立ado.net entity data model的时候没有使用默认生成一堆.tt文件的方式,而是使用了老的形式。实现方法是首先按默认程序建立起data model,建立好data model之后删除.tt文件。然后打开.edmx文件,右键单击空白处选择properties(属性),会出现下面的截图,这时候只需要修改一下code generation strategy(中文翻译不知道是什么,第一个就对了)的值,默认是none,我们修改为default,然后保存.edmx

edmx-propertoties

你应该已经注意到了,项目里多了一个tyentities.cs文件,这个我们是我们这个系统中实现transaction(事务处理)的关键。
我们使用static和[threadstatic]属性来保证一个线程拿到的tyentities(objectcontext)总是同一个,这就解决了transaction事务的问题。没有解释到的请详细阅读下面代码里面的注释。

复制代码 代码如下:

using system;
 using system.collections.generic;
 using system.linq;
 using system.text;
 using system.threading.tasks;
 using system.web;

 namespace tystudiodemo.models
 {
     public partial class tyentities
     {
         #region fields

         //定义索引名称
         const string contextkey = "tyentities";

         //标记为threadstaticattribute的静态字段不在线程之间共享。
         //每个执行线程都有单独的字段实例,并且独立地设置及获取该字段的值。如果在不同的线程中访问该字段,则该字段将包含不同的值。
         [threadstatic]
         private static tyentities _current;

         #endregion

         #region properties

         public bool disposed { get; set; }

         /// <summary>
         /// 当系统工作在httpcontext下,将使用延迟家在技术返回一个tyentities(objectcontext),如果没有httpcontext将返回null
         ///
         /// 不论在哪里使用tyentities,在请求结束后都需要调用tyentities.cleanup()方法
         /// 最佳的方式是tyentities.cleanup()放到global.asax.cs文件里面。
         /// void application_endrequest(object sender, eventargs e)
         /// {
         ///     tystudiodemo.models.tyentities.cleanup();
         /// }
         /// </summary>
         private static tyentities forwebrequest
         {
             get
             {
                 var context = httpcontext.current;

                 //检查httpcontext是否存在
                 if (context != null)
                 {
                     //试着从context中得到tyentities
                     var result = context.items[contextkey] as tyentities;

                     if (result == null)
                     {
                         //创建新的datacontext,并且保存到context里面
                         result = new tyentities();
                         context.items[contextkey] = result;
                     }

                     return result;
                 }

                 return null;
             }
         }

         /// <summary>
         /// 这是一个用来获取tyentities(objectcontext)的公共属性
         ///
         /// 如果你通过httpcontext获取tyentities,同样不论在哪里使用tyentities,在请求结束后都需要调用tyentities.cleanup()方法
         ///
         /// 如果没有通过httpcontext获取tyentities,你必须在使用结束之后调用tyentities.cleanup()方法,来清理objectcontext。
         ///
         /// 需要注意的一点是,无论使用哪种方式获取tyentities,我们都必须手动的清理和dispose tyentities(objectcontext)。
         /// 所以一定不要在using块中使用tyentities(objectcontext)。
         /// </summary>
         public static tyentities current
         {
             get
             {
                 //从httpcontext中获取datacontext
                 var result = tyentities.forwebrequest;

                 if (result != null)
                     return result;

                 //试着获取当前活动的tyentities
                 if (_current == null)
                     _current = new tyentities();

                 return _current;
             }
         }

         /// <summary>
         /// 清理结束tyentities(objectcontext)
         /// </summary>
         public static void cleanup()
         {
             if (httpcontext.current != null)
             {
                 var result = httpcontext.current.items[contextkey] as tyentities;

                 if (result != null)
                     result.dispose();

                 httpcontext.current.items[contextkey] = null;
             }
             else if (_current != null)
             {
                 _current.dispose();
                 _current = null;
             }
         }

 
         protected override void dispose(bool disposing)
         {
             bool disposed = disposed;
             disposed = true;

             if (!disposed)
                 cleanup();

             base.dispose(disposing);
         }

         #endregion
     }
 }

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网