Ado.net Quick Questions and Answers

Source: Internet
Author: User
Tags abstract reflection mysql database connectionstrings

One, the problem in the code

1, pseudo-Sqlmapper base class Why use abstract class Basesqlmapper without interface?

A: Building pigs also know that the base class of the Sqlmapper under Ibatis is an interface isqlmapper, but when the building pig writes the code, discovers the generic constraint writes once in the abstract base class, does not write again under the Sqlmapper, but uses the interface must write two times the restriction. Oh, the consequences of a lazy, not deeply aware of the "interface programming" this common sense, put forward the problem of children's shoes can obviously see the technical high-rise pig, Ah, ha, Lou Pig to learn this lesson, The final demo building pig will be basesqlmapper unified replaced by the Isqlmapper interface, please note.

2, in the foreground page to write SQL statements too much, is not very good?

That's a good question, too. In a real project, SQL statements are usually written in a specific DAO file, such as the Persondao class mentioned in the previous demo. When we add a person, the example is through the view plaincopy to Clipboardprint?

public int Insert (string sqlinsert)

public int Insert (string sqlinsert)

This form of method executes inserts, 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 performs a database insert operation. So it is unreasonable to write SQL statements in the foreground (presentation layer). Thanks again for the children's shoes that point to the code problem. If some novice has caused some misleading, building pig here to deep introspection and review (demo write obviously a bit casual and lazy), here, building pig solemn reminder, the location of the SQL statement please do not imitate the demo in this bad writing, Lou Pig is also an experienced, Because the use of batis.net is placed in the DAL layer of XML, please be sure to pay attention to it.

Second, the question (directly extracts the content of the mail)

1, "Database switching does not seem to be right." How to switch MSSQL. Acc,xml and so on? "

A: This should be said that the source code for a variety of database support issues. Oh, Lou Pig hasn't been realized yet. Looking at the source, SQL Server simple basic CRUD functionality has been supported. If you want to extend support to other databases, emulate the specific implementations in SQL Server classes. No Oracle or MySQL database is installed in the machine environment of the building pig, so it is not good to test when finished. If you have the conditions, you can try to finish it. As for the database switch, the ideal state of the implementation of the most classical abstract factory model, OH, the configuration file with reflection on the line. But Lou Pig recommends that you simply use pseudo Sqlmapper for database type switching. Concrete operation, in fact, as long as in the configuration file appsetting add a database type node SqlType, with the class of common database types of enumeration can be.

<appSettings>

<add key= "Db_timeout" value= "5000"/>

<!--database type 0 SQL Server 1 Orcale 2 mysql-->

<add key= "SqlType" value= "0"/>

</appSettings>

Enumerated below:

Code

Namespace Adonetdataaccess.mapper

{

public enum Sqlenum

{

Default = 0,

SQL Server = 0,

Oracle = 1,

MYSQL = 1

}

}

Then, it is some instantiation of the sqlmapper process to judge, the problem of switching the database completely transferred to the instantiation of 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,

SQL Server = 0,

Oracle = 1,

MYSQL = 1

}

#endregion

public sealed class Mapperutill

{

#region Fields

public static string Currentsqlkey = "Sqlconn";

public static int cmdtimeout = 15;

private static int sqltype = 0;//database type 0 SQL Server 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

{

cmdtimeout = Int. Parse (configurationmanager.appsettings["db_timeout"]);

}

Catch

{

Cmdtimeout = 15;

}

Try

{

SqlType = Int. Parse (configurationmanager.appsettings["SqlType"]);

}

catch (Exception ex)

{

Throw ex;

}

Instantiation of 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, cmdtimeout);

}

}

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 SQL Server (SQLCONSTR, conntimeout);

Break

Case 1:

Operation = new Orcale (SQLCONSTR, conntimeout),//orcale not implemented

Break

Case 2:

Operation = new MySql (SQLCONSTR, conntimeout);//mysql didn't realize it either.

Break

}

if (operation = null)

{

throw new Exception ("The type of database you have configured is likely to have a problem");

}

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 ("database connection string primary key is empty!");

}

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

}

}

It is important to note that the database switching mode here is not absolute, you can follow your favorite habits of other ways to complete the switch, Lou Pig here is just throwing bricks.

2, "I am not familiar with ORM, I would like to ask you this ORM in the end is to save what part of the work?" I think there is a lot of reflection inside, is this very effective? ”

First of all, building pigs are not familiar with ORM. This is not technically an ORM at all, but there are two ORM shadows that have been used by the building's own pigs.

Second, the current implementation of the Dongdong is not to save which part of the work and design, Lou Pig's original intention is to reread Ado.net classic Redbook and do review notes.

Third, the reflection phase is of course very effective for not using reflection. It should be explained that the need to use reflection should be based on actual project needs. According to the personal development experience of the building pig, for most programmers to achieve a simple common front and rear mis system, in the case of ensuring basic needs, the customer if the efficiency of no opinion, with no great relationship, most of the work to the server to complete it, programmers do not have to do too much work. But for large web sites with frequent visits, real-time systems or systems that deal with large amounts of data, it is recommended that you do not use reflection, and you may want to focus on the data "decorating".

Finally, if there are children's shoes to ORM interested in, may wish to search the Internet to search the works of Daniel, the famous nhibernate, Ibatis.net,websharp,linq and so on (so many Orm, one to be familiar with, the source code also research but come ah, suggest novice skilled one or two kinds of enough), perhaps to you have more inspiration. If you've implemented your own mainstream functionality similar to other popular ORM, such as automatically generating SQL statements or through XML configuration, caching, delay loading, etc.), and more powerful, more convenient use of new Orm, OH, sincerely look forward to your merit perfection that day early arrival, quickly to benefit thousands of programmers it.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.