-->

what is Microsoft.Practices.EnterpriseLibrary.Data

2019-03-18 19:29发布

问题:

I want to know what is Microsoft.Practices.EnterpriseLibrary.Data and why we use this dll. what are the benefits of this dll.

I want to create a project on 3-tier architecture what is the best way for sql queries.

weather i use this dll or go for simple sqlcommand and dataadapter. currently i am working in this way: My Code in DAL File is:

public void Insert(long id)
{
    connection.Open();
    SqlCommand dCmd = new SqlCommand("test_procedure", connection);
    dCmd.CommandType = CommandType.StoredProcedure;
    try
    {
        dCmd.Parameters.AddWithValue("@id", id);           
        dCmd.ExecuteNonQuery();
    }
    catch
    {
        throw;
    }
    finally
    {
        dCmd.Dispose();
        connection.Close();
        connection.Dispose();
    }
}

I am confused weather i am working in right way or should i use Microsoft.Practices.EnterpriseLibrary.Data and then i create DatabaseFactory.

回答1:

The main advantage of the Microsoft.Practices.EnterpriseLibrary.Data library is that it makes it easier to produce database-agnostic code. The developer interacts mainly with more generic Database vs SqlConnection and DbCommand vs SqlCommand objects, in theory the mechanism of switching the underlying database, from MSSQL to Oracle becomes somewhat easier. Even though in my development experience I've never seen that occur.

Microsoft.Practices.EnterpriseLibrary.Data also directs the developer to use DbParameter for query parameters, which reduces the risk of SQL injection attacks.

The Microsoft.Practices.EnterpriseLibrary.Data is a higher abstract of the core ADO .Net constructs and enables the developer to complete the same tasks in a minimal amount of code.

If your learning Data access strategies I suggest continuing with ADO .Net the more you understand the basics the more useful Microsoft.Practices.EnterpriseLibrary.Data or Entity Framework or NHibernate is to use because you understand the fundamentals since the technologies are built on top of ADO .Net.