2016-01-28 30 views
0

ASP.NET局部類我創建了一個類連接,並使用與register.aspx沒有任何問題。當我嘗試將代碼從register.aspx.cs移動到regpartial.cs然後我得到一個衝突錯誤:「connection conn = new connection();」類在App_Code文件

我想代碼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 
    { 

    } 
} 
+0

爲什麼您認爲您首先需要部分班? – mason

+0

連接在類 – csharpfolk

+0

之外是的,那是我在每個aspx頁面上使用的想法(連接在類之外),但沒有在partialclass上使用。在註冊表和其他頁面中有很多代碼,這就是爲什麼我想將頁面代碼移動到部分類。 – Cagatay

回答

1

一般而言,您的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 Web表單解決方案的基礎。一個更好的解決方案將使用MVP/MVVM模式和依賴倒置原則,但如果您不瞭解基本的分離關係,那麼這些模式就不值得使用。

相關問題