Preface:
After the previous article is published
Most of my friends' responses are too abstract. It seems that I didn't talk about anything.
In fact, the concept is actually abstract, and the essence is simply a simple word.
Haha ~ A little more,
This second article mainly serves as an example of implementation,
To see if it is so ethereal.
Architecture:
The blue box represents an independent DLL
Specifically, it must be conveyed,
The interface definition at the data access layer is tied to the logic layer.
The implementation of the data access layer is a data access layer interface dependent on the logic layer.
Engineers often Design Software
Written as: logic layer ==> data access layer
Recommended Practice: logical layer (data access interface <=) = data access layer
Changing the direction of dependency is an important concept in object-oriented design.
Example:
namespace Clark.Example{ public class Product { public string ProductID = string.Empty; public string Caption = string.Empty; public string Description = string.Empty; } public interface IProductStore { void Add(Product product); void Update(Product product); void Delete(Product product); Product[] GetAll(); } public class Business { private IProductStore _store; public Business(IProductStore store) { _store = store; } public int GetProductCount() { Product[] products = _store.GetAll(); return products.Length; } }}namespace Clark.Example.SqlStorage{ public class SqlProductStore : IProductStore { private string _connectionString; public SqlProductStore(string connectionString) { _connectionString = connectionString; } public Product[] GetAll() { Product product; List<Product> productList; SqlConnection conn = new SqlConnection(_connectionString); SqlCommand cmd = new SqlCommand("SELECT * FROM Products WHERE", conn); SqlDataReader reader = null; conn.Open(); reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); productList = new List<Product>(); while (reader.Read()) { product = new Product(); product.ProductID = reader.GetValue(1).ToString(); product.Caption = reader.GetValue(2).ToString(); product.Description = reader.GetValue(3).ToString(); productList.Add(product); } reader.Close(); conn.Dispose(); cmd.Dispose(); return productList.ToArray(); } public void Add(Product product) { // } public void Update(Product product) { // } public void Delete(Product product) { // } }}namespace Clark.ExampleConsole{ class Program { static void Main(string[] args) { IProductStore store = CreateStore(); Business business = new Business(store); Console.WriteLine(business.GetProductCount()); Console.ReadLine(); } private static IProductStore CreateStore() { return new Clark.Example.SqlStorage.SqlProductStore("xxxxx"); } }}
Note:
This application is very simple, mainly
Display the number of products in the data storage area on the screen
It is worth noting some design ideas.
Clark. Example
Product: This is the object that needs to be stored permanently.
Permanent storage is a key word,
In terms of concept, we can regard the system as the figure below
The execution area is the object used for application execution.
Sometimes we store objects in a place that can be stored for a long time (database or other)
You can get the previously stored objects at the next startup.
Note: [the object is stored, not the data 」]
Simply store data, it is easy to fall into the direction of data-oriented programming.
Iproductstore: interface defined at the data access layer
Define the data access layer and implement interface functions.
Note the following when defining this interface: [storing "object" instead of "data 」]
Interface functions should be defined as: objects that can be stored permanently. Which methods can be used through the data access layer.
Business: logical layer object
The logical object of system operation.
Clark. example. sqlstorage
Sqlproductstore: Object of the interface defined at the data access layer
This object is the interface defined in the data access layer, and the destination of the object to be stored is the SQL database.
Define interface objects through various implementation data access layer,
We can switch to the object storage destination without changing to the enterprise logic.
Clark. exampleconsole
Program: entry point of program code
In addition to program code entry points,
In addition, he is responsible for generating iproductstore and business.
The generation mode in the example does not unbind Clark. example. sqlstorage from the system.
When actually writing related programs
The extended DLL is unbound through various generation modes.
Object generation mode. If you have time, refer to the data snore mentioned in the previous article.
Conclusion:
Friends who read this article
It may not be very familiar with what I want to express
Wait for a few more articles.
Interpreting concepts from different perspectives should be a new outline. ^
PS: Call ~ It is not easy to express your thoughts in words.