2

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 patterns 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 ?

Rasto
  • 17,000
  • 40
  • 141
  • 232
  • 1
    I think you've got something confused with the using keyword, as all it does is make sure that Dispose is called, and using DI helps you Unit Test your controller, as you can pass in a mock context. There are also other advantages to DI such as decoupling etc. – Linkgoron Mar 24 '11 at 22:54
  • 1
    Well, dependency injection would allow you to pass the "data layer" into the constructor instead of having a bunch of `new ...` everywhere. That lets you decouple the how the "data layer" is created from where it's used (which as Linkgoron pointed out, helps with unit testing). – R0MANARMY Mar 24 '11 at 22:57
  • Thank you both now I think I understand this part. – Rasto Mar 24 '11 at 23:03
  • 1
    Some good basics here as well: http://stackoverflow.com/questions/2503442/architecting-asp-net-mvc-app-to-use-repositories-and-services – BlackICE Mar 24 '11 at 23:16

2 Answers2

2

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.

Community
  • 1
  • 1
Robert Koritnik
  • 97,460
  • 50
  • 267
  • 388
  • @Robert Koritnik Dear Robert, to react to the first part of your answer - that application was poorly designed by myself at the first place. There's no other person that is going to refractor it, secondly. And yes, it is meant to be deployed at the end. Well it might seem that I don't understand basic and it also might be true. I've never done anything real for web environment (except of few rather static pages). And I'm not .net expert (more into Java) either. I think I'm quite good desktop programmer mostly when it comes to GUI and non trivial algorithmic problems. But I'll have to figure... – Rasto Mar 25 '11 at 10:55
  • ...out how to do this because there is no one else to do it. MVC is completely new way to me - as I said as desktop programmer I'm used to events and I should probably use web forms - but I don't like them I think they are not natural for web environment and I don't feel like having control over application with them. So catch up with patterns is what I need right now. – Rasto Mar 25 '11 at 11:00
  • @Robert Koritnik As to the second part - the problem is that I'm not home right now but in different country and I'll be for some time. I don't know anybody here that would be able to explain me the think properly not even an analyst - don't even speak the language that si spoken here. So I'm trying to learn from web but that causes this kind of strange questions... Thank you for that link to your answer anyway. – Rasto Mar 25 '11 at 11:21
  • 1
    @drasto: Ok so I understand the background of bad design. I'd like to point you to the first link I provided as well. You'll find example Asp.net MVC applications there which will help you lots in getting up to speed with Asp.net MVC development. And since you're Java developer: have you considered JSP for this project maybe? It would probably be easier for you even though you're a desktop developer otherwise. And just an idea: even though Asp.net MVC is very well suited to the HTTP protocol, you shouldn't dismiss web forms completely. Lots of applications work with it rather well. – Robert Koritnik Mar 25 '11 at 11:43
  • @Robert Koritnik I know the fist link you provided very well :) Particularly interesting is that e-commerce application video series but I did not really watch all of it because they are making thinks too complicated for my purpose - I don't do extensive testing. And the videos are also wast of time sometimes - they speak about not important or clear thinks for too long. I have considered JSP... But the learning curve there is even worse the in MVC, I'm not as productive in Java as in C# (faster syntax), tooling for .net is better(EF)and faster to learn. Besides I have bad experience with JSP. – Rasto Mar 25 '11 at 12:02
  • @Robert Koritnik Well I've started with MVC, I already know how to do a lot of staff and I'd have to learn everything in web forms again. Besides I'want to have quite some custom client side code that is supposed to be hard to handle in web forms... I hope I've made right decision. I've spent whole day reading about pos/cons so I should have. – Rasto Mar 25 '11 at 12:08
  • 1
    @drasto: If you're developing a *fat client* they you did make the correct decision by doing it in MVC. That's fine. All you have to do is create your **model**, **service**, and **data** layer for better SoC. And then put in code that actually belongs into each of them. – Robert Koritnik Mar 25 '11 at 12:14
  • @Robert Koritnik I think I understand all I needed about typical tiers architecture in MVC and how tiers interact after rearing some more resources. Thank you – Rasto Mar 25 '11 at 12:18
1

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).

Community
  • 1
  • 1
Nima
  • 947
  • 1
  • 13
  • 20
  • add " I will suggest that you keep your data layer in a separate assembly (project)." Already doing that. – Rasto Mar 25 '11 at 11:02
  • @Drasto - Well, that is good. I really suggest that you look into using a IoC like Unity, for use with e.g. your repositories and service classes. It gives a nice structure, and a very flexible architecture. – Nima Mar 25 '11 at 11:20