Abstract: The last article wrote the first part of how to build a simple project framework, and talked about the relevant knowledge about the decoupling between Dal and Bll. This article will talk about the latter part.
I talked about DbSession in the previous article, and now I'll continue to talk about it.
First of all, some similar operations are perfected. Same as the Dal layer, we can also encapsulate some widely used similar operations in the Bll layer into the base class. In addition, we also need to add an interface layer to the Bll layer.
The corresponding code is directly given below:
Bll layer base class:
BaseService.cs
1 public abstract class BaseService<T> where T: class,new() 2 { 3 public IBaseDal<T> CurrentDal {get; set;} 4 5 public IDbSession DbSession 6 { 7 get 8 { 9 return DbSessionFactory.GetCurrentDbSession(); 10} 11} 12 13 public BaseService() 14 { 15 SetCurrentDal(); 16} 17 18 public abstract void SetCurrentDal();//Abstract method: subclass must be implemented. 19 20 public IQueryable<T> GetEntityByLambda(Expression<Func<T,bool>> wherelambda) 21 { 22 return CurrentDal.GetEntityByLambda(wherelambda); 23 } 24 25 26 public T AddEntity(T entity) 27 { 28 CurrentDal.AddEntity(entity); 29 DbSession.SaveChanges(); 30 return entity; 31} 32 33//Delete 34 35 public bool DeleteEntity(T entity) 36 { 37 CurrentDal.DeleteEntity(entity); 38 return DbSession.SaveChanges()> 0; 39 40} 41//change 42 public bool UpdateEntity(T entity) 43 { 44 CurrentDal.UpdateEntity(entity); 45 return DbSession.SaveChanges()> 0; 46 47} 48}
IBaseService.cs
1 public interface IBaseService<T> where T: class,new() 2 { 3 IQueryable<T> GetEntityByLambda(Expression<Func<T, bool>> wherelambda); 4 T AddEntity(T entity); 5 bool DeleteEntity(T entity); 6 bool UpdateEntity(T entity); 7}
IUserService.cs
1 public interface IUserService:IBaseService<Users> 2 { 3 4}
UserService.cs
1 public class UserService:BaseService<Users>,IUserService 2 { 3 4//IUserDal userDal = AbstractFactory.GetUserDal(); 5//IDbSession dbSession = DbSessionFactory.GetCurrentDbSession(); 6//public Users AddUser(Users user) 7//{ 8//dbSession.UserDal.AddEntity(user); 9//dbSession.SaveChanges(); 10//return user; 11//} 12 13//public IQueryable<Users> GetUserByLambda(Expression<Func<Users,bool>> wherelambada) 14//{ 15//return dbSession.UserDal.GetEntityByLambda(wherelambada); 16//} 17 18//public bool DeleteUser(Users user) 19//{ 20//dbSession.UserDal.DeleteEntity(user); 21//return dbSession.SaveChanges()> 0; 22//} 23 24//public bool UpdateUser(Users user) 25//{ 26//dbSession.UserDal.UpdateEntity(user); 27//return dbSession.SaveChanges()> 0; 28//} 29 30//Assign the properties of the parent class 31 public override void SetCurrentDal() 32 { 33 CurrentDal = this.DbSession.UserDal; 34} 35}
The above is the construction of some encapsulation and interface layer of the Bll layer. Now based on the existing code, let's take a look at the routine calls of the controller to the Bll layer:
Here is a short code:
HomeController => AddUser method
1 public ActionResult AddUser(FormCollection form) 2 { 3 Users user = new Users(); //Focus on this sentence, calling the Bll layer 4 IUserService userService = new UserService(); 5 user.UserName = form["name"]; 6 user.PassWord = form["pwd"]; 7 user.PhoneNumber = form["phone"]; 8 user.EMail = form["email"]; 9 userService.AddEntity(user); 10 if (user == null) 11 { 12 return View("Contact"); 13} 14 else 15 { 16 return View("About"); 17} 18}
This is a piece of code to add user information to the database, the front-end code is omitted.
From this code, we can see that the coupling between the Bll layer and the UI is still very high.
IUserService userService = new UserService();
This sentence is the same as the Bll layer and the Dal layer mentioned earlier. The degree of coupling is very high. So how to reduce this degree of coupling? Do you use the factory method again? No, no, it seems too troublesome. A new point of knowledge is going to be used here-spring.net (it is said that this is very awesome, but I am not sure, I can only write while learning).
Next, let's talk about the usage and steps of spring.net:
1. Add Spring.Net block configuration and Spring.Net container configuration node in web.config
Block configuration node:
1 <!--Spring.Net block configuration--> 2 <sectionGroup name="spring"> 3 <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/> 4 </sectionGroup>
Container configuration node
1 <!--Configuration node of Spring.Net container--> 2 <spring> 3 <context> 4 <resource uri="file://~/Config/controllers.xml"/> 5 </context> 6 </spring>
2. Add relevant references
1. create a Spring.Net folder in the package folder under the program directory, and then import the following files:
Import the following files:
Add a reference under the Ui project:
3. Change the global.aspx.cs file
Change MvcApplication from inheriting System.Web.HttpApplication to Sring.Web.Mvc.SpringMvcApplication.
4. Create a new Config folder under the UI project, and create the following two configuration files in the folder: controller.xml and services.xml
The controller.xml file code is as follows:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <objects xmlns="http://www.springframework.net"> 3 4 <!--<object></object> If you refer to a few Services, write a few Services. Here only one Service is referenced, so only one--> 5 <!--type = "The full name of the class (namespace name + class name), assembly name" --> 6 <object type="IotPf.UI.Controllers.HomeController,IotPf.UI" singleton="false"> 7 <property name="UserService" ref="UserService"/> 8 9 </object> 10 </objects>
The services.xml file code is as follows:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <objects xmlns="http://www.springframework.net"> 3 4 <object name="UserService" type=" IotPf.Bll.UserService,IotPf.Bll" singleton="false"> 5 </object> 6 7 </objects>
Note: Remember to add the reference of services.xml in web.config, and pay attention to the order, first services.xml, then controller.xml
5. Then we return to HomeController: add the attribute userService in it
Then execute the program and find the following error will be reported:
At this point, we add the following reference:
microsoft.aspnet.webapi
Then, execute the program again:
The operation result is correct:
This is how to use Spring.Net.
By using Spring.Net, the purpose of decoupling the UI layer and the Bll layer is also achieved, and the operation is much simpler than the operation of the factory.
Well, the second part of a simple framework, the decoupling of the UI layer and the Bll layer has also been completed. This blog about the framework is written here.
My email: 3074596466@qq.com
If there is any error, please correct me!