spring.net-Business Layer Warehousing
Catalog: ASP. NET MVC4 get started to Master Series catalog summary
In the previous section, we have built the prototype of the project framework, and now I am going to start the business implementation, in the process of business implementation, constantly to improve our existing framework.
1, suppose we do a user login business
Then we can now idal the interface defined in the project Iou_userinfodal, attention is partial class partial, in order to facilitate management, these extensions of some of the interfaces are unified into the folder Extensionidal for management, Note that the namespace is consistent with the previous part of the interface.
Using Model;
namespace idal{public partial interface iou_userinfodal { ou_userinfo getuserinfobyname (string LoginName);} }
2. Dal project, new folder Extensiondal, new section under folder Ou_userinfodal
Using idal;using model;namespace dal{public partial class Ou_userinfodal:iou_userinfodal {public ou_ UserInfo getuserinfobyname (String loginName) { return base. Getlistby (x = X.uloginname = = loginName). FirstOrDefault ();}}}
3. IBLL Project New Folder EXTENSIONIBLL, new IOU_USERINFOBLL interface under folder
Using model;namespace ibll{public Partial interface iou_userinfobll { ou_userinfo Login (String strName , string strpwd);} }
New Folder EXTENSIONBLL in BLL project, new OU_USERINFOBLL class under Folder
Using model;using ibll;namespace bll{public partial class OU_USERINFOBLL:IOU_USERINFOBLL {// < summary>///Login//</summary>// <param name= "StrName" ></param>// < param name= "strpwd" ></param> ///<returns></returns> Public ou_userinfo Login (string StrName, String strpwd) { //1. Call the business layer method to query ou_userinfo usr = base based on the login name. Getlistby (U = u.uloginname = = strName). FirstOrDefault (); 2. Determine if the login succeeded return null;}}
4, about the use of Spring.net please refer to: 17, the introduction of ASP. NET MVC to master--spring.net
We download the Spring.net package and then copy the Spring.Core.dll and Common.Logging.dll. Add a Lib folder in your Web project to hold a third-party DLL.
Di project, add a reference to these two DLLs, and then create a new class Springhelper
Using spring.context;using spring.context.support;namespace di{public static class Springhelper { # Region 1.0 Spring Container Context-iapplicationcontext springcontext//<summary>// Spring Container context/// </ summary> private static Iapplicationcontext springcontext { get { return Contextregistry.getcontext (); } } #endregion #region 2.0 Gets the configuration file configuration Object +t getobject<t> (string objname) where t:class// <summary>
///Get configuration file Configuration Objects///</summary>// <typeparam name= "T" ></typeparam>// < param name= "objname" ></param>/// <returns></returns> public static T getobject<t > (string objname) where T:class { return (T) springcontext.getobject (objname); } #endregion }
5. Modify the Web. config file in the project to add a spring configuration
<!--Spring Configuration-- <sectiongroup name= "Spring" > <section name= "context" type= " Spring.Context.Support.WebContextHandler, Spring.web "/> <!--support for defining objects in Web. config-- <section Name= "Objects" type= "Spring.Context.Support.DefaultSectionHandler, Spring.core"/> </sectionGroup> </configSections> <spring> <context> <resource uri= "~/config/ Objects.xml "/> </context> </spring>
A folder named Config is created in the Web project to save a separate configuration file
<?xml version= "1.0" encoding= "Utf-8"? ><objects xmlns= "Http://www.springframework.net" > <object Id= "Ou_userinfo" type= "BLL. Ou_userinfo,bll "singleton=" false "></object> <object id=" bllsession "type=" BLL. Bllsession,bll "singleton=" false "></object> <object id=" dbsessfactory "type=" DAL. Dbsessionfactory,dal "></object></objects>
Modify BaseBLL.cs
#region Data Warehousing Properties + idbsession dbsession///<summary>//Data Warehousing properties//</summary> Public Idal. Idbsession Dbsession {get {if (idbsession = = null) { 1. Read the configuration file//string Strfactorydll = Common.ConfigurationHelper.AppSetting ("Dbsessionfatory DLL "); String strfactorytype = Common.ConfigurationHelper.AppSetting ("Dbsessionfatory"); 2.1 Create a Dbsessionfactory factory object by reflection//assembly Daldll = Assembly.LoadFrom (Strfactorydll); Type typedbsessionfatory = Daldll.gettype (Strfactorytype); Idal. Idbsessionfactory sessionfactory = Activator.CreateInstance (typedbsessionfatory) as Idal. Idbsessionfactory; 2. Use the spring.net in the DI layer to create the Dbsessionfactory factory object Idal based on the content of the configuration file. Idbsessionfactory sessionfactory = DI. springhelper.getobject<Idal. Idbsessionfactory> ("Dbsessfactory"); 3. Create Dbsession object through Factory idbsession = Sessionfactory.getdbsession (); } return idbsession; }} #endregion
6. We look at the call of the controller
Public ActionResult Index () { //1. Querying data via the business interface var ou_userinfobll = DI. Springhelper.getobject<iou_userinfobll> ("Ou_userinfo"); var userinfo= ou_userinfobll.login ("", "" "); 2. Load View return view (); }
Every place I go through di.springhelper.getobject<iou_userinfobll> ("Ou_userinfo"), to invoke is not very inconvenient, Then we can create a business layer warehouse to manage the creation of these objects uniformly.
7, IBLL project, new T4 template ibllsession.tt, copy before Idal template idalsession come over, slightly modified on it can be
View Code
<#@ Template language= "C #" debug= "false" hostspecific= "true" #>
<#@ include file= "EF. Utility.CS.ttinclude "#><#@ output extension=". CS "#>
<#
Codegenerationtools code = new Codegenerationtools (this);
Metadataloader loader = new Metadataloader (this);
Coderegion region = new Coderegion (this, 1);
Metadatatools EF = new Metadatatools (this);
String inputfile = @ "... \MODEL\OA.EDMX ";
Edmitemcollection itemcollection = loader. Createedmitemcollection (Inputfile);
String namespacename = code. Vsnamespacesuggestion ();
Entityframeworktemplatefilemanager FileManager = Entityframeworktemplatefilemanager.create (this);
#>
using System;
Using System.Collections.Generic;
using System.Linq;
using System.Text;
Namespace IBLL
{
public partial Interface Ibllsession
{
<#
Emit Entity Types
foreach (EntityType entity in itemcollection.getitems<entitytype> (). (e = e.name))
{#>
I<#=entity. NAME#>BLL i<#=entity. Name#>bll{get;set;}
<#}#>
}
}
8, the BLL project, new T4 template bllsession.tt, copy the previous Dal template dalsession come over, a little modification on it can be
View Code
<#@ Template language= "C #" debug= "false" hostspecific= "true" #>
<#@ include file= "EF. Utility.CS.ttinclude "#><#@ output extension=". CS "#>
<#
Codegenerationtools code = new Codegenerationtools (this);
Metadataloader loader = new Metadataloader (this);
Coderegion region = new Coderegion (this, 1);
Metadatatools EF = new Metadatatools (this);
String inputfile = @ "... \MODEL\OA.EDMX ";
Edmitemcollection itemcollection = loader. Createedmitemcollection (Inputfile);
String namespacename = code. Vsnamespacesuggestion ();
Entityframeworktemplatefilemanager FileManager = Entityframeworktemplatefilemanager.create (this);
#>
using System;
Using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
Namespace BLL
{
public partial class Bllsession:ibllsession
{
<#
int index=0;
Emit Entity Types
foreach (EntityType entity in itemcollection.getitems<entitytype> (). (e = e.name))
{
index++;
#>
#region <#=index #> Data Interface i<#=entity. Name#>bll
I<#=entity. NAME#>BLL i<#=entity. name#>bll;
Public i<#=entity. NAME#>BLL i<#=entity. name#>bll{
Get
{
if (i<#=entity. Name#>bll==null)
I<#=entity. Name#>bll=new <#=entity. NAME#>BLL ();
Return i<#=entity. name#>bll;
}
Set
{
I<#=entity. name#>bll=value;
}
}
#endregion
<#}#>
}
}
9. Under the UI directory, add the class Library project Web.helper, add a reference to the Ibll and DI projects, and create a new OperateContext.cs
Using di;using ibll;namespace web.helper{public class Operatecontext {public static ibllsession _ Ibllsession = springhelper.getobject<ibllsession> ("Bllsession");} }
10. Modify the controller call
Using IBLL;
Using Web.helper;
Public ActionResult Index () { var userInfo = Operatecontext._ibllsession.iou_userinfobll.login ("", ""); 2. Load View return view (); }
Now it is not convenient to call up a lot.
spring.net-Business Layer Warehousing