简体   繁体   English

Asp.net Mvc:使用LINQ to SQL创建模型类

[英]Asp.net Mvc: Creating Model Classes with LINQ to SQL

I am trying to learn Asp.net Mvc so I am trying out this Tutorial. 我正在努力学习Asp.net Mvc所以我正在尝试这个教程。

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. 他们讨论了存储库模式以及如何轻松地更改为另一种数据访问技术,而不是直接将Linq调用到Sql。

Using LINQ to SQL within a controller class makes it difficult to switch data access technologies in the future. 在控制器类中使用LINQ to SQL会使将来很难切换数据访问技术。 For example, you might decide to switch from using Microsoft LINQ to SQL to using the Microsoft Entity Framework as your data access technology. 例如,您可能决定从使用Microsoft LINQ切换到使用Microsoft Entity Framework作为数据访问技术。 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. 注意:在阅读本教程之前,我从未真正理解界面是如何工作的,并且它仍然不是100%清晰。 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. 在成功使用Linq to Sql之后,我认为尝试Ado.net实体框架会很好,因为我已经阅读了很多相关内容。 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? 我的问题是: 如何更改为Ado.net EF?

This is what I think I should do. 这是我认为我应该做的。

  • Add the Movie.edmx file and configure it(add my movie table). 添加Movie.edmx文件并对其进行配置(添加我的影片表)。
  • Write a new class based on the IMovieRepository and maybe call it MovieEFRepository. 基于IMovieRepository编写一个新类,并将其称为MovieEFRepository。
  • Change the parameter in the controller constructor to MovieEFRepository. 将控制器构造函数中的参数更改为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? 我是否需要更改每个控制器,因为我指定了MovieRepository类?
  • 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. 因为它使用的是由Linq to Sql设计器创建的Product类。 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? 我想我应该使用一些不依赖于dbml文件的对象?

Forgive me if I have a slightly simplistic view of Asp.net Mvc. 如果我对Asp.net Mvc略微简单化一下,请原谅我。 I am webdesigner with a lot of interest for Asp.net webdevelopment. 我是webdesigner,对Asp.net webdevelopment很感兴趣。

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. 首先,我试图找出IoC(控制反转)实际意味着什么。

One of the first sites I found was a website with a screencast about Unity. 我找到的第一个网站之一是一个关于Unity的截屏网站。 Which is a DI/IoC framework for .Net. 哪个是.Net的DI / IoC框架。

Looking at it now this is actually a very good screencast and example on how easy it is to use Unity and IoC/DI. 现在看一下这实际上是一个非常好的截屏视频,并说明使用Unity和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. 我一直遇到的一个网站是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. 我读过的下一个网站是关于温莎城堡的,因为AlfredoFernández说它很容易使用。

The documentation wasn't to bad but I had some problems converting their "getting started" basic example to my Asp.net Mvc application. 文档并没有坏,但我在将他们的“入门”基本示例转换为我的Asp.net Mvc应用程序时遇到了一些问题。 Also part 2 and 3 were missing from their getting started. 第2部分和第3部分在开始时缺失。

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. 我有很多xml配置文件的问题,我似乎无法让它们工作。 I tried Windsor, Structure Map and Spring.net but I always got stuck with the xml files. 我试过Windsor,Structure Map和Spring.net,但我总是遇到xml文件。

So I decided to go to the Asp.net Mvc site because that's where I started learning about Asp.net Mvc. 所以我决定去Asp.net Mvc网站,因为那是我开始学习Asp.net Mvc的地方。 I found the first screencasts and MIX09 presentations very clear and I understood most of what people were talking about. 我发现第一次截屏和MIX09演示非常清楚,我理解人们谈论的大部分内容。 I got stuck at the second screencast by Rob Conery when building the Storefront application. 在构建Storefront应用程序时,我被Rob Conery的第二次截屏视频困住了。 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. 因为我对存储库和IOC / DI了解得更多,所以我认为再次开始观看Rob Conery的截屏会是一个好主意。 In one of the screencasts he talks about uploading all the samples to codeplex. 在其中一个截屏视频中,他谈到将所有样本上传到codeplex。

I went to codeplex and found out you can browse through the source files without downloading them. 我去了codeplex,发现你可以浏览源文件而无需下载它们。 I tried to find out how Rob Conery handles IOC/DI with his repositories. 我试图找出Rob Conery如何使用他的存储库处理IOC / DI。 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. 所以我很高兴看到他正在使用Structure Map,但他没有使用xml配置文件,而是使用了一个引导程序类,它将所有接口注册到它们的具体类。

After trying this with my webapplication I finaly was able to get Structure Map to work in my application (Hooray). 在使用我的webapplication尝试这个之后,我最终能够在我的应用程序中使用Structure Map(Hooray)。

He also showed me how to fix the dependency on my Product class that comes from Linq to Sql. 他还向我展示了如何修复从Linq到Sql的Product类的依赖性。 He creates an extra object that then gets called by "select new product { }" in the Linq queries. 他创建了一个额外的对象,然后通过Linq查询中的“选择新产品{}”进行调用。

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. 哇,这个答案比我的计划要长一点,但我希望这能帮助像我这样非常喜欢编码的人和Asp.net Mvc。

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. 我建议你阅读有关IOC的内容,简单而且非常有趣,你可以像城堡windsor一样使用和ioc容器。

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. 使用您的实体,您可以使用控制器执行相同的操作,为每个实体创建一个界面并使用ioc模式,您只需更改ioc容器的配置文件即可。

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! 抱歉我的英文!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM