Creating/Resolving 使用 Autofac 的对象列表
Creating/Resolving a list of objects using Autofac
我正在学习 IoC 和 Autofac,所以我的问题很基础,但找不到满意的答案。我有这些测试 类:
DataModel.cs
public class DataModel : IDataModel
{
public int PosX { get; set; }
public int PosY { get; set; }
public string Name { get; set; }
public DataModel(int posx, int posy, string name)
{
PosX = posx;
PosY = posy;
Name = name;
}
public void WriteName()
{
Console.WriteLine($"Hello, my name is \"{Name}\". Nice to meet you!");
}
public void WritePosition()
{
Console.WriteLine($"\"{Name}\": Position of X axis is {PosX} and position of Y axis is {PosY}");
}
}
BusinessLogic.cs
public class BusinessLogic : IBusinessLogic
{
ILogger _logger;
IList<IDataModel> _dataModels;
public BusinessLogic(ILogger logger, IList<IDataModel> dataModels)
{
_logger = logger;
_dataModels = dataModels;
}
public void ProcessData()
{
_logger.Log("Starting the processing of devices data.");
_logger.Log("Processing the data...");
foreach (var model in _dataModels)
{
model.WriteName();
model.WritePosition();
}
_logger.Log("Finished processing the data.");
}
}
现在您可以看到,BusinessLogic 的构造函数需要 IDataModel 的集合。问题是如何创建存储在容器中的接口列表以实现如下目的:
for(int x = 0; x <=7; x++)
{
for(int y = 0; y <= 7; y++)
{
list.Add(new DataModel(x,y,$"Object {x}{y}"));
}
}
也许我整个想法都错了。我很感激每一个答案。谢谢!
注意IList<IDataModel> dataModels
不是服务,待解决。我不确定这是最好的方法,但是您必须创建一个服务(例如 IDataInitializer
)为 IList<IDataModel> dataModels
提供方法或对象,然后在 IServiceCollection
上注册它或IOC
容器在 Startup.cs
中,然后可以在其他服务和 类.
中解析
这是一个例子:
public interface IDataInitializer
{
IList<IDataModel> GetDataModel();
}
public class DataInitializer : IDataInitializer
{
private IList<IDataModel> _dataModels;
public DataInitializer()
{
for (int x = 0; x <= 7; x++)
{
for (int y = 0; y <= 7; y++)
{
_dataModels.Add(new DataModel(x, y, $"Object {x}{y}"));
}
}
}
public IList<IDataModel> GetDataModel()
{
return _dataModels;
}
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IDataInitializer>(new DataModel());
}
我正在学习 IoC 和 Autofac,所以我的问题很基础,但找不到满意的答案。我有这些测试 类:
DataModel.cs
public class DataModel : IDataModel
{
public int PosX { get; set; }
public int PosY { get; set; }
public string Name { get; set; }
public DataModel(int posx, int posy, string name)
{
PosX = posx;
PosY = posy;
Name = name;
}
public void WriteName()
{
Console.WriteLine($"Hello, my name is \"{Name}\". Nice to meet you!");
}
public void WritePosition()
{
Console.WriteLine($"\"{Name}\": Position of X axis is {PosX} and position of Y axis is {PosY}");
}
}
BusinessLogic.cs
public class BusinessLogic : IBusinessLogic
{
ILogger _logger;
IList<IDataModel> _dataModels;
public BusinessLogic(ILogger logger, IList<IDataModel> dataModels)
{
_logger = logger;
_dataModels = dataModels;
}
public void ProcessData()
{
_logger.Log("Starting the processing of devices data.");
_logger.Log("Processing the data...");
foreach (var model in _dataModels)
{
model.WriteName();
model.WritePosition();
}
_logger.Log("Finished processing the data.");
}
}
现在您可以看到,BusinessLogic 的构造函数需要 IDataModel 的集合。问题是如何创建存储在容器中的接口列表以实现如下目的:
for(int x = 0; x <=7; x++)
{
for(int y = 0; y <= 7; y++)
{
list.Add(new DataModel(x,y,$"Object {x}{y}"));
}
}
也许我整个想法都错了。我很感激每一个答案。谢谢!
注意IList<IDataModel> dataModels
不是服务,待解决。我不确定这是最好的方法,但是您必须创建一个服务(例如 IDataInitializer
)为 IList<IDataModel> dataModels
提供方法或对象,然后在 IServiceCollection
上注册它或IOC
容器在 Startup.cs
中,然后可以在其他服务和 类.
这是一个例子:
public interface IDataInitializer
{
IList<IDataModel> GetDataModel();
}
public class DataInitializer : IDataInitializer
{
private IList<IDataModel> _dataModels;
public DataInitializer()
{
for (int x = 0; x <= 7; x++)
{
for (int y = 0; y <= 7; y++)
{
_dataModels.Add(new DataModel(x, y, $"Object {x}{y}"));
}
}
}
public IList<IDataModel> GetDataModel()
{
return _dataModels;
}
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IDataInitializer>(new DataModel());
}