views:

531

answers:

2

Hello,
I am trying to learn Asp.net Mvc so I am trying out this Tutorial.

They talk about the Repository Pattern and how it is easy to change to another data access technology instead of just calling Linq to Sql directly.

Using LINQ to SQL within a controller class makes it difficult to switch data access technologies in the future. For example, you might decide to switch from using Microsoft LINQ to SQL to using the Microsoft Entity Framework as your data access technology. In that case, you would need to rewrite every controller that accesses the database within your application.

Note: I never really understood how an interface worked before reading this tutorial and it's still not 100% clear. I see it now as some sort of 'template' for a class.

After successfully using Linq to Sql I thought it would be nice to try out Ado.net Entity Framework since I've been reading a lot about this. They said using the Repository Pattern would make it easy to switch so I thought I would test that.

My question is: what should I do to change to Ado.net EF?

This is what I think I should do.

  • Add the Movie.edmx file and configure it(add my movie table).
  • Write a new class based on the IMovieRepository and maybe call it MovieEFRepository.
  • Change the parameter in the controller constructor to MovieEFRepository. This is the first thing I find strange because in the tutorial they say that not using the repository will force you to change all the controllers if you change to an other datasource. Don't I need to change every controller anyway since I am specifying the MovieRepository class?
  • The last adjustment I think I need to do is to change the View. Because it's using the Product class which was created by the Linq to Sql designer. I am not sure how I am going to do this. I guess I should have used some object that wasn't dependent on the dbml file?

Forgive me if I have a slightly simplistic view of Asp.net Mvc. I am webdesigner with a lot of interest for Asp.net webdevelopment.

+1  A: 

You might have your repository decoupled because of injection, not if you followed just the examples because of

public MoviesController() : this(new **MovieRepository**())

I recomend you to read about IOC, is easy and very interesting, you can use and ioc container like castle windsor.

With that, your contoller will have only one constructor, the one with the interface, and not will need to be changed.

With your entities you can do the same that with the controllers, create an interface for each entity and use the ioc pattern too, with tha you will only have to change your configuration file for your ioc container.

If you don't do these things, your right, you will need to change all you said.

I hope that help! sorry about my english!

Alfredo Fernández
Hello thanks for the answer. I am reading about IOC (which surprisingly doesn't mean International Olympic Committee!) and it seems that's the way to go. How I understand IoC is that you can tell it to use a specific class for a certain Interface and because the settings are stored in a separated file it's easy to make global adjustments. The only thing that bothers me is that i need an extra framework for IoC and also that there is more than one of those. Anyway thanks again for the answer.
Pickels
+4  A: 

So after a few days of reading and a lot of googling I got it to work. First I tried to find out what IoC (Inversion of Control) actually meant.

One of the first sites I found was a website with a screencast about Unity. Which is a DI/IoC framework for .Net.

Looking at it now this is actually a very good screencast and example on how easy it is to use Unity and IoC/DI. At the time I didn't understand it completely so I went on and kept googling.

One website I kept running into was the one from Martin Fowler.

For me, a person that is a coding novice this website is a little to abstract. Also this might sound weird but the font, line-height and typography on that website was really awful which made it even harder to read.

The next website I read was about Windsor Castle since Alfredo Fernández said it was easy to use.

The documentation wasn't to bad but I had some problems converting their "getting started" basic example to my Asp.net Mvc application. Also part 2 and 3 were missing from their getting started.

After this I started looking for the different frameworks to see if i could find a really basic example. If I just looked at the first screencast again I would have fixed it a lot sooner but somehow I lost track of it.

I had a lot of problems with xml configuration files and I couldn't seem to get them to work. I tried Windsor, Structure Map and Spring.net but I always got stuck with the xml files.

So I decided to go to the Asp.net Mvc site because that's where I started learning about Asp.net Mvc. I found the first screencasts and MIX09 presentations very clear and I understood most of what people were talking about. I got stuck at the second screencast by Rob Conery when building the Storefront application. Because I knew a little more about repository and IOC/DI now I thought it would a be a good idea to start watching Rob Conery's screencasts again. In one of the screencasts he talks about uploading all the samples to codeplex.

I went to codeplex and found out you can browse through the source files without downloading them. I tried to find out how Rob Conery handles IOC/DI with his repositories. So I was glad to see he was using Structure Map but instead of using a xml configuration file he was using a bootstrapper class that registers all the interfaces to their concrete class.

After trying this with my webapplication I finaly was able to get Structure Map to work in my application (Hooray).

He also showed me how to fix the dependency on my Product class that comes from Linq to Sql. He creates an extra object that then gets called by "select new product { }" in the Linq queries.

Wow, this answer is a little longer than I planned but I hope this helps other people like me who are very novice in coding and Asp.net Mvc.

Pickels