簡體   English   中英

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

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

我創建了一個用於連接並與register.aspx一起使用的類,沒有任何問題。 當我嘗試將代碼從register.aspx.cs移動到regpartial.cs時,出現沖突錯誤:“ connection conn = new connection();”

我想將代碼register.aspx.cs移到mypartial.cs。 我認為這會更好,但是我不確定如何解決沖突問題。

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(最終嘗試)

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(最終嘗試)

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
    {

    }
}

通常, aspx.cs文件中不應包含太多代碼。

您想將直接是視圖/表示邏輯(使.aspx頁面起作用的東西)與業務邏輯(排序,轉換,驗證)分開的邏輯,最后要使數據訪問/資源訪問成為孤立。

我也建議使用Dapper.NET通過原始ADO.NET/SqlConnection。

我將整理一個非常基本的示例,說明如何將其分開。 這不能保證編譯c#代碼,但是會非常接近偽代碼。

內部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

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 };   
    }
}

最后,您應該有一個RegistrationAccess.cs,其中僅包含用於讀取和寫入SQL Server /其他數據庫/文件系統的代碼。

請注意aspx.cs文件如何不具有RegistrationAccess的知識。 您的視圖不應直接調用數據庫。 要注意的另一件事是RegistrationService不了解該視圖。 它接收一個注冊類型,然后執行業務邏輯並調出DAL。 DAL類對視圖和RegistrationService的知識為零,對數據庫一無所知。

這是一個非常簡單的ASP.NET Webforms解決方案的基礎。 更好的解決方案將使用MVP / MVVM模式和Dependency Inversion Principle,但是如果您還不了解關注點的基本分離,則不值得使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM