It was convenient to do some small projects with EntityFramework Code first, but after touching the mybatis in a Java project, I deeply liked the most direct and most original and flexible database operation. So the final decision to transform the previous project, using Ibatis access to the database;
First, frame construction
1) Create a new ASP. NET MVC application. NET use 4.5
2) Install Unity 3.5 with Nuget because you are more familiar with unity;
3) using Nuget to install Unity Bootstrapper for ASP.
4) Install Common service Locator using Nuget
5) Install Ibatisnet, version is a bit old, but still can stabilize work, first use it;
Front desk, jquery,bootstrap, etc.
Second, the configuration
1) Configuration IBatis
A) Configuring log information in Web. config
b) Provider.config, and Sqlmap.config file, placed under ~/app_data
2) Configure Unity
A) Configure with the configuration file:
First to the solution, find packages/unity.xx.xx.xx under the unityconfiguration30.xsd, copy to VisualStudio Xml/schema directory, so that the editing configuration file has syntax hints;
Under the ~/app_data directory, create a unity.config file that reads as follows:
<?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 Join assembly--<assembly Name="mwms.framework"/> <assembly name="MICROSOFT.PRACTICES.UNITY.MVC"/> <assembly name="SYSTEM.WEB.MVC"/> <!--namespaces Join the namespace so that you can use the type directly--<namespaceName="mwms.framework"/> <namespaceName="Mwms.Framework.Interfaces"/> <namespaceName="Mwms.Framework.Repository"/> <namespaceName="SYSTEM.WEB.MVC"/>
<!--Configuring a life cycle Manager--<alias alias="Perrequestlifetimemanager"Type="Microsoft.Practices.Unity.PerRequestLifetimeManager, MICROSOFT.PRACTICES.UNITY.MVC"/> <container> <!--packaging IBatis sqlmapper, self-realization, -<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 Configuring a generic warehousing implementation map--<register type="irepository ' 1"mapto="ibatisrepository ' 1"/>
<!--add more type mappings--</container> </unity></configuration>
b) Load UnityContainer configuration:
Modify the ~/app_start directory UnityConfig.cs, modify the Registertypes method, load the corresponding configuration file;
Public Static voidregistertypes (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> (); varPath = System.IO.Path.Combine (AppDomain.CurrentDomain.BaseDirectory,"App_Data","Unity.config"); varFilemap =NewExeconfigurationfilemap {execonfigfilename =path}; varConfiguration =configurationmanager.openmappedexeconfiguration (Filemap, Configurationuserlevel.none); varUnitysection = (unityconfigurationsection) configuration. GetSection ("Unity"); Containerobject.loadconfiguration (unitysection); }
c) in the Sqlmap configuration file, load the corresponding mapper file ...
Other, is ibatis standard development process, entity class, XML configuration file, warehousing class, service interface, service realization, etc.
Once done, you can directly use the constructor to inject, use code to access the database in the controller, directly with SQL can also complete a lot of logic
Unity + iBatis + ASP network MVC System Setup