I have a project on Github that I am trying to develop that shows how to use best practices in a practical way. However I am having problems with the concepts. When I do, I go right back to basics and try a simple example. To see the project follow this link:
https://github.com/franasm/Store.git
The working branch has DI/IOC fully working. The broken branch is what happened when I tried to add a simple business layer. So I went back to basics.
I created a bog standard simple MVC4 application. I then added EF5 with a single table (the product table from the project above).
I decided to add a business layer without any DI/IOC or any fancy stuff. But the concept just confuses me.
All I want to do in this example, is make a class that extends the product class, add a property called TaxAmt, and add a method to calculate the TaxAmt when the class is instantiated.
I tried a variety of ways but get all kinds of errors. From stack overflow execptions, to instance not set exceptions. Most of the time, the thing just doesn't display a value because the Product.Price field is not initialised.
I also find that my BL is in the wrong place:
I have my model,
then my BL,
then my controller.
Fine for this small example. But because the closest to a working example I can get is to create a partial class of the already existing class generated by EF, when I split this out to use DI/IOC the BL is going to be on the wrong side of the interfaces because it will be a part of the Model class (partial class).
I would like to have my layers clearly separated like:
EF Model class,
Interface,
Repo,
BL,
Controller,
ViewModels,
Views.
Because the Repo is responsible for the crud operations. The BL should build on this. After the product details (one of which is price) has been read, the BL can then take this information, do its tax calculation, and then the controller can use this.
Im not sure if my thinking is correct on this.
For the most part I do strongly disagree that the BL should be part of the model. But even if this were to be the case for my contrived example as described above. I still cant get it to work. I would however be looking for a solution to this that would look toward inheriting the model class, extending it, then using it alongside the repo. But this leads me in circles again. The crud gets done by the repo layer, which does not have access to the BL, so how does the BL get initialised in order to do the tax calc. Am I not doubling up on objects, a POCO for each model instance, then also a BL instance of the same object.
Im really confused...
Essentially, I have not included code because all I literally done was make a simple table, a simple mvc project, added EF5 model, wired it all up to do the crud operations and ensure it was working. Then attempted to add a BL as described. A single class in a BL folder that either was defined as a partial class of the model (not ideal), or inherited the model class directly (also not sure if this is cool). Ideally I would like to be able to use DI, to inject my repo into my BL, and then my BL into my controller.
If anyone knows how to do this, can you please provide me with an answer, either by providing step by step instructions (and preferably some sample code) or an explanation of how to go about this process and why I am getting so confused.
Thanks in advance for any help.