应该在businesslogiclayer中编写什么代码? [英] what is the code should write inside businesslogiclayer?

查看:80
本文介绍了应该在businesslogiclayer中编写什么代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 命名空间 BusinessLogicLayer 
{
class BusinessClass
{
public string _Name;
public int _Roll_number;
public string _Email_Id;
public string _Mobile_Number;

DataAccessClass objdal = new DataAccessClass();

public string 名称
{
get
{
return _Name;
}
set
{
_Name = value ;
}
}
public int Roll_Number
{
get
{
return _Roll_number;
}
set
{
_Roll_number = value ;
}
}
public string Email_Id
{
get
{
return _Email_Id;
}
set
{
_Email_Id = value ;
}
}
public string Mobile_Number
{
get
{
return _Mobile_Number;
}
set
{
_Mobile_Number = value ;
}

}


数据访问层中的
代码是



 命名空间 DataAccessLayer 
{
public < span class =code-keyword> class DataAccessClass
{
// String cs = ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString;
SqlConnection con;
SqlCommand cmd;
public DataAccessClass()
{
SqlConnection con = new SqlConnection (ConfigurationManager.ConnectionStrings [ ConnectionString]。ConnectionString);
SqlCommand cmd = new SqlCommand( sp_InsertEmpdetails ,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ Name,objdal.Name); // 这里我无法访问objdal对象为什么?请帮帮我
cmd.Parameters.AddWithValue ( @ Roll_Number,objdal.Roll_Number); / /
cmd.Parameters.AddWithValue( @ Email_Id,objdal.Email_Id);
cmd.Parameters.AddWithValue( @ Mobile_Number,objdal.Mobile_Number);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}

}



我应该写什么代码来将数据插入数据库

解决方案

你的问题答案:

首先,你应该在业务层写的代码应该是'顾名思义'任何与业务运营相关的逻辑执行任何计算,业务验证等。



关于第二个问题,我认为您已经使用存储过程将记录插入数据库,如代码所示。所以我不确定,因为你的问题令人困惑。



关于你的评论//这里我无法访问objdal对象为什么?请帮助我
我认为您想要访问业务类中定义的属性。如果是这种情况,请将BusinessClass类设为public,然后创建它的实例。然后在您的数据访问层中使用它。





但是我的一些观察结果。

1.在业务层中,由于您已经拥有公共属性,因此应将变量标记为私有。在你的情况下,它们是公开的。



2.看来你正在命名以'sp_'开头的存储过程。这应该避免,因为系统存储过程也被命名为'sp_',因此从SQL Server的角度来看这不是最佳实践,因为它会有轻微的性能开销。你可以通过谷歌搜索来阅读它。 :)



3.始终建议使用try catch块来防止任何未处理的异常破坏您的应用程序。在您的情况下,您可以在数据访问层中使用try catch块来记录任何异常并关闭任何连接。



我希望这可能对您有所帮助..

namespace BusinessLogicLayer
{
    class BusinessClass
    {
        public string _Name;
        public int _Roll_number;
        public string _Email_Id;
        public string _Mobile_Number;

        DataAccessClass objdal = new DataAccessClass();

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        public int Roll_Number
        {
            get
            {
                return _Roll_number;
            }
            set
            {
                _Roll_number = value;
            }
        }
        public string Email_Id
        {
            get
            {
                return _Email_Id;
            }
            set
            {
                _Email_Id = value;
            }
        }
        public string Mobile_Number
        {
            get
            {
                return _Mobile_Number;
            }
            set
            {
                _Mobile_Number = value;
            }

        }


in dataaccess layer the code is

namespace DataAccessLayer
{
    public class DataAccessClass
    {
        //String cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection con;
        SqlCommand cmd;
        public DataAccessClass()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("sp_InsertEmpdetails", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name",objdal.Name);//here i could not access the objdal object why?please help me
            cmd.Parameters.AddWithValue("@Roll_Number", objdal.Roll_Number);//
            cmd.Parameters.AddWithValue("@Email_Id", objdal.Email_Id);
            cmd.Parameters.AddWithValue("@Mobile_Number", objdal.Mobile_Number);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

 }


and what code should i write to insert data into database

解决方案

Answers to you questions:
Firstly, the code you should write in your business layer should be 'as the name suggests' any business operation related logic like performing any calculations, business validations etc.

Regarding your second question, i think you are already inserting records into your database with a stored procedure as seen from your code. So I am not sure about that as your question is confusing.

Regarding your comment //here i could not access the objdal object why?please help me
I think you want to access the properties defined in your business class. If that is the case, make your 'BusinessClass' class as public and then create an instance of it. And then use it in your data access layer.


However there are a few observations from my side.
1. In the business layer, since you are already having public properties, you should mark the variables as private. In your case they are public.

2. It seems you are naming your stored procedures starting with 'sp_'. This should be avoided since system stored procedures too are named as 'sp_' hence this is not a best practice from SQL Server point of view since it will have a slight performance overhead. You can read it more by googling it out. :)

3. It is always advisable to use try catch blocks for preventing any un-handled exceptions breaking your application. In your case, you can use try catch block in your data access layer to log any exceptions and also to close any connections.

I hope this might help you...


这篇关于应该在businesslogiclayer中编写什么代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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