Autofac 未成功创建复杂对象
Autofac is not creating complex objects successfully
我是基于 Autofac 的依赖注入的新手。根据我们公司的要求,我必须在我正在创建的新网站 API 中使用它。以下是我的代码。我正在为 Autofac 使用 IocConfig class 在我的 Web API.
中创建和注入对象
public class MemberCodeController : ApiController
{
private IMemberStrategy memberStrategy;
private IMemberDetail memberDetail;
//Constructor dependency injection through Autofac
public MemberCodeController(IMemberStrategy memberStrategy,
IMemberDetail memberDetail)
{
this.memberStrategy = memberStrategy;
}
public IHttpActionResult CreateMember(string email)
{
memberDetail.DataFields.RegisterCode = GetCode();
memberDetail.Email = email;
}
}
The MemberDetail class:
public interface IMemberDetail
{
String Email { get; set; }
DataFields DataFields { get; set; }
}
public class MemberDetail : IMemberDetail
{
public String Email { get; set; }
public DataFields DataFields { get; set; }
}
public interface IDataFields
{
String RegisterCode { get; set; }
}
public class DataFields : IDataFields
{
public String RegisterCode { get; set; }
}
IocConfig:
public class IocConfig
{
//Autofac configuration
public static void Configure()
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<MemberStrategy>().As<IMemberStrategy>();
builder.RegisterType<DataFields>().As<IDataFields>();
builder.RegisterType<MemberDetail>().As<IMemberDetail>();
IContainer container = builder.Build();
AutofacWebApiDependencyResolver resolver = new
AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
}
我遇到的问题是,当我尝试使用这条线时,
memberDetail.DataFields.RegisterCode = 获取代码();
DataFields 为空。显然,Autofac 在构造 MemberDetail 对象时并没有创建它。
MemberDetail 在其构造函数中未显示对 IDataFields 的依赖。
因此 Autofac 不会为该依赖项创建实例。
像这样添加依赖项会注入一个实例:
public class MemberDetail : IMemberDetail
{
public String Email { get; set; }
public IDataFields DataFields { get; set; }
public MemberDetail(IDataFields dataFields)
{
DataFields = dataFields;
}
}
我还把DataFields的成员类型改成了IDataFields来做接口的依赖。
MemberDetail 对象已正确创建。您缺少的是 DataFields 属性 的初始化。将构造函数添加到 DataFields class 并在那里初始化 DataFields 属性,问题就会消失。
我是基于 Autofac 的依赖注入的新手。根据我们公司的要求,我必须在我正在创建的新网站 API 中使用它。以下是我的代码。我正在为 Autofac 使用 IocConfig class 在我的 Web API.
中创建和注入对象public class MemberCodeController : ApiController
{
private IMemberStrategy memberStrategy;
private IMemberDetail memberDetail;
//Constructor dependency injection through Autofac
public MemberCodeController(IMemberStrategy memberStrategy,
IMemberDetail memberDetail)
{
this.memberStrategy = memberStrategy;
}
public IHttpActionResult CreateMember(string email)
{
memberDetail.DataFields.RegisterCode = GetCode();
memberDetail.Email = email;
}
}
The MemberDetail class:
public interface IMemberDetail
{
String Email { get; set; }
DataFields DataFields { get; set; }
}
public class MemberDetail : IMemberDetail
{
public String Email { get; set; }
public DataFields DataFields { get; set; }
}
public interface IDataFields
{
String RegisterCode { get; set; }
}
public class DataFields : IDataFields
{
public String RegisterCode { get; set; }
}
IocConfig:
public class IocConfig
{
//Autofac configuration
public static void Configure()
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<MemberStrategy>().As<IMemberStrategy>();
builder.RegisterType<DataFields>().As<IDataFields>();
builder.RegisterType<MemberDetail>().As<IMemberDetail>();
IContainer container = builder.Build();
AutofacWebApiDependencyResolver resolver = new
AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
}
我遇到的问题是,当我尝试使用这条线时, memberDetail.DataFields.RegisterCode = 获取代码(); DataFields 为空。显然,Autofac 在构造 MemberDetail 对象时并没有创建它。
MemberDetail 在其构造函数中未显示对 IDataFields 的依赖。
因此 Autofac 不会为该依赖项创建实例。
像这样添加依赖项会注入一个实例:
public class MemberDetail : IMemberDetail
{
public String Email { get; set; }
public IDataFields DataFields { get; set; }
public MemberDetail(IDataFields dataFields)
{
DataFields = dataFields;
}
}
我还把DataFields的成员类型改成了IDataFields来做接口的依赖。
MemberDetail 对象已正确创建。您缺少的是 DataFields 属性 的初始化。将构造函数添加到 DataFields class 并在那里初始化 DataFields 属性,问题就会消失。