1: Project Structure
2: Add references to other layers for each layer. Here we place all the files generated by all layers except the web layer under the library folder under the solution, then, each project references the DLL project files respectively.
On the model project, right-click Properties-> Generate-> select the library folder at the upper level in the output below.
2.2 adjust the project generation sequence, right-click the solution or any project, select generate dependency, and adjust the dependencies of each project. The purpose is to adjust the project generation sequence.
Note: here you choose dependencies, and do not add DLL references between projects, but simply modify the order of Project generation.
The bll layer depends on the common layer and the model layer.
Common dependency model layer
The repository depends on the model and common layers.
Bll depends on model, common, and repository layers.
The UI Layer depends on all the above layers. The order of Project generation after adjustment is as follows:
The final generation order is
Compile the entire project to see the generation sequence of each project.
2.3 We reference each project.
The bll layer references the DLL files generated at the common layer and model layer in the library folder.
Common references the DLL file generated by the model layer in the library folder
Repository references the DLL files generated by the model and common layers in the library folder.
Bll references the DLL files generated at the model, common, and repository layers in the library folder.
The UI Layer references the DLL files generated by the model, common, and BLL layers in the library folder (you do not need to reference the repository Layer ).
3: At the model layer, add the EF Entity Framework and copy the link strings in APP. config to Web. config In the UI Layer.
4: Write the repository database access layer code.
4.1 write userinforepository
Database Access Code of userinforepository User table /// <summary> // User table operation class /// </Summary> public class userinforepository {public userinforepository () {} private modelfirstdemoentities DB = new modelfirstdemoentities (); # Add an object to region // <summary> // Add an object // </Summary> /// <Param name = "model"> </param> /// <returns> </returns> Public userinfo addentity (userinfo Model) {dB. userinfo. add (model); dB. savechanges (); Return Model ;}# endregion # region perform the delete operation int Delete (expression <func <userinfo, bool> wherelamion) based on the input conditions) /// <summary> /// Delete int Delete (expression <func <userinfo, bool> wherelambda) based on the input conditions) /// </Summary> /// <Param name = "wherelammary"> conditions for deletion </param> /// <returns> </returns> Public int Delete (expression <func <userinfo, bool> wherelamist) {var deletelist = dB. userinfo. where (wherelambda ). tolist (); deletelist. foreach (u => dB. userinfo. remove (u); Return dB. savechanges () ;}# endregion # region modifies the object Value int Update (userinfo model, parameter string [] propertyname) based on the attributes to be modified by the object) /// <summary> /// modify the object Value int Update (userinfo model, string [] propertyname) based on the attributes that need to be modified by the object) /// </Summary> /// <Param name = "model"> modified object </param> /// <Param name = "propertyname"> attributes to be modified </param> // <returns> returns the number of affected rows </returns> Public int modif (userinfo model, string [] propertyname) {dbentityentry entry = dB. entry (model); entry. state = entitystate. unchanged; // set the state of the model to be modified to the unmodified state foreach (VAR proname in propertyname) {entry. property (proname ). ismodified = true; // set the attribute to be modified.} return dB. savechanges ();} # endregion # modify the object values in batches Based on the attributes that need to be modified by the entity. // <summary> // modify the object values in batches Based on the attributes that need to be modified by the entity/ // call the instance userinfo newuserinfo = new userinfo () {username = "new name"}; // modifby (newuserinfo, u => U. id> 1, "username "); /// </Summary> /// <Param name = "newmodel"> new value </param> /// <Param name = "wherelambda"> query data </param> /// <Param name = "propertyname"> attributes to be modified </param> /// <returns> the number of affected rows returned </returns> Public int modifby (userinfo newmodel, expression <func <userinfo, bool> wherelambda, Params string [] propertyname) {list <userinfo> modilist = dB. userinfo. where (wherelambda ). tolist (); Type type = typeof (userinfo); // obtain the type list <propertyinfo> propertyinfos = type. getproperties (). tolist (); dictionary <string, propertyinfo> dictionary = new dictionary <string, propertyinfo> (); foreach (VAR property in propertyinfos) {If (propertyname. contains (property. name) {dictionary. add (property. name, property) ;}} foreach (VAR olduserinfo in modilist) {foreach (VAR proname in propertyname) {propertyinfo property = dictionary [proname]; object val = property. getvalue (newmodel, null); // obtain data from the New Model Based on attributes, for example, obtaining property. setvalue (olduserinfo, Val, null); // modify the value based on this attribute} return dB. savechanges () ;}# endregion # Region Query without the public list page <userinfo> getlistby (expression <func <userinfo, bool> wherelamby) {return dB. userinfo. where (wherelambda ). tolist ();} # endregion # region query by PAGE and reverse order // <summary> // query by page or reverse order /// </Summary> // <typeparam name = "tkey"> This can be left empty, the compiler will automatically deduce the type of the column (Int or string type) in your orderby statement) </typeparam> /// <Param name = "wherelam.pdf"> query conditions </param> /// <Param name = "orderbylambda"> sorting conditions </param> // /<Param name = "pagesize"> page size </param> // <Param name = "pageindex"> page number </param> // <Param name =" isdesc "> reverse order, whether the default value is (that is, we use the forward sequence from small to large by default) </param> // <returns> </returns> public list <userinfo> getlistby <tkey> (expression <func <userinfo, bool> wherelambda, expression <func <userinfo, tkey> orderbylambda, int pagesize = 10, int pageindex = 1, bool isdesc = false) {If (isdesc) {return dB. userinfo. where (wherelambda ). orderbydescending (orderbylambda ). skip (pagesize * (pagesize-1 )). take (pagesize ). tolist ();} else {return dB. userinfo. where (wherelambda ). orderby (orderbylambda ). skip (pagesize * (pagesize-1 )). take (pagesize ). tolist () ;}# endregion}
4.2 Considering that we have multiple tables and each table has these additions, deletions, modifications, and queries, We Need To refactor and write a baserepository class.