Unity + iBatis + Asp.net Mvc 系统搭建

之前用EntityFramework Code First做了一些小项目,很是方便;后来在一个 Java 项目中接触了myBatis之后,深深的喜欢上了这种最直接最原始最灵活的数据库操作,所以最终决定改造之前的项目,使用IBatis访问数据库;

一、框架搭建

1)新建一个Asp.net Mvc的应用,.Net使用4.5

2)使用 Nuget 安装 Unity 3.5,因为比较熟悉 Unity;

3)使用 Nuget 安装 Unity Bootstrapper for ASP.NET MVC

4)使用 Nuget 安装 Common Service Locator

5)安装 IBatisNet,版本有点老了,不过还是能稳定工作,先用着吧;

前台,jquery,bootstrap 等

二、配置

1)配置  IBatis

  a)web.config 中配置 log 信息

  b)provider.config,以及 sqlmap.config 文件,放到了 ~/App_Data下

2)配置 Unity

  a)采用配置文件进行配置:

  首先到解决方案下,找到 Packages/Unity.xx.xx.xx 下的 UnityConfiguration30.xsd,拷贝到VisualStudio 的 xml/schema目录下,这样编辑配置文件就有语法提示了;

  在 ~/App_Data目录下,创建一个 unity.config 文件,内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

    <!-- assemblies 加入程序集 -->
    <assembly name="Mwms.Framework"/>
    <assembly name="Microsoft.Practices.Unity.Mvc"/>
    <assembly name="System.Web.Mvc"/>
    
    <!-- namespaces 加入命名空间,这样就可以直接使用其中的类型了 -->
    <namespace name="Mwms.Framework" />
    <namespace name="Mwms.Framework.Interfaces" />
    <namespace name="Mwms.Framework.Repository" />
      
    <namespace name="System.Web.Mvc"/>

  <!-- 配置一个生命周期管理器 --> <alias alias="PerRequestLifeTimeManager" type="Microsoft.Practices.Unity.PerRequestLifetimeManager, Microsoft.Practices.Unity.Mvc"/> <container> <!-- 包装 IBatis 的 SQLMapper,自行实现, --> <register type="SqlMapperWrapper" mapTo="SqlMapperWrapper"> <lifetime type="PerRequestLifeTimeManager" /> <constructor> <param name="configPath" value="~/App_Data/SqlMap.config" /> </constructor> </register> <!--<register type="IMwLogger" mapTo="DebugLogger" /> <register type="IMwCacher" mapTo="DefaultNetCacher" />--> <!-- Repositories 配置一个泛型仓储实现映射 --> <register type="IRepository`1" mapTo="IBatisRepository`1" />  
    <!-- 加入更多类型映射 --> </container> </unity> </configuration>

  b)载入UnityContainer 配置:

  修改 ~/App_Start 目录下 UnityConfig.cs , 修改 RegisterTypes 方法,载入对应的配置文件;      

public static void RegisterTypes(IUnityContainer containerObject)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            // container.LoadConfiguration();

            // TODO: Register your types here
            // container.RegisterType<IProductRepository, ProductRepository>();

            var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_data", "unity.config");

            var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = path };
            var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");

            containerObject.LoadConfiguration(unitySection);
        }

  c)SqlMap 配置文件中,载入相应的Mapper文件……

  其他的,就是IBatis的标准开发流程,实体类,xml配置文件,仓储类,服务接口,服务实现等;

做完之后,就可以直接使用构造函数注入,在 Controller中使用代码访问数据库了,直接用SQL也可以完成很多逻辑

原文地址:https://www.cnblogs.com/mobwiz/p/4578413.html