【问题标题】:Where does my CRUD LINQ Code Go? ASP.NET MVC我的 CRUD LINQ 代码在哪里? ASP.NET MVC
【发布时间】:2011-05-15 15:42:55
【问题描述】:

我目前正在一个项目中使用 ASP.NET MVC 框架(几乎是我的第一次)

我使用 Linq2SQL 作为我的数据模型..

我应该在哪里有这种代码:

var entries = from e in db.sometable select e;

我目前在控制器中有这样的代码,并将我得到的数据传递到视图中..

这样好吗?

如果不是,我如何让我的 linq2sql 数据模型包含这种代码?

谢谢

丹尼尔

【问题讨论】:

    标签: asp.net-mvc model-view-controller separation-of-concerns


    【解决方案1】:

    要添加@Poco 所说的内容,这是一个示例:

    在Foo.Common.Repositories(在Foo.Common 项目内):

    public interface IRepository<T>
    {
        IEnumerable<T> GetAll();
        void Update(T entity);
        void Add(T entity);
        void Delete(T entity);
        void Save();
    }
    
    public interface IUserRepository : IRepository<User>
    {
        void GetByCredentials(string username, string password);
    }
    

    内部Foo.Data.Repositories(内部Foo.Data项目):

    public class UserRepository
    {
        // ... other methods/properties snipped.
    
        public IEnumerable<User> GetAll()
        {
            // Where this.Users would be L2Sql or Code-First... or some other ORM.
            return from u in this.Users orderby u.Name select u;
        }
    }
    

    然后在你的实际Foo.Web:

    public class UserController : Controller
    {
        private readonly IUserRepository userRepository;
    
        public UserController(IUserRepository userRepository)
        {
             this.userRepository = userRepository;
        }
    
        [AcceptVerbs(HttpVerbs.Get)]
        public ViewResult List()
        {
            var users = this.userRepository.GetAll();
            return this.View(users);
        }
    }
    

    在您的 Global.asax 中,您将拥有 Ninject 或其他一些 IoC 容器来解析 IUserRepository:

    public static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IUserRepository>().To<UserRepository>();
    }
    
    protected void Application_Start()
    {
        var kernel = new StandardKernel();
    
        AreaRegistration.RegisterAllAreas();
    
        MvcApplication.RegisterGlobalFilters(GlobalFilters.Filters);
        MvcApplication.RegisterRoutes(RouteTable.Routes);
        MvcApplication.RegisterServices(kernel);
    
        // I'm using MVC3 here:
        DependencyResolver.SetResolver(new NinjectResolver(kernel));
    }
    

    【讨论】:

      【解决方案2】:

      在控制器方法中有 Linq 查询很好。

      如果我们在谈论关注点分离,那么想法是您的数据层(在这种情况下,为您提供 db.sometable 的存储库(?)代码)解耦您的逻辑代码(在这种情况下是控制器方法)从数据存储。

      您查询数据层而不是数据库,因此您可以更改底层数据存储,并且您的控制器代码仍然可以工作。

      有些人认为最好还是将尽可能多的逻辑代码移出控制器并移入模型代码(请参阅the first answer here),但这取决于您想走多远。

      【讨论】:

        【解决方案3】:

        这是一个如何实现repository pattern的示例

        除此之外,我将实现一个附加层来处理您的应用程序业务逻辑并保持您的控制器轻量级

        【讨论】:

          【解决方案4】:

          在 MVC 中使用 Repository 模式是很常见的。 通常,您定义一个接口,例如 IProducts,然后实现该接口,调用您的 linq2sql 代码。您的控制器将接受这个接口作为构造函数的参数,因此它依赖于这个接口,而不是具体的类。使用依赖注入器,例如 Ninject,将允许您向构造函数提供具体的接口实现。这可以在您的 Web 应用上启用单元测试,并增加灵活性。

          有一本非常好的书,Pro ASP.NET MVC 2 Framework,它解释了所有这些。我目前正在阅读它,我就是喜欢它。

          【讨论】:

            猜你喜欢
            • 2012-03-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多