Skip to content

Commit b44aa53

Browse files
authored
Create _posts2024-12-24-New Project.md
1 parent ad0a45c commit b44aa53

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

_posts2024-12-24-New Project.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# vb2ae.ServiceLocator.MSDependencyInjection
2+
3+
4+
## What is the common service locator?
5+
6+
The Common Service Locator (CSL) library serves as a shared interface for service location, aimed at both application and framework developers. By providing an abstraction layer over IoC (Inversion of Control) containers and service locators, the CSL library allows applications to access these capabilities indirectly, without the need for hard references. This design ensures that third-party applications and frameworks can leverage IoC and service location features without being locked into a specific implementation. Ultimately, the goal is to promote flexibility and interoperability across various development ecosystems.
7+
8+
## What is vb2ae.ServiceLocator.MSDependencyInjection?
9+
10+
vb2ae.ServiceLocator.MSDependencyInjection is an implementation of the Common Service Locator (CSL) for the Microsoft.Extensions.DependencyInjection library. Currently, it uses version 9 of Microsoft.Extensions.DependencyInjection, enabling it to support all Keyed services effectively.
11+
12+
13+
## How to register your services
14+
15+
The ConfigureServices method is where we are registering the Objects for dependency injection
16+
17+
private IServiceProvider Build()
18+
{
19+
if (_built)
20+
throw new InvalidOperationException("Build can only be called once.");
21+
_built = true;
22+
23+
_defaultBuilder.ConfigureServices((context, services) =>
24+
{
25+
services.AddSingleton<IService, ServiceImpl>();
26+
services.AddTransient<ICar, Ford>();
27+
services.AddTransient<ICar, Toyota>();
28+
services.AddTransient<ICar, Chevy>();
29+
services.AddKeyedTransient<IPet, Dog>("Dog");
30+
services.AddKeyedTransient<IPet, Cat>("Cat");
31+
});
32+
33+
_services = _defaultBuilder.Build().Services;
34+
CommonServiceLocator.ServiceLocator.SetLocatorProvider(() => new vb2ae.ServiceLocator.MSDependencyInjection.MSDependencyInjectionServiceLocator(_services));
35+
return _services;
36+
}
37+
38+
39+
40+
The line of code below registers vb2ae.ServiceLocator.MSDependencyInjection with the CommonServiceLocator
41+
42+
CommonServiceLocator.ServiceLocator.SetLocatorProvider(() => new vb2ae.ServiceLocator.MSDependencyInjection.MSDependencyInjectionServiceLocator(_services));
43+
return _services;
44+
45+
46+
## How to Get classes stored in Dependency Injection with the CommonServiceLocator
47+
48+
# GetService
49+
50+
var serviceType = typeof(IService);
51+
var result = CommonServiceLocator.ServiceLocator.Current.GetService(serviceType);
52+
53+
This returns a ServiceImpl
54+
55+
# GetService< T >
56+
57+
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance<IService>();
58+
59+
This returns a ServiceImpl
60+
61+
# GetInstance(serviceType, key)
62+
63+
var serviceType = typeof(IPet);
64+
var key = "Cat";
65+
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance(serviceType, key);
66+
67+
This returns an instance of Cat
68+
69+
# GetInstance< T >(key)
70+
71+
var key = "Dog";
72+
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance<IPet>(key);
73+
74+
This returns an instance of Dog
75+
76+
# GetInstance(serviceType)
77+
78+
var serviceType = typeof(IService);
79+
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance(serviceType);
80+
81+
This returns a ServiceImpl
82+
83+
# GetInstance< T >
84+
85+
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance<IService>();
86+
87+
This returns a ServiceImpl
88+
89+
# GetAllInstances(ServiceType)
90+
91+
var result = CommonServiceLocator.ServiceLocator.Current.GetAllInstances(typeof(IPet));
92+
93+
Will return an IEnumerable<object> that contains a Cat and Dog
94+
95+
# GetAllInstances< T >
96+
97+
var result = CommonServiceLocator.ServiceLocator.Current.GetAllInstances<ICar>()
98+
99+
Will return an IEnumerable<ICar> that contains a Ford, Chevy and Toyota
100+
101+
## Notes
102+
Passing a null in as the key will return the first of the Type registered
103+
104+
var serviceType = typeof(IPet);
105+
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance(serviceType, null);
106+
107+
Will return the First IPet registered for DependencyInjection in this case an instance of the Dog class
108+
109+
All examples are based on what is registered in the How to register your services section

0 commit comments

Comments
 (0)