The most basic :UI-BLL-DAL
This is the stratification we are familiar with
First of all, I heard that dependency injection seems very complicated
Actually : In order to realize that different teams work on different levels . We can have a team deal with the data access layer , One team deals with the business layer , One team deals with UI.
First set up : The most basic three-tier architecture
Physical layer :
public class Product
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}The data layer :DAL
public class ProductDAL
{
private readonly List<Product> _products;
public ProductDAL()
{
_products = new List<Product>
{
new Product { Id = Guid.NewGuid(), Name= "iPhone 9",
Description = "iPhone 9 mobile phone" },
new Product { Id = Guid.NewGuid(), Name= "iPhone X",
Description = "iPhone X mobile phone" }
};
}
public IEnumerable<Product> GetProducts()
{
return _products;
}
public IEnumerable<Product> GetProducts(string name)
{
return _products
.Where(p => p.Name.Contains(name))
.ToList();
}
}lLogic layer :BLL
public class ProductBL
{
private readonly ProductDAL _productDAL;
public ProductBL()
{
_productDAL = new ProductDAL();
}
public IEnumerable<Product> GetProducts()
{
return _productDAL.GetProducts();
}
public IEnumerable<Product> GetProducts(string name)
{
return _productDAL.GetProducts(name);
}
}
UI layer :UI
class Program
{
static void Main(string[] args)
{
ProductBL productBL = new ProductBL();
var products = productBL.GetProducts();
foreach (var product in products)
{
Console.WriteLine(product.Name);
}
Console.ReadKey();
}
}The above is our basic structure : UI - BLL - DAL Citation order
Then there are three problems :
1. We can't have three different teams working at each level .
2. The business layer is hard to scale , Because it depends on the implementation of the data access layer .
3. The business layer is hard to maintain , Because it depends on the implementation of the data access layer .
High level objects should not rely on low-level objects . Both have to rely on abstraction . So what is an abstract concept ?
Abstraction is the definition of function . In our case , The business layer relies on the data access layer to retrieve books . stay C# in , We use interfaces for abstraction . Interface represents the abstraction of function .
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ The following is the modified structure
The data layer :DAL
DAL: Interface
public interface IProductDAL
{
IEnumerable<Product> GetProducts();
IEnumerable<Product> GetProducts(string name);
}
DAL: Realization
public class ProductDAL : IProductDAL
Business logic layer :BLL So we did Make it dependent on IDAL Abstraction layer , instead of DAL Layer directly implements :
public class ProductBL
{
private readonly IProductDAL _productDAL;
public ProductBL()
{
_productDAL = new ProductDAL();
}
public IEnumerable<Product> GetProducts()
{
return _productDAL.GetProducts();
}
public IEnumerable<Product> GetProducts(string name)
{
return _productDAL.GetProducts(name);
}
}
Business logic layer :IBLL ( alike UI Depend on BLL We do the same Make an abstraction layer Interface )
public interface IProductBL
{
IEnumerable<Product> GetProducts();
IEnumerable<Product> GetProducts(string name);
} Empathy : to update BLL layer :
public class ProductBL : IProductBL
UI layer : Just like BLL Depend on DAL equally UI Depend on BLL
class Program
{
static void Main(string[] args)
{
IProductBL productBL = new ProductBL();
var products = productBL.GetProducts();
foreach (var product in products)
{
Console.WriteLine(product.Name);
}
Console.ReadKey();
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The above is the establishment of the abstraction layer ,
Remember where we marked red ?
public ProductBL()
{
_productDAL = new ProductDAL();
}
BLL Depend on IDAL , however Ultimately, we rely on DAL
up to now , What we're doing has nothing to do with dependency injection
In order to be in BLL Depend on DAL The function of , Without concrete realization , The class must be created by someone else . Others have to provide concrete implementations of the underlying objects , This is what we call dependency injection . It literally means that we inject dependent objects into higher-level objects .
One way to implement dependency injection is to use constructors for dependency injection .
Update the business layer :
public class ProductBL : IProductBL
{
private readonly IProductDAL _productDAL;
public ProductBL(IProductDAL productDAL)
{
_productDAL = productDAL;
}
public IEnumerable<Product> GetProducts()
{
return _productDAL.GetProducts();
}
public IEnumerable<Product> GetProducts(string name)
{
return _productDAL.GetProducts(name);
}
}
UI:
class Program
{
static void Main(string[] args)
{
IProductBL productBL = new ProductBL(new ProductDAL());
var products = productBL.GetProducts();
foreach (var product in products)
{
Console.WriteLine(product.Name);
}
Console.ReadKey();
}
}
establish DAL Control and UI Bind together . This is also called inversion of control
We're not here BLL Created in DAL Example , But in UI Create it in . Main Method will inject instances into the business logic layer . therefore , We inject instances of low-level objects into instances of high-level objects .
This is called dependency injection
Now? , If we look at the code , We only rely on the business access layer (BLL) Data access layer in (IDAL) The abstraction of , And the business access layer (BLL) It uses the interface implemented by the data access layer . therefore , We follow the principle that both higher-level objects and lower level objects depend on abstraction , Abstraction is a contract between higher level objects and lower level objects .
The next step is to show the benefits of maintainability and scalability . for example , If we want to help SQL Server Create a new data access layer , We just need to implement the abstraction layer of data and access it .
Original link : www.cnblogs.com/hhhnicvscs/p/14204806.html
Link to the original text :www.codeproject.com/Articles/5274732/Dependency-Injection-and-IoC-Containers-in-Csharp









