0

I'm building a project with C# AspNet.Mvc. I'm using Entity Framework Core. It has Poco which maps in data from DB. On Mvc project itself i've got ViewModels folder where i have models topass between controller and view. But how should i organize models on web to map from POCO entities. So far i was keeping them in the same ViewModels folder which is a bit confusing whether it holds data from Poco or pass data from controller to View. Thanks you al in advance.

3
  • 1
    There's no right or wrong way - if you find it confusing to have them in the same folder, then why not split them out into a different folder? Commented Nov 8, 2020 at 9:59
  • 1
    For the close voters: OP's approach may have what some would consider to be beginner mistakes, but that does not detract from the validity of the question itself. Having mistakes doesn't warrant closure, it warrants answering the question.
    – Flater
    Commented Nov 9, 2020 at 13:38
  • Flater's answer is correct, there are options for extensibility based on whether or not you think some other app might want to share the same POCOs you are writing now, but if you don't have any current requirement to do that, then just put 2 new folders into your project (1 for services for biz logic, and 1 for those POCOs) and add the models and classes there. That way you can easily move them out into their own projects later if needed.
    – GHP
    Commented Nov 9, 2020 at 13:55

1 Answer 1

2

You will find that the predominant clean coding advice will advocate (or implicitly assume) that you have separated your codebase into several layers. The three most commonly used layers are the presentation (in your case, the MVC project), services (where your business logic resides), and persistence (in your case, your EF Core logic).

So the answer to your question on how to store your data logic classes inside your MVC project is that you don't store them there. They reside in their own project.

Note that depending on your specific architecture, which projects/layers you use will slightly vary. In the simplest incarnation, your EF entities live inside the persistence layer.

I don't think this is the place for me the explain multi-tiered layering from scratch. Plenty of resources exist online on this precise topic, and they'll do a better job of explaining it fully than I can. Which resources you use also depends on how deep you want to delve into this.
Here's my personal favorite, but as I said there are plenty of other resources online.


That being said, it's not impossible to keep everything in the same project. It's not a good idea, and you're not going to find many developers even willing to discuss it as a viable option, but it's technically not impossible.

Even if you did to this, I would still advocate using the same separation (presentation/services/persistence), but using project folders instead of projects. Keep your presentation, services and persistence completely separate. Nothing good will come of lumping them together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.