ASP. net mvc + EF framework + EasyUI permission management series (4)-encapsulation of the business logic layer

Source: Internet
Author: User

If you want to watch a blog, I suggest you start with the first one, which is the easiest to understand. If you don't talk much about it, let's go straight to the topic.

1. Preliminary Design of the business logic layer

(1) From yesterday we made a comment on LYZJ. userLimitMVC. for BLL class library operations, we added UserInfoService under this class library, so other object objects also have services, so this is a repetitive action, UserInfo contains all the methods for operating the database, role also contains database operation methods. What should we do if other tables are the same? Of course, it is encapsulated. At this time, we need to create a base class to implement this action.

(2) First, we added two classes to the base of yesterday. Then we added a RoleService class and a BaseService class under the LYZJ. UserLimitMVC. BLL class library.

2. BaseService Analysis

(1) first, the BaseService class must be a generic type that limits its inheritance from the class. In this case, the corresponding addition, deletion, modification, and query methods will be implemented.

(2) At this time, I will post part of the BaseService code and then explain it. If anyone who has developed such projects knows how to use this item, I will explain it below, the explanation is not very good. It is my own language. If anyone can better explain it, I would like to leave a message for me. I would like to thank you very much.


1 namespace LYZJ. UserLimitMVC. BLL
2 {
3 public abstract class BaseService <T> where T: class, new ()
4 {
5 public IDAL. IBaseRepository <T> CurrentRepository {get; set ;}
6 // base class Constructor
7 public BaseService ()
8 {
9 SetCurrentRepository (); // called in the constructor. This sets the abstract method of the current warehouse.
10}
11
12 // Constraints
13 public abstract void SetCurrentRepository (); // The subclass must be implemented.
14
15 public T AddEntity (T entity)
16 {
17 // call the warehouse corresponding to T for adding
18 return CurrentRepository. AddEntity (entity );
19}
20}
21}

(3) The following is my analysis of the above Code

First, write the Public T AddEntity () {return CurrentRepository. addEntity (entity) ;}, when we add an object, we first need to call the database access layer corresponding to T to call the corresponding Entity method, when we wrote UserInfoService yesterday, we directly called the Entity method of UserInfoRepository (warehouse). However, the code in UserInfoService must be implemented in the base class, the T in the base class corresponds to the warehouse at the database access layer, which we do not know. What should we do?

Don't be alarmed. If we analyze the code carefully, we will find the solution. Who is T's warehouse now? We need to call the method to implement the Add work. Now, let's assume that we know that the store corresponding to T is called CurrentRepository. We only need to pass the entity parameter to the method to achieve CurrentRepository. addEntity (entity);, but it is assumed that it is still not true, but this is our idea at this time, we already know that this can be solved, so we only need to find a solution.

At present, we don't know who the T Warehouse corresponds to in our base class. reflection can be used here, but I won't do this here, since our base class does not know who T corresponds to the warehouse? Then we must define who the sub-class T corresponds to, because the sub-class corresponds to the object, so we have a solution at this time, at this time, we only need to let the subclass set the instance of this attribute. At this time, we will write an attribute under the base class. Note: our current warehousing may target the warehousing of all tables, so we write: public abstract void SetCurrentRepository ();, then we will call the interface attributes to complete the process.

At this time, we thought, why should we write this? Because the current warehouse may point to any warehouse, it may point to the UserInfo warehouse, or it may point to the RoleInfo warehouse, so here we use IDAL. IBaseRepositroy is acceptable, because when you carefully look at the figure I spent, you can see that the warehouse basically inherits from the IBaseRepository warehouse indirectly, then we can use the IBase warehouse to receive the current warehouse without any problems.

Key: when we define this, CurrentRepository is null. In this case, if the following method is called, an exception may be reported. Therefore, we must assign a value to the CurrentRepository Attribute before calling this method, so where can I assign a value to this attribute? I thought of the constructor. The constructor assigns the most appropriate value to this attribute. Here, we do not know the value assigned by CurrentRepository in the base class, only the subclass knows that the parent class wants the subclass to assign a value to this attribute. What should I do? We define an abstract method. When we define an abstract method, our class should also be changed to abstract. The abstract method is as follows: public abstract void SetCurrentRepository ();, this abstract method subclass must be implemented. Here I will call this abstract method in the constructor.

The constructor in the base class calls the pure abstract method of the parent class, and then executes the warehousing in the subclass when the method is finally called after polymorphism.

At this point, my basic class explanation on the business logic layer has been completed. I personally feel that the language is really poorly organized. If anyone doesn't understand it or what it is, you can add a group. Let's discuss it together, here, we use LYZJ. userLimitMVC. the BLL class library is complete.

3. source code of the LYZJ. UserLimitMVC. BLL class library

(1) I have already discussed the implementation of this class library, so it is not cumbersome here. Next I will paste the final code for your reference.

(2) BaseService. CS


1 namespace LYZJ. UserLimitMVC. BLL
2
3 {
4
5 public abstract class BaseService <T> where T: class, new ()
6
7 {
8
9 public IDAL. IBaseRepository <T> CurrentRepository {get; set ;}
10
11 // base class Constructor
12
13 public BaseService ()
14
15 {
16
17 SetCurrentRepository (); // called in the constructor. This sets the abstract method of the current warehouse.
18
19}
23 public abstract void SetCurrentRepository (); // The subclass must be implemented.
24
27 // Add a database
28
29 public T AddEntity (T entity)
30
31 {
32
33 // call the warehouse corresponding to T for adding
34
35 return CurrentRepository. AddEntity (entity );
36
37}
41 // implement the data modification function
42
43 public bool UpdateEntity (T entity)
44
45 {
46
47 return CurrentRepository. UpdateEntity (entity );
48
49}
53 // Delete the database
54
55 public bool DeleteEntity (T entity)
56
57 {
58
59 return CurrentRepository. DeleteEntity (entity );
60
61}
65 // query the database-simple query
66
67 public IQueryable <T> LoadEntities (Func <T, bool> wherelamties)
68
69 {
70
71 return CurrentRepository. LoadEntities (wherelamties );
72
73}
77 // <summary>
78
79 // implement paging query of data
80
81 /// </summary>
82
83 // <typeparam name = "S"> sort by class </typeparam>
84
85 // <param name = "pageIndex"> current page </param>
86
87 // <param name = "pageSize"> how many pieces of data are displayed on a page </param>
88
89 // <param name = "total"> total number of entries </param>
90
91 /// <param name = "whereLambda"> obtain the sorting condition </param>
92
93 // <param name = "isAsc"> sort by reverse or ascending order </param>
94
95 // <param name = "orderByLambda"> sort by the field </param>
96
97 /// <returns> </returns>
98
99 public IQueryable <T> LoadPageEntities <S> (int pageIndex, int pageSize, out int total, Func <T, bool> wherelamties,
100
101 bool isAsc, Func <T, S> orderByLambda)
102
103 {
104
105 return CurrentRepository. LoadPageEntities (pageIndex, pageSize, out total, wherelamties, isAsc, orderByLambda );
106
107}
108
109}
110
111} (3) UserInfoService. cs


1 namespace LYZJ. UserLimitMVC. BLL
2
3 {
4
5 /// <summary>
6
7 // UserInfo business logic
8
9 /// </summary>
10
11 public class UserInfoService: BaseService <UserInfo>
12
13 {
14
15 public override void SetCurrentRepository ()
16
17 {
18
19 CurrentRepository = DAL. RepositoryFactory. UserInfoRepository;
20
21}
22
23 // access the DAL to implement CRUD
24
25 // private DAL. UserInfoRepository _ userInfoRepository = new UserInfoRepository ();
26
27 // Programming Based on interfaces
28
29 // private IUserInfoRepository _ userInfoRepository = new UserInfoRepository ();
30
31 // private IUserInfoRepository _ userInfoRepository = RepositoryFactory. UserInfoRepository;
32
33 // public UserInfo AddUserInfo (UserInfo userInfo)
34
35 //{
36
37 // return _ userInfoRepository. AddEntity (userInfo );
38
39 //}
40
41 // public bool UpdateUserInfo (UserInfo userInfo)
42
43 //{
44
45 // return _ userInfoRepository. UpdateEntity (userInfo );
46
47 //}
48
49}
50
51} (4) RoleService. cs


1 namespace LYZJ. UserLimitMVC. BLL
2
3 {
4
5 public class RoleService: BaseService <Role>
6
7 {
8
9 // rewrite the abstract method and set the current warehouse to Role warehouse
10
11 public override void SetCurrentRepository ()
12
13 {
14
15 // set the current warehouse to Role warehouse
16
17 CurrentRepository = DAL. RepositoryFactory. RoleRepository;
18
19}
20
21}
22
23} 4. business logic interface layer

(1) Do we need an interface layer for the business logic layer? I think so. Why? Because we are involved in calling between layers, and the UI Layer calls the business logic layer, we must rely on interfaces for programming. Then we will implement the business logic interface layer, which is basically consistent with the role of the data access interface layer.

(2) First, we add a class library LYZJ. UserLimitMVC. IBLL to encapsulate the business logic interface layer. Then we create three new interfaces: IUserInfoService, IRoleService, and IBaseService. We need to add a reference to the Model layer for this class library. Below we will write the code for these three interface layers. I will not explain it here, which is similar to the previous meaning.

(3) IBaseService Interface


1 namespace LYZJ. UserLimitMVC. IBLL
2
3 {
4
5 public interface IBaseService <T> where T: class, new ()
6
7 {
8
9 // Add the database and reference the EF framework
10
11 T AddEntity (T entity );
12
13 // modify the database
14
15 bool UpdateEntity (T entity );
16
17 // Delete the database
18
19 bool DeleteEntity (T entity );
20
21 // query the database-simple query
22
23 IQueryable <T> LoadEntities (Func <T, bool> wherelamties );
24
25 /// <summary>
26
27 // implement paging query of data
28
29 /// </summary>
30
31 // <typeparam name = "S"> sort by class </typeparam>
32
33 // <param name = "pageIndex"> current page </param>
34
35 /// <param name = "pageSize"> how many pieces of data are displayed on a page </param>
36
37 // <param name = "total"> total number of entries </param>
38
39 // <param name = "whereLambda"> obtain the sorting condition </param>
40
41 // <param name = "isAsc"> sort by reverse or ascending order </param>
42
43 // <param name = "orderByLambda"> sort by the field </param>
44
45 /// <returns> </returns>
46
47 IQueryable <T> LoadPageEntities <S> (int pageIndex, int pageSize, out int total, Func <T, bool> wherelamties,
48
49 bool isAsc, Func <T, S> orderByLambda );
50
51}
52
53} (4) IUserInfoService Interface


1 namespace LYZJ. UserLimitMVC. IBLL
2
3 {
4
5 public interface IUserInfoService: IBaseService <UserInfo>
6
7 {
8
9}
10
11} (5) IRoleService Interface


1 namespace LYZJ. UserLimitMVC. IBLL
2
3 {
4
5 public interface IRoleService: IBaseService <Role>
6
7 {
8
9}
10
11} 5. Re-Modify the business logic layer

(1) When we implement the business logic interface layer, we need to let BLL implement this interface. We add IBLL references to the BLL class library, and then let UserInfoService, roleService implements their own inheritance relationships. Here I only paste the code of RoleService, and others are the same.


1 namespace LYZJ. UserLimitMVC. BLL
2
3 {
4
5 public class RoleService: BaseService <Role>, IRoleService
6
7 {
8
9 // rewrite the abstract method and set the current warehouse to Role warehouse
10
11 public override void SetCurrentRepository ()
12
13 {
14
15 // set the current warehouse to Role warehouse
16
17 CurrentRepository = DAL. RepositoryFactory. RoleRepository;
18
19}
20
21}
22
23}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.