我应该在后面的代码中直接使用 Linq To SQL 还是使用其他方法? [英] Should I use Linq To SQL directly in code behind or use some other approach?

查看:12
本文介绍了我应该在后面的代码中直接使用 Linq To SQL 还是使用其他方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在用 ASP.NET/C# 开发一个项目,这不是一个非常大的项目,而是一个相当大的项目.目前我们已经开发了几个页面.我现在是从单个页面的角度说话.到目前为止,每个页面都遵循这种方法.

We are developing a project in ASP.NET/C# which is a not a very large project but a sizeable one. Currently we have developed few pages. I am talking from point of view of a single page right now.The approach is followed for every pages that has been developed so far.

在我的页面后面的代码中,我们直接使用 Linq To SQL 查询.插入操作完成,queries填充dropdownlists和其他database相关操作在其后面的代码中使用.

In the code behind of my page we use Linq To SQL queries directly. The insert operation is done , queries to fill dropdownlists and other database related operations are used in code behind itself.

虽然我们使用函数.其他页面也是如此.

We use functions though.The same goes for other pages as well.

我的问题是我应该将它们包含在 class 文件中,然后创建 objects 并调用适当的方法来完成我的工作吗?

My question is should I include them in class files and then create objects and call appropriate methods to do my stuff?

如果是,我们应该创建一个 class 还是每页创建一个类.这是否称为创建数据访问层.

If yes, should we create a single class or create one class per page. Is this called creating Data Access Layer.

谁能帮我建议一个正确的方法来做到这一点?

Can anyone help me suggest a proper way to do this?

这种方法是否是一种好的编程习惯.

Is this approach a good programming practice.

这是我们在后面的代码中使用的一个简单函数

This is a simple function that we are using in our code behind

public void AccountTypeFill()
        {
            //Get the types of Account ie Entity and individual
            var acc = from type in dt.mem_types
                       select type.CustCategory;
            if (acc != null)
            {
                NewCustomerddlAccountType.DataSource = acc.Distinct().ToList();
                NewCustomerddlAccountType.DataBind();  
            }
        }

谁能指出一个简单的例子来引用这个查询?

Can anyone point a simple example referring to this query?

我希望我的问题有意义.欢迎提出任何建议.

I hope my question makes sense. Any suggestions are welcome.

推荐答案

不要在代码隐藏中硬编码.使用类而不是每个页面一个类的问题.

Don't hard-code in the code-behind. Use Classes and it is not a matter of one class for each page.

这些链接将有所帮助:

在 LINQ to SQL 中设计数据访问层

LINQ to SQL 生成的对象可以解耦吗?

点击按钮时调用的方法代码放在哪里?

LINQ to SQL 和存储库模式

使用 C# 将业务逻辑层与用户界面层解耦

这篇关于我应该在后面的代码中直接使用 Linq To SQL 还是使用其他方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆