Ado.net Quick Start Questions and answers

Source: Internet
Author: User

Ado.net Quick Start Questions and answers

I. Problems in code

1. Why does the pseudo-SqlMapper base class use the abstract class BaseSqlMapper instead of the interface?

A: Lou pig also knows that the SqlMapper base class under IBatis is an interface ISqlMapper. But when Lou pig writes code, it finds that generic constraints are written once in the abstract base class, in SqlMapper, do not write any more, but use the interface to write the constraints twice. Haha, I was not deeply aware of the common sense of "interface-based programming". The shoes that raised this question clearly show that the skills are superior. Haha, lou pig learned this lesson. At the end of this article, the demo pig changed BaseSqlMapper to the ISqlMapper interface. Please note.

2. Isn't it good to write too many SQL statements on the front-end page?

This problem is also very good. In actual projects, SQL statements are usually written in specific Dao files, such as the PersonDao class mentioned in the previous demo. When we add a Person, the example uses view plaincopy to clipboardPRint?

Public int Insert (string sqlInsert)

Public int Insert (string sqlInsert)

This method is inserted, but in actual projects, we usually write view plaincopy to clipboardprint?

Public int Insert (Person model)

Public int Insert (Person model)

This type of method is used to insert a database. Therefore, it is unreasonable to write SQL statements at the front-end (performance Layer. Thanks again to the kids who pointed out the code problems. If some new users have been misled, Lou pig should thoroughly review and review it here (the demo is obviously a bit casual and lazy). Here, Lou pig solemnly reminds me that, do not imitate the bad way of writing SQL statements in the demo. Lou pig is also a programmer, because Batis.net is used in the Dal layer xml, so pay attention to it.

Ii. Questions (directly extract the content of the email)

1. "It seems that there is no database switch, right? How to switch MSSQL. What about ACC and XML "?

A: this should be about the support of source code for multiple databases. Oh, Lou pig hasn't implemented it yet. Check the source code. SQL Server supports the simple basic CRUD function. If you want to expand to support other databases, just imitate the specific implementation in the SqlServer class. Oracle or MySQL databases are not installed in the machine environment of Lou pig, so it is not good to test after writing. If the conditions are met, you can try to complete it. For database switching, the ideal state is the most classic Abstract Factory mode. However, we recommend that you use pseudo SqlMapper to switch database types. For specific operations, you only need to add a database type node sqlType in the configuration file appsetting, and use the enumeration of Common Database types in the class.

<Deleetask>

<Add key = "db_timeOut" value = "5000"/>

<! -- Database Type 0 SqlServer 1 Orcale 2 MySql -->

<Add key = "sqlType" value = "0"/>

</AppSettings>

The enumerated values are as follows:

Code

Namespace AdoNetDataaccess. Mapper

{

Public enum SqlEnum

{

Default = 0,

SQLServer = 0,

Oracle = 1,

MySql = 1

}

}

Then, some processes for instantiating SqlMapper are judged, and the database switching problem is completely transferred to instantiating SqlMapper:

Code

Using System;

Using System. Collections. Generic;

Using System. Configuration;

Namespace AdoNetDataAccess. Mapper

{

Using AdoNetDataAccess. Core. Contract;

Using AdoNetDataAccess. Core. Implement;

# Region enum

Public enum SqlEnum

{

Default = 0,

SQLServer = 0,

Oracle = 1,

MySql = 1

}

# Endregion

Public sealed class MapperUtill

{

# Region fields

Public static string currentSqlKey = "sqlConn ";

Public static int timeout = 15;

Private static int sqlType = 0; // Database Type 0 SqlServer 1 Orcale 2 MySql

Private static readonly object objSync = new object ();

Private static readonly IDictionary <string, ISqlMapper> dictMappers = new Dictionary <string, ISqlMapper> ();

# Endregion

# Region constructor and methods

Private MapperUtill ()

{

}

Static MapperUtill ()

{

Try

{

Specified Timeout = int. Parse (ConfigurationManager. deleettimeout ["db_timeOut"]);

}

Catch

{

Required timeout = 15;

}

Try

{

SqlType = int. Parse (ConfigurationManager. etettings ["sqlType"]);

}

Catch (Exception ex)

{

Throw ex;

}

// Instantiate SqlDbMapper

For (int I = 0; I <ConfigurationManager. ConnectionStrings. Count; I ++)

{

String key = ConfigurationManager. ConnectionStrings [I]. Name;

String value = ConfigurationManager. ConnectionStrings [I]. ConnectionString;

CreateMapper (key, value, interval timeout );

}

}

 

Public static ISqlMapper GetSqlMapper (string key)

{

Return MapperUtill. GetMapper (key );

}

Public static ISqlMapper GetCurrentSqlMapper ()

{

Return MapperUtill. GetMapper (currentSqlKey );

}

Public static void CreateMapper (string connKey, string sqlConStr, int connTimeOut)

{

IDbOperation operation = null;

Switch (sqlType)

{

Default:

Case 0:

Operation = new SqlServer (sqlConStr, connTimeOut );

Break;

Case 1:

// Operation = new Orcale (sqlConStr, connTimeOut); // Orcale is not implemented

Break;

Case 2:

// Operation = new MySql (sqlConStr, connTimeOut); // MySql is not implemented either.

Break;

}

If (operation = null)

{

Throw new Exception ("the database type you configured may be faulty ");

}

SqlMapper mapper = new SqlMapper (operation );

DictMappers. Add (connKey. ToUpper (). Trim (), mapper); // case insensitive

}

Public static ISqlMapper GetMapper (string sqlConKey)

{

If (string. IsNullOrEmpty (sqlConKey ))

{

Throw new Exception ("the primary key of the database connection string is null! ");

}

SqlConKey = sqlConKey. ToUpper (); // case insensitive

ISqlMapper mapper = null;

If (dictMappers. ContainsKey (sqlConKey ))

{

Mapper = dictMappers [sqlConKey];

}

Else

{

Throw new Exception (string. Format ("no database connection for {0}", sqlConKey ));

}

Return mapper;

}

/// <Summary>

/// Release all

/// </Summary>

Public void Release ()

{

Foreach (KeyValuePair <string, ISqlMapper> kv in dictMappers)

{

SqlMapper mapper = kv. Value as SqlMapper;

If (mapper = null)

{

Continue;

}

Mapper. CurrentDbOperation. CloseConnection ();

}

DictMappers. Clear ();

}

# Endregion

}

}

Note that the database switching method here is not absolute. You can program the switching based on other methods you like. Lou pig is only throwing bricks here.

2. "I'm not familiar with ORM. I 'd like to ask what kind of work your ORM has saved? I think there are a lot of reflection in it. Does this affect efficiency ?"

First, Lou pig is not familiar with ORM. The implementation is strictly not an ORM, but there are two ORM shadows that Lou pig has used.

Secondly, the current implementation of Dongdong is not designed to save some work. Lou pig's original intention is to re-read the notes of the classic Redbook ado.net.

Third, reflection has a significant impact on efficiency compared to the absence of reflection. It should be noted that reflection should be used according to the actual project requirements. Based on the personal development experience of Lou pig, for the simple and common front-and back-end mis system to be implemented by most programmers, if the customer has no opinions on efficiency while ensuring basic requirements, it doesn't matter if you use it. Most of the work is handed over to the server. Programmers don't have to do too much work. However, for large websites with frequent accesses, real-time systems, or systems that handle large data volumes, we recommend that you do not use reflection, and focus on data "decoration.

Finally, if you are interested in ORM, You may search for the works of the experts online, such as nhib.pdf, iBatis.net, WebSharp, and LINQ, the Source Code cannot be studied. It is recommended that you be a beginner with one or two skills.) You may have more inspiration. If you have implemented mainstream functions similar to other popular ORM (such as automatic generation of SQL statements or XML configuration, caching, and delayed loading), and are more powerful, the new ORM, which is more convenient to use, is very helpful. We sincerely hope that your success will come soon.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.