简体   繁体   English

ASP.NET中的类App_Code中的局部类

[英]Class in ASP.NET Partial Class in App_Code

I created a class for connection and using with register.aspx without any problem. 我创建了一个用于连接并与register.aspx一起使用的类,没有任何问题。 When i try to move codes from register.aspx.cs to regpartial.cs then i get an conflict error: "connection conn = new connection();" 当我尝试将代码从register.aspx.cs移动到regpartial.cs时,出现冲突错误:“ connection conn = new connection();”

I would like to move codes register.aspx.cs to mypartial.cs. 我想将代码register.aspx.cs移到mypartial.cs。 I think it will be better but i'm not sure how can i solve conflict problem. 我认为这会更好,但是我不确定如何解决冲突问题。

Register.aspx.cs (final) Register.aspx.cs(最终)

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using test.App_Code;


namespace test
{   
    public partial class register: System.Web.UI.Page
    {
        private void Page_Load(object sender, EventArgs e)
        {

        }

Connection.cs (final try) Connection.cs(最终尝试)

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace test.App_Code
{
    public class connection
    {

        public SqlConnection connect()
        {
            SqlConnection conn= new SqlConnection("Data Source=******;Initial Catalog=****;Integrated Security=False;User Id=****;Password=*****;MultipleActiveResultSets=True");
            baglanti.Open();
            return (conn);
        }
}

regpartial.cs (Final try) regpartial.cs(最终尝试)

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace test.App_Code.partials
{
    connection conn = new connection();
    public partial class regpartial : register
    {

    }
}

In general you shouldn't have much code inside your aspx.cs files. 通常, aspx.cs文件中不应包含太多代码。

You want to separate the logic that is directly your view/presentation logic (stuff that makes the .aspx pages work) from your business logic (sequencing, transformation, validation), and finally you want to have your Data Access / Resource Access to be isolated. 您想将直接是视图/表示逻辑(使.aspx页面起作用的东西)与业务逻辑(排序,转换,验证)分开的逻辑,最后要使数据访问/资源访问成为孤立。

I'd also recommend using Dapper.NET over raw ADO.NET/SqlConnection. 我也建议使用Dapper.NET通过原始ADO.NET/SqlConnection。

I'm going to string together a very basic example of how you can separate this. 我将整理一个非常基本的示例,说明如何将其分开。 This is not guaranteed to compile c# code but it will very close pseudocode. 这不能保证编译c#代码,但是会非常接近伪代码。

Inside registration.aspx.cs 内部registration.aspx.cs

private void btnRegister_Click(object sender, EventArgs e)
{
    var email = txtEmail.Text;
    var password = txtPassword.Text;

    var registration = new Registration { Email = email, Password = password }

    var bizService = new RegistrationService();

    var response = bizService.Register(registration);

    if(response.Success) Response.Redirect("~/registration/success");

    ltlError.Text = response.FailureMessage;

}

RegistrationService.cs RegistrationService.cs

public class RegistrationService {

    public RegistrationResponse Register(Registration req)
    {
        var regDAL = new RegistrationAccess();

        var isEmailDuplicated = regDal.DoesEmailExist(req.Email)

        if(isEmailDuplicated) 
              return new RegistrationResponse { 
                    Success = false,
                    FailureMessage = "Email exists, did you mean to login instead? 
                }

        regDAL.InsertNewRegistration(req)

        return new RegistrationResponse { Success = true };   
    }
}

Lastly you should have a RegistrationAccess.cs that contains only code for reading and writing to SQL Server / other database / file system. 最后,您应该有一个RegistrationAccess.cs,其中仅包含用于读取和写入SQL Server /其他数据库/文件系统的代码。

Note how the aspx.cs file has no knowledge of RegistrationAccess. 请注意aspx.cs文件如何不具有RegistrationAccess的知识。 Your view should not be directly calling the database. 您的视图不应直接调用数据库。 The other thing to note is that RegistrationService has no knowledge of the view. 要注意的另一件事是RegistrationService不了解该视图。 It receives a Registration type, then executes business logic and calls out to the DAL. 它接收一个注册类型,然后执行业务逻辑并调出DAL。 The DAL class will have zero knowledge of both the view and the RegistrationService, it will know only one thing, the database. DAL类对视图和RegistrationService的知识为零,对数据库一无所知。

This is the basis of a very simple ASP.NET webforms solution. 这是一个非常简单的ASP.NET Webforms解决方案的基础。 A better solution would use the MVP/MVVM patterns and the Dependency Inversion Principle but those aren't worth using if you don't understand basic separation of concerns yet. 更好的解决方案将使用MVP / MVVM模式和Dependency Inversion Principle,但是如果您还不了解关注点的基本分离,则不值得使用。

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

相关问题 app_code文件夹中的asp.net使用类 - asp.net use class in app_code folder 访问ASP.NET中App_Code中未声明的类 - Accessing a Class that is NOT declared in App_Code in ASP.NET 为什么abc.aspx.cs的类在asp.net的App_code文件夹的类中不可见 - why class for abc.aspx.cs is not visible in class of App_code folder in asp.net 无法从ASP.Net网站项目中的代码访问App_Code中的类 - Unable to access class in App_Code from code behind in ASP.Net Website Project 如何访问不在ASP.NET中的app_code中的全局类中的变量 - How to access variable in a global class not in app_code in asp.net 尝试从ASP.NET中的App_Code文件夹中的另一个类派生时键入冲突 - Type conflict when trying to derive from another class in the App_Code folder in ASP.NET 无法从asp.net网站项目中的app_code文件夹访问类 - Can't access class from app_code folder in asp.net website project 如何从app_code中的类设置母版页中的属性? 与asp.net C# - How to set property in master page from class in app_code? with asp.net c# 在ASP.NET app_code文件夹中调用移动检测类方法 - Call mobile detection class method in ASP.NET app_code folder 在dll中调用Activator.CreateInstance来实例化位于Asp.Net App_Code中的实例类 - Call Activator.CreateInstance in a dll to intance class that reside inside Asp.Net App_Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM