Ok, I'm trying to rewrite huge part of code in this ASP.NET MVC application that was poorly designed. I've read a lot about many concepts and pa开发者_StackOverflow社区tterns some of them were little more advanced but I found myself to be confused about very basic staff.
So what should be overall structure of MVC application ?
I have something that wraps data access - lets call it "data layer". It might be implementation of Repository pattern or just some Entity framework context class or...
Then I'll have services layer wrapping business logic.
What should scenario of serving http request ? Controller gets created for the request --> action on it is executed. And then ? I create instance of "data layer" and pass it to the methods service layer commit changes and display results ?
So each method in all services (belonging to service layer) should take some "data layer" class as parameter so it can talk to "data layer" ? Or do I pass "data layer" reference to services in constructor ? In that case I have to create new service for each request right ?
I've read about using using
keyword in controller to pass "data layer" reference to services. The other approach is using dependency injection. I don't really understand the difference... And why not just create it in controllers constructor and keep it simple ?
The actual answer to your problem
No offence @drasto, but it seems you're not the person to refactor poorly designed existing application because you're struggling with rather basic things yourself. At least you shouldn't be doing it on an application is meant to be deployed.
The help
It would be better to talk to some development analyst that's done EF/Asp.net MVC/repository/service pattern and knows how they talk to each other and how they should be implemented. The good thing is that you may learn a lot this way and learn it in the way that is right. Why would you reinvent the whole wheel.
You can also learn from examples presented on http://www.asp.net/mvc which will show you how things should be done step by step with nice application architecture.
I would only like to tell you that you rather learn the concepts first before some boss starts giving you the hard time. No bad thing admitting you don't know enough of the stuff as long as you're eager to learn.
I've set up a simplified example of an Asp.net MVC application architecture in one of my answers as well which presents these concepts in a very superficial level. but may be helpful as well.
In order to get you in the correct direction, I would take a look at my previous question, which might get you some ideas of MVC structure using dependency injection (with UNITY) and a simple usage of the repository pattern.
MVC, EF - DataContext singleton instance Per-Web-Request in Unity
In my example, the controllers get "injected" the needed dependencies, in this case the needed repositories. After handling the request, the controllers are then disposed together with the repositories.
Furthermore, I will suggest that you keep your data layer in a separate assembly (project).
精彩评论