xxxxxxxxxx
// First, create an interface for the dependency
public interface IMyDependency
{
void DoSomething();
}
// Implement the interface in a concrete class
public class MyDependency : IMyDependency
{
public void DoSomething()
{
Console.WriteLine("Doing something...");
}
}
// Define the class that requires the dependency and inject it through constructor
public class MyClass
{
private readonly IMyDependency _dependency;
public MyClass(IMyDependency dependency)
{
_dependency = dependency;
}
public void UseDependency()
{
_dependency.DoSomething();
}
}
// Configure the dependency injection container (e.g., using Microsoft.Extensions.DependencyInjection)
public class Program
{
public static void Main(string[] args)
{
// Set up the dependency injection container
var serviceProvider = new ServiceCollection()
.AddTransient<IMyDependency, MyDependency>()
.BuildServiceProvider();
// Resolve the class that requires the dependency from the container
var myClass = serviceProvider.GetService<MyClass>();
// Use the dependency
myClass.UseDependency();
}
}
xxxxxxxxxx
public class Person
{
private readonly string _name;
public Person(string name)
{
_name = name;
}
public void Process(IMyLogger logger)
{
logger.log($"{_name} processed successfully!");
}
}
public class PersonFactory
{
public Person Produce(string name)
{
IMyLogger logger = new MyLogger();
Person person = new Person(name);
person.Process(logger);
return person;
}
}
}
xxxxxxxxxx
// Interface representing a logger
public interface ILogger
{
void Log(string message);
}
// Implementation of the logger interface
public class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
// Class that depends on a logger
public class MyClass
{
private readonly ILogger logger;
// Dependency injection via constructor
public MyClass(ILogger logger)
{
this.logger = logger;
}
public void DoSomething()
{
logger.Log("Doing something...");
}
}
// Main program
public class Program
{
public static void Main()
{
// Creating an instance of the logger
ILogger logger = new ConsoleLogger();
// Creating an instance of MyClass and injecting the logger
MyClass myClass = new MyClass(logger);
// Calling a method on MyClass
myClass.DoSomething();
}
}
xxxxxxxxxx
public class Person
{
private readonly IMyLogger _logger;
private readonly string _name;
public Person(IMyLogger logger, string name)
{
_logger = logger;
_name = name;
_logger.log($"{_name} was created!");
}
}
public class PersonFactory
{
public Person Produce(string name)
{
IMyLogger logger = new MyLogger();
return new Person(logger, name);
}
}
xxxxxxxxxx
public class Person
{
private readonly string _name;
public Person(string name)
{
_name = name;
}
public IMyLogger Logger { get; set; }
public void Process()
{
Logger.log($"{_name} processed successfully!");
}
}
public class PersonFactory
{
public Person Produce(string name)
{
IMyLogger logger = new MyLogger();
Person person = new Person(name) { Logger = logger };
person.Process();
return person;
}
}