I wrote an article a while ago called Architecture of Business Layer working with Entity Framework, which has been popular. I am now playing with Entity Framework (EF) Core and some sample code made me think – could I better isolate the EF part of my Business Logic? The answer is yes, and this article describes the new and improved approach, with a lot more examples.
Note: All the code in this article is using .NET Core and Entity Framework Core (EF Core). However, the code is easy to understand for anyone who has played with either flavour of EF, and the concepts work equally well with EF 6.x or EF Core.
What is the Business Layer?
If you are new to idea of Business Logic, then I suggest you read the section near the top called ‘What is the Business Layer’ in my original article as it gives a good description. However, if you are in a hurry here is the short version.
Business Logic is what we call pieces of code that carry out operations that are specific to the job that the application is carrying out. For instance, on an e-commerce site like Amazon you would have Business Logic that handles a customer’s purchase: checking on availability and delivery, taking payments, sending an email confirmation etc. Business logic is definitely a step up on complexity over CRUD (Create, Read, Update and Delete) operations.
While Business Logic can be spread throughout an application and the database, it is accepted best practice to try and isolate the Business Logic into one place, which am calling the Business Layer.
Aside: While having all the business rules in the Business Layer is something we should always strive for I find that in practice some issues go outside the Business Layer for good reasons. For instance validation of data often flows up to the Presentation Layer so that the user gets early feedback. It may also flow down to the database, which checks data written to the database, to ensure database integrity.
Other business logic appears in the Presentation Layer, like not allowing users to buy unless they provide a credit card, but the status should be controlled by the Business Layer. Part of the skill is to decide whether what you are doing is justifiable or will come back and bite you later! I often get that wrong, but hopefully I learn from my mistakes.
The (improved) architecture of my Business Layer
Let me start with a diagram of the new, improved structure of the Business Logic within an application – in this case an ASP.NET web application but the same approach would work in many other application types.
Next I will explain each of the main layers, with example code.
1. The DataLayer
The Data Layer is where the EF POCO (entity) classes are defined, along with the EF setup and DbContext. I am not going to describe how to create the EF entity classes or the setup of the DbContext as I will assume you already know about EF. For information on how to set up the database and EF entities see:
- For EF Core see this documentation.
- For EF 6 see this documentation.
Note: I have shown the Data Layer as one assembly, but in bigger applications the EF entity classes are often in a different assembly to the EF setup code. In that case the BizLogic would only link to the EF entity classes.
2. The BizLogic Layer
I have written numerous applications, all of which has some quite complex Business Logic. Over the years I have tried different ways of organising these applications and I have come up with one key philosophy – that the “The Business Layer is King“, i.e. its design and needs drives everything else.
I am a big fan of Domain-Driven Design (DDD) and Eric Evans’ seminal book on DDD, Domain-Driven Design. Eric Evans’ book talks about how the business problem we are trying to solve, called the “Domain Model”, should be “the heart of the Software” (see Eric Evans book, page 4). Eric goes on to say “When the domain is complex, this is a difficult task, calling for the concentrated effort of talented ad skilled people”. Therefore, I try to make sure that the Business Logics data structures are defined by, and solely focused on, the business problem. How that data is stored and how that data is viewed are secondary issues.
Because of the way the EF works it is not sensible to combine the Business Logic with the actual EF POCO classes. Eric Evan’s recognises and discusses this problem in his book, see Eric Evans book, page 159. However, lots of people have tried to make EF more DDD-like.
NOTE: There is plenty of debate on how to apply DDD to EF. Personally I did try making the EF entity classes behave in a DDD way, but for me the amount of work for the gain in separation became too much (however, it is easier in EF Core though). In trying to go fully DDD with EF I felt I was losing the core tenant of Eric Evans book, which is that the business problem was the focus, not the technology. I recommend Jimmy Bogard comments near the end of this EF issue ‘The impedance mismatch between EF and domain models: what is Entity Framework’s answer?’, which I think are very helpful.
My rules for the Business Logic are:
a. The Business Logic data classes should not be affected by other higher layers
Business Logic can be hard to write. Therefore, I want to stay focused on business issue and not worry about how other layers above might need to reformat/adapt it. So, all data in or out of the Business Logic is either defined inside the BizLogic layer, or it is a Data Layer class. It is Service Layer’s job to act as an Adapter, i.e. Service Layer converts any data to/from what the Business Logic needs.
In new/modern databases the EF POCO (entity) classes should be a very good fit to the Business Logic needs, with a few extra properties needed to set up the database relationships, e.g. Foreign Keys. Therefore, the Business Logic can use the EF POCO (entity) classes directly without any adaption.
However, one of my reader,
, pointed out that for for old/legacy databases the fit between what the database and what the Business Logic may not be a good match.b. The Business Logic works on a set of in-memory data
I don’t want the Business Logic worrying about how to load and save data in the database. It is much simpler to design the Business Logic code to work on simple in-memory set of data. The changes described in this article improve this significantly over the previous article.
In the previous approach I used to have a specific part of the Business Logic, normally at the start, which loaded all of the data. However, the new approach has what I call a DbAccess class which handles all the reading and writing (see later for why this is better). The DbAccess is class is a Facade pattern.
Example Code
Here is an example of Business Logic which is creating the first part of the order, which consists of an Order class instance with a number of LineItems. It is handed the customer’s Id, and list of LineItems which contain an entry for each BookId, followed by how many copies of that book the customer wants.
namespace BizLogic.Orders.Concrete { public interface IPlaceOrderAction : IBizActionAsync<PlaceOrderInDto, Order> { } public class PlaceOrderAction : IPlaceOrderAction { private readonly IPlaceOrderDbAccess _dbAccess; private readonly List<string> _errors = new List<string>(); public IImmutableList<string> Errors => _errors.ToImmutableList(); public bool HasErrors => _errors.Any(); public PlaceOrderAction(IPlaceOrderDbAccess dbAccess) { _dbAccess = dbAccess; } /// <summary> /// This validates the input and if OK creates an order /// and calls the _dbAccess to add to orders /// </summary> /// <returns>returns an Order. Can be null if there are errors</return>; public async Task<Order> ActionAsync(PlaceOrderInDto dto) { if (!dto.TsAndCsAccepted) { _errors.Add("You must accept the T and Cs to place an order."); return null; } if (!dto.LineItems.Any()) { _errors.Add("No items in your basket."); return null; } var booksDict = await _dbAccess.FindBooksByIdsAsync( dto.LineItems.Select(x => x.BookId)) .ConfigureAwait(false); var order = new Order { CustomerName = dto.UserId, LineItems = FormLineItemsWithErrorChecking( dto.LineItems, booksDict) }; _dbAccess.Add(order); return order; } private List<LineItem> FormLineItemsWithErrorChecking( IEnumerable<OrderLineItem> lineItems, IDictionary<int,Book> booksDict) { // rest of the code left out…
A few things to point out about the above.
- We have created a generic IBizAction<Tin, Tout> interface that the PlaceOrderAction class has to implement. This is important as we use a generic BizRunner to run all the actions (described later in the Service Layer section). The IBizActionAsync interface ensures we have the Errors and HasErrors properties as well as the ActionAsync
- It uses the IPlaceOrderDbAccess provided via the constructor to a) find the books that were referred to in the order and b) to add the order to the EF db.Orders set.
NOTE: I also setup an interface, IPlaceOrderAction. I need this to get Dependency Injection (DI) to work. But I won’t be describing DI in this article. However, I really recommend the use of DI on any non-trivial application.
2. The BizDbAccess Layer (new)
The BizDbAccess layer contains a corresponding class for each BizLogic class that accesses the database. It is a very thin Facade over the EF calls. I should stress, I am not trying to hide the EF calls, or make a repository. I am just trying to Isolate the calls. Let me explain.
The problem with my previous approach, where the Business Logic called EF directly, wasn’t that it didn’t work, but that it was hard to remember where all the EF calls were. When it came to refactoring, especially when doing performance improvements, then I had to hunt around to check I had got every EF call. By simply moving all the EF calls into another class I have all of the EF commands in one place.
The main approach I will describe assumes that the database is of a new/modern design and there is a good fit between the data held in the database and the Business Logic requirements. The code below is a PlaceOrderDbAccess class, which goes with the PlaceOrderAction Business Logic described above:
public class PlaceOrderDbAccess : IPlaceOrderDbAccess { private readonly EfCoreContext _db; public PlaceOrderDbAccess(EfCoreContext db) { _db = db; } /// <summary> /// This finds any books that fits the BookIds given to it /// </summary> /// <returns>A dictionary with the BookId as the key, and the Book as the value</returns> public async TaskIDictionary<int, Book>> FindBooksByIdsAsync(IEnumerable<int>; bookIds) { return await _db.Books.Where(x => bookIds.Contains(x.BookId)) .ToDictionaryAsync(key => key.BookId) .ConfigureAwait(false); } public void Add(Order newOrder) { _db.Orders.Add(newOrder); } }
Please note that I use a method called Add, to add the order to the EF Orders DbSet. As I said, I am not trying to hide what is going on, but just isolate the commands, so using the same terminology is helpful.
NOTE: To help with finding the relevant DbAccess I call the Business Logic class <name>Action and the DbAccess calls <name>DbAccess. They also have the same top-level directory name. See the attached picture of the solution directories on the right.
A secondary, but very powerful improvement, is that I could test my Business Logic without using an EF database. Because I provide an Interface then I can replace the DbAccess class with a Mock. Here is my Mock for PlaceOrderDbAccess.
public class MockPlaceOrderDbAccess : IPlaceOrderDbAccess { public ImmutableList<Book> Books { get; private set; } public Order AddedOrder { get; private set; } public MockPlaceOrderDbAccess() { Books = CreateTestData.CreateBooks().ToImmutableList(); } /// <summary> /// This finds any books that fits the BookIds given to it /// </summary> /// <param name="bookIds"></param> /// <returns>A dictionary with the BookId as the key, and the Book as the value</returns> public async Task<IDictionary<int, Book>> FindBooksByIdsAsync( IEnumerable<int> bookIds) { return Books.AsQueryable() .Where(x => bookIds.Contains(x.BookId)) .ToDictionary(key => key.BookId); } public void Add(Order newOrder) { AddedOrder = newOrder; } }
Being able to test all your Business Logic that way is very useful.
Handling old/legacy databases
One of my readers, one of his comments that he finds this approach useful when the database tables are not a good fit for the Business Logic.
, has been using a similar approach to the one describe in this article for some time. He pointed out inAt that point, in addition to isolating the EF commands, the BizDbAccess class can carry out any adapting/ reformatting of the data between the database and the Business Logic.
For example I have come across database which does not mark its relationship keys and Foreign Keys. This stops EF from doing relational fixup, i.e. linking the various entities loaded. In that case you would add code to the DbAccess class to link the loaded entities so that the Business Logic can work on a linked, in-memory set of classes.
The Service Layer
As I said earlier in my applications the Service Layer is very important layer that links everything together. The Service Layer uses both the Command pattern and the Adapter pattern between the presentation layer and all the layers below.
Let me start by showing you one of what I call the BizRunner classes, which is the Command part of the code. This class contains the code to runs the PlaceOrderAction method above, which has a signature of taking data in and producing data out. It also needs data to be written to the database, hence the format/content of this BizRuner:
public class RunnerWriteDbAsync<TIn, TOut> { private readonly IBizActionAsync<TIn, TOut> _actionClass; private readonly DbContext _db; public RunnerWriteDbAsync(IBizActionAsync<TIn, TOut> actionClass, DbContext db) { _db = db; _actionClass = actionClass; } public async Task<TOut> RunActionAsync(TIn dataIn) { var result = await _actionClass.ActionAsync(dataIn).ConfigureAwait(false); if (!_actionClass.HasErrors) //EF can throw a variety of exceptions during SaveChanges await _db.SaveChangesAsync(); return result; } }
The RunnerWriteDbAsync BizRunner is a simple, but powerful class which, through the use of interfaces and generics can run different Business Logic.
One this to note in this arrangement is that only the BizRunner’s RunActionAsync method should call the EF command SaveChangesAsync. That is important for a number of reasons:
- If any of the BizLogic/BizDbAccess methods called SaveChanges arbitrarily then you could get problems, with part of the data being saved while another part wasn’t. By having only the BizRunner calling SaveChanges it means that you know that all the Business Logic has finished.
- A subtler (and more advanced) reason is it allows us to chain together multiple Business Logic calls within a transaction (I describe this in the article ‘Architecture of Business Layer – Calling multiple business methods in one HTTP request’).
- The handling of errors is important, and you can see that the BizRunner will only call EF’s SaveChanges if there are no errors.
Note: The discarding of data by not calling ‘SaveChanges’ only works in situation where each call has its own DbContext. This is the case in a web application as each HTTP request gets a new DbContext. However, in Windows Applications etc. where the DbContext can be kept alive for longer you need to be careful about the lifetime/disposal of the DbContext.
Here is the code inside the Service Layer that uses the BizRunner to call PlaceOrderAction. You can see this acts as an Adapter pattern to the input and output of the Business Logic. The method:
- Gets current basket from a cookie and converts it into the format that the Business Logic needs.
- Calls the RunActionAction method from the PlaceOrderAction class.
- If RunActionAction method was successful it clears the basket, as the order now holds them.
- It extracts and returns the OrderId from the Order instance, as that is what the Presentation Layer needs.
The code is:
{ public class PlaceOrderService { private readonly CheckoutCookie _checkoutCookie; private readonly EfCoreContext _db; public IImmutableList<string> Errors { get; private set; } public PlaceOrderService(IRequestCookieCollection cookiesIn, IResponseCookies cookiesOut, EfCoreContext db) { _db = db; _checkoutCookie = new CheckoutCookie(cookiesIn, cookiesOut); } /// <summary> /// This creates the order and, if successful clears the cookie /// </summary> /// <returns>Returns the OrderId, or zero if errors</returns> public async Task<int> PlaceOrderAsync(bool tsAndCsAccepted) { var checkoutService = new CheckoutCookieService(_checkoutCookie.GetValue()); var action = new PlaceOrderAction(new PlaceOrderDbAccess(_db)); var runner = new RunnerWriteDbAsync<PlaceOrderInDto, Order>(action, _db); var order = await runner.RunActionAsync( new PlaceOrderInDto(tsAndCsAccepted, checkoutService.UserId, checkoutService.LineItems)); Errors = action.Errors; if (action.HasErrors) return 0; //successful so clear the cookie line items checkoutService.ClearAllLineItems(); _checkoutCookie.AddOrUpdateCookie( checkoutService.EncodeForCookie()); return order.OrderId; } }
You can see the PlaceOrderAction instance being created, along with its linked PlaceOrderDbAccess class. This would normally be done by Dependency Injection, but to make the example simpler to understand I have created it by hand.
You can see the adapter pattern in progress. On input the information is contained in a cookie, but the business layer shouldn’t have to deal with that. Therefore, the Service Layer method PlaceOrderAsync converts the cookie content into the form that the PlaceOrderAction wants.
Similarly, for the output of the PlaceOrderAction Business Logic, which returns an Order instance, but the presentation layer actually wants the OrderId, Again the PlaceOrderAsync method extracts the OrderId from the Order and returns that to the MVC Action.
Interestingly the pattern of returning a database class instance is quite typical, as the Business Logic won’t know what the entity’s key is, as it isn’t generated until the BizRunner in the Service Layer calls EF’s SaveChanges. It then becomes the job of the Service Layer to extract/adapt the parts that are needed by the Presentation Layer.
NOTE: The BizRunner and the code above works fine, but is simpler than what I use in practice. I have a private library called GenericActions, which has the same sort of features as my GenericServices library. This can identify the right signature of BizRunner to call by unpacking the Interface parts. GenericActions also uses the same ISuccessOrErrors<T> class as GenericServices, which returns both the status/errors and the class being returned. It also has the AutoMapper mapping built in to do any needed adapting.
Presentation layer
For completeness I have included the presentation layer, which in this case is an ASP.NET Core MVC page. If the order is successful it redirects to the confirmation page, otherwise it copies the errors into the ModelState and shows the basket again, with the error message(s).
public async Task<IActionResult> PlaceOrder(bool iAcceptTAndCs) { var service = new PlaceOrderService( HttpContext.Request.Cookies, HttpContext.Response.Cookies, _db); var orderId = await service.PlaceOrderAsync(iAcceptTAndCs); if (!service.Errors.Any()) return RedirectToAction("ConfirmOrder", "Orders", new { orderId}); //Otherwise errors, so copy over and redisplay foreach (var error in service.Errors) { ModelState.AddModelError("", error); } var listService = new CheckoutListService(_db, HttpContext.Request.Cookies); return View(listService.GetCheckoutList()); }
I try to keep my Controller Actions small and simple by putting as much logic as possible into the Service Layer or lower layers. I do this because it is hard to test Controller actions, but much easier to Unit Test the Service Layer or lower layers.
Quick aside – handling errors needs thought.
This article is already quite long, so I won’t dwell on this subject, but providing good error feedback to the user is not trivial. Many libraries use Exceptions to report errors – some of these errors are user friendly, like Validation Errors which (by default) are found by EF when SaveChanges is called. However, many Exceptions, such as EF’s DbUpdateException, are very unfriendly and sometimes produce messages that relate to the inner working for the application, and hence should not be shown for security reasons.
In this article I have implemented a very simple error feedback mechanism. In practice you would need to implement a much more comprehensive approach. I suggest you look at my open-source library, GenericServices, with uses ISuccessOrErrors<T> to return a result with possible error messages.
Conclusion
Writing CRUD (Create, Read, Update and Delete) methods used to take a lot of (boring) development writing boiler-plate code. I solved that by writing a library called GenericServices, which has radically improves the speed of development (see this site CRUD-only site, which I wrote in about 10 days).
I now find I spend most of my development effort on a) the Business Logic and b) building responsive user-interfaces. Therefore, continuing to improve my approach to Business Logic writing is well worth the effort, which is what this article is all about. (See a different article on how I am improving my building responsive user-interfaces too).
I recently reviewed an e-commerce application I developed which used the older approach to writing Business Logic, i.e. EF called in the Business Logic. The Business Logic was quite complex, especially around pricing and delivery. My conclusion was that the Business Logic work fine, but it was sometimes difficult to find and understand the database accesses when refactoring and performance tuning the Business Logic.
I tried out the new approach described in this article by refactoring some of the Business Logic in this e-commerce application over to the new approach. Overall it did make a difference by bring all the EF access code into one clear group per Business Logic case.
In additions I wrote an example application using ASP.NET Core and EF Core with this new approach, again with good results (this is where the example code comes from in this article). Couple this with the other positive feature of being able to test the Business Logic with a Mock DbAccess class rather than accessing EF directly and it is clearly a good step forward.
The only down side to this new approach is that does need one more class than the previous approach. However, that class does provide a much better separation of concerns. Therefore, I think this new approach is a positive improvement to my previous approach to building Business Logic and is worth the effort extra effort of another class.
I hope this article gives you some ideas on how to better design and build complex Business Logic in your applications.