What is Container? What is IOC? What is DI? Types of Container?
Container means saman dhone wala.
Ek hota khud se saman ko dhona but es me 100% success ka chance kam hota h, kuch na kuch galti ho hi sakta h, to as the better hum dhone wala machine le lete h dhone k liye samjhe. wahi different type ka dhone wala machine hota h.
Container programming world me v same real world jesa hi hota h.
Note: IOC k ander DI ata h (DI is subset of IOC)
IOC(Inversion Of Control)
ye ek design principal h jo software component ya module dependencies ko control nhi karta h balki external source se prapt krata h.
IOC software component ya module dependencies ko kam karta h aur reusability aur maintainability ko badhata h.
IOC ek higher level ka concept h aur eska use kisi v tarike se kar sakte h jese ex.- DI, Service Locator, event-based IOC etc.
DI (Dependency Injection)
Ye v software component ya module ko loosely couple banata h. taki s/w component ko asani se test , reuse kr sake
DI, IOC ka special implementation h.
Types of DI (Dependency Injection)
1. Constructor, 2. Property, 3 method injection
Yahi 3 injection hota h
a. Constructor Injection: Dependencies component ke constructor ke through pass kiye jate hain.
ex.
public class UserService {
private UserRepository userRepository;
// Constructor injection
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
// ...
}
ex.
public class UserService {
private UserRepository userRepository;
// Setter injection
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
// ...
}
UserService userService = new UserService();
UserRepository userRepository = new UserRepository();
userService.setUserRepository(userRepository);
c. Interface Injection: Component ko ek specific interface implement karne ke liye force kiya jata hai, jisse dependencies provide ki ja sakti hain.
ex.
public interface IDependency {
void DoSomething();
}
public class MyDependency : IDependency {
public void DoSomething() {
Console.WriteLine("Dependency is doing something.");
}
}
public class MyClass {
private IDependency _dependency;
public MyClass(IDependency dependency) {
_dependency = dependency;
}
public void DoWork() {
_dependency.DoSomething();
}
}
Container programming world me data ko hold, access, manipulate, manage aur deploy karta h.
.NET ek Microsoft dvara develop kiya gaya ek software framework hai, jiska upayog various programming languages, such as C#, VB.NET, aur F# se application development ke liye kiya ja sakta hai. Isme ek container concept bhi hota hai, jiska upayog application deployment aur management me hota hai.
Containers ek tarah se virtualization technology hoti hain, jisse aap ek application aur uske dependencies ko ek isolated environment me run kar sakte hain, without affecting the underlying operating system. Ye containers portable hote hain, matlab ki aap unhe ek system se dusre system par aasani se shift kar sakte hain, kyun ki isme application aur dependencies ek bundle me packaged hote hain.
.NET me containers ki madad se aap .NET applications ko packaging kar sakte hain, jaise ki Docker containers, aur fir in containers ko kisi bhi system par deploy kar sakte hain, jahan par Docker ya koi dusra container runtime support ho. Isse application deployment aur scaling me madad milti hai.
Containers .NET world me application deployment aur management ko simplify karne me madadgar hote hain aur isse applications ko consistent aur predictable tarike se run kiya ja sakta hai.
Types of Container
1. Unity: A container provided by Microsoft as part of the Enterprise Library. It's suitable for ASP.NET and other applications.
2. Autofac
3. Ninject: A lightweight, open-source dependency injection container.
4. Castle Windsor: A mature and widely used open-source container.
5. Simple Injector: A high-performance, open-source container designed to be easy to use.
6. Structure Map: An open-source IoC/DI container for .NET.
7. Dryioc: A fast, lightweight, and extensible open-source container.
8. Scrutor: A library for discovering and configuring services in the .NET Core application.
9. Microsoft.Extensions.DependencyInjection (This container built in .net core)
==============================================================
IOC(Inversion Of Control) n DI(Dependency Injection)
"Inversion of Control" (IoC) aur "Dependency Injection" (DI) dono software design concepts hain, jo software components ke interaction aur organization ke tarike ko describe karte hain. Yeh dono concepts often ek dusre ke sath use hote hain, lekin unme ek farq hota hai:
1. Inversion of Control (IoC):
IoC ek design principle hai, jo ke kahta hai ki ek software component ya module apne dependencies ko control nahi karna chahiye, balki unko external source se prapt karna chahiye. IoC ka mukhya uddeshya hota hai software components ke coupling ko kam karna aur reusability aur maintainability ko badhana.
IoC ek higher-level concept hai aur iska use kisi bhi tarike se kar sakte hain, jaise ki Dependency Injection, Service Locator, Event-based IoC, etc. IoC ka main idea hai ki aap ek component ko unke dependencies ke liye responsible nahi banate, aur dependencies ko kisi external source se provide karte hain.
2. Dependency Injection (DI):
Dependency Injection ek specific implementation hai IoC principle ki. Isme dependencies ek component ko external source se di jati hain, usually through constructor parameters, setter methods, ya interfaces. Isse component ka coupling kam hota hai aur component ko asani se test aur reuse kiya ja sakta hai.
Dependency Injection ke kuch common types hote hain:
a. Constructor Injection: Dependencies component ke constructor ke through pass kiye jate hain.
b. Setter Injection: Dependencies component ke setter methods ke through set kiye jate hain.
c. Interface Injection: Component ko ek specific interface implement karne ke liye force kiya jata hai, jisse dependencies provide ki ja sakti hain.
In summary, IoC ek design principle hai jo ke dependencies ko external sources se obtain karne ki approach ko describe karta hai, jabki DI ek specific implementation hai IoC ka, jisme dependencies component ke through external sources se provide ki jati hain. Dependency Injection IoC ka ek subset hai, aur iska istemal IoC principle ko apply karne ke liye hota hai.
==============================================================
In .NET, there are several types of containers that are used to store and manage data. These containers are part of the .NET Framework and include:
1. **Arrays**: Fixed-size collections of elements with the same data type.
2. **Lists**: Dynamic, resizable collections of elements. `List<T>` is a common example.
3. **Dictionaries**: Key-value pairs collections. `Dictionary<TKey, TValue>` is frequently used.
4. **Queues**: A collection that follows the First-In-First-Out (FIFO) principle. `Queue<T>` is an example.
5. **Stacks**: A collection that follows the Last-In-First-Out (LIFO) principle. `Stack<T>` is an example.
6. **Sets**: Unordered collections of unique elements. `HashSet<T>` is a common set implementation.
7. **Linked Lists**: A data structure where each element points to the next one. `LinkedList<T>` is an example.
8. **Hashtable**: A data structure for key-value pairs, similar to a dictionary.
9. **Concurrent Collections**: Thread-safe collections that allow concurrent access. Examples include `ConcurrentQueue<T>` and `ConcurrentDictionary<TKey, TValue>`.
10. **Custom Collections**: You can create your own custom containers by implementing interfaces like `ICollection`, `IList`, and `IDictionary`.
These are some of the commonly used container types in .NET, and they provide various ways to manage and manipulate data in your applications.
What is Container? What is IOC? What is DI? Types of Container?
Reviewed by Rikesh
on
November 05, 2023
Rating:
No comments: