Back to directory
First, I want to know iunitofwork. Why does the iunitofwork interface appear? 2. What happened to submtchanges () in LINQ to SQL and entityframeworks Third, there is no conflict between method integrity and unified submission. Fourth, dbcontext object creation should be made public to the BLL Layer Fifth, my iunitofwork + repository Architecture
In EF, the data context is usually dbcontext or objectcontext, while in LINQ to SQL, the data context is datacontext. Their role is to create a database ORM ing object Orm, with more convenient operations on databases, and their creation work, I have constrained it to the Dal layer for a long time, but the BLL layer does not expose the creation method, but when I am on. net, it is necessary to publish the creation of data context to the BLL layer.ProgramPerformance and atomic operations.
It turns out that when we call an add operation on The bll layer, we need to first define this implementation on the Dal layer. Even if this entity only has one add operation, you need to implement it. This will undoubtedly increaseCodeVolume, like this:
Public Class Productrepository: testbase <product> { # Region Constructors Public Productrepository (){} Public Productrepository (iunitofwork dB ): Base (Testdatacontext) dB ){} # Endregion /// <Summary> /// This repository should also be created in one method, which is a bit bad. /// </Summary> /// <Param name = "entity"> </param> Public Override Void Insert (product entity ){ Base . Insert (entity );}}
However, if we publish the data context creation Work To The bll layer, the results will be different. Then we can integrate the Dal method in The bll layer with the iunitofwork idea, it's cool to send a connection request to the database.
Public Abstract Class Bllbase { Protected Iunitofwork { Get ; Private Set ;} Public Bllbase (): This ( Null ){} Public Bllbase (iunitofwork) {iunitofwork = Iunitofwork ;} Protected Icompleterepository <t> loadrepository <t> () Where T: Class { Return Iunitofwork = Null ? New Testbase <t> (): New Testbase <t> (Iunitofwork );}}
For the founders of the BLL layer, haha, bllbas, it injects the data context creation work in the construction method, and then passes it to the loadrepository generic method, which can be used in the business class of The bll layer.
Inherit from it and instantiate the data context, and then use loadrepository to directly perform the curd operation on the data table. Everything is as simple as this. Look at the Code:
# RegionDirectly call public methods in bllbaseIunitofwork. isnotsubmit=True;NewOrderrepository (iunitofwork). insert (order );If(Product! =Null) Loadrepository<Product>(). Insert (product); iunitofwork. savechanges ();# Endregion
OK, the UI Layer directly calls the specific business method of The bll layer. Let's look at the class structure of My Dal layer. Sometimes, the more I get the class structure, the more I can explain the problem than the Code:
Curd operation specifications:
Dal layer repository mode implementation:
Iunitofwork work unit specifications:
Back to directory