Reputation: 4872
I'm making a website using mvc.net 4 which will connect to a database via entity framework 6.
I've been following
and they suggest splitting up an application into data layer, business entities, business logic and web app.
Now, since I'm making a simpler application I was hoping to simplify from making 4 projects and instead make a data and business logic layer called "Business" which connects to a database in this case and expose the data via API which will be in the "Business" layer which will incorporate the other 3 (Business logic, business entities and data layer).
What bothers me is that entity framework generates entities as public so it's an obvious problem since I want it to stay hidden from the web app.
Should I make all the entities private or make a project between the EF6 generated entities and the "Business" layer?
Upvotes: 0
Views: 877
Reputation: 34642
I believe you're getting a bit confused about the design and separation of concerns versus the access modifiers on the classes.
To take a step back and get a high-level overview, first look at your typical application structure.
With that in mind, you mention that you are using Entity Framework. Entity Framework is an ORM which maps your objects to incompatible relational types creating sort of a "database in objects" (or a "virtual object database" as Wikipedia puts it). One thing to keep in mind here is that EF is just how you are managing your data objects within your system. It makes life easier in many ways, but it is not the actual data. The fact that the classes are "public" is a requirement of EntityFramework, yes, but it does not mean that you need to allow access to or use those data objects in other parts of your project (although, there is a caveat to that which I will get to in a moment).
So, at this point, you could create an application that might look like this:
My caveat from before - please note that the next layer up does need to know about the layer below it. If you think about that logically, how are you going to create a business layer if you have no data to build off of? But, the view layer should not be directly touching your data objects directly and your business logic should (generally) not be using the data objects to make business decisions. There is a sort of conversion step in moving between the layers.
If that is all clear, then, back to what you are asking. How much you split out or don't split out the layers is really up to you as the designer. I have seen the data layer sit directly in the same project as the business logic - particularly if there is no real business logic. Heck, I have seen a very simple application put everything in a single WebApp project. You have to think through what your project currently needs and what the benefits and negatives are if you design things a certain way.
Some things to think about:
There are lots of other articles out there on this type of design. I would recommend hitting up Google to read more. Hopefully this clarifies things a bit!
Upvotes: 3