我在Autofac
注射或注册时遇到问题。
这是我的代码
存储库
namespace ClientConfiguration.Data.Repository
{
public class MappingBaseRepository : RepositoryBase<MappingBase>, IMappingBaseRepository
{
public MappingBaseRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{
}
}
public interface IMappingBaseRepository : IRepository<MappingBase>
{
}
}
服务
namespace ClientConfiguration.Service {
public interface IMappingBaseService
{
IEnumerable<MappingBase> GetElements();
void SaveElement();
}
public class MappingBaseService : IMappingBaseService
{
private readonly IMappingBaseRepository MappingBaseRepository;
private readonly IUnitOfWork unitOfWork;
public MappingBaseService(IMappingBaseRepository MappingBaseRepository, IUnitOfWork unitOfWork)
{
this.MappingBaseRepository = MappingBaseRepository;
this.unitOfWork = unitOfWork;
}
#region Members
public IEnumerable<MappingBase> GetElements()
{
var Elements = MappingBaseRepository.GetAll();
return Elements;
}
public void SaveElement()
{
unitOfWork.Commit();
}
#endregion
} }
Autofac init
private static void SetAutofacContainer() {
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerRequest();
// Repositories
builder.RegisterAssemblyTypes(typeof(ClientElementRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces().InstancePerRequest();
// Services
builder.RegisterAssemblyTypes(typeof(ClientElementService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces().InstancePerRequest();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
现在如果我在控制器内部,我有一个服务对象的实例没有问题。但我必须访问我的服务IMappingBaseService
才能从此类中的数据库中获取数据:
namespace ClientConfiguration.Mappings {
public class CustomDisplayNameAttribute : DisplayNameAttribute {
private static IMappingBaseService mappingBaseService { get; set; }
public CustomDisplayNameAttribute(string value)
: base(GetMessageFromResource(value)) {
}
private static string GetMessageFromResource(string value) {
var els = mappingBaseService.GetElements().ToList();
//get value from DB
//mappingBaseService is always null
return "";
}
}
}
任何帮助将不胜感激!提前谢谢。
答案 0 :(得分:0)
代码演示如:
grade5
答案 1 :(得分:0)
也许你可以修复代码注册autofac,因为autofac只注册接口,如:
builder.RegisterAssemblyTypes(typeof(IClientElementRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces().InstancePerRequest();
// Services
builder.RegisterAssemblyTypes(typeof(IClientElementService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces().InstancePerRequest();
builder.RegisterAssemblyTypes(typeof(IMappingBaseService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces().InstancePerRequest();
答案 2 :(得分:0)
解决方案是使用Property注入(在autofac init中实现类)
我们必须添加此行
builder.Register(c => new CustomDisplayNameAttribute {
_mappingBaseService = c.Resolve<IMappingBaseService>() });
并在CustomDisplayNameAttribute中添加空构造函数
public CustomDisplayNameAttribute() {}
和
public IMappingBaseService _mappingBaseService { get; set; }
并获取我们使用的对象
var _mappingBaseService = DependencyResolver.Current.GetService<IMappingBaseService>();
答案 3 :(得分:-1)
问题是我从DisplayNameAttribute(ASP.NET MVC)附加了CustomDisplayName
public class ClientElementsViewModel {
private static IMappingBaseService _mappingBaseService;
public ClientElementsViewModel(IMappingBaseService mappingBaseService) {
_mappingBaseService = mappingBaseService;
}
[Key]
[Display(Name = "Id")]
public long ClientElementId { get; set; }
[CustomDisplayName("", _mappingBaseService)]
public string CompanyCode { get; set; }
//[CustomDisplayName("")]
public string WebAppBaseUrl { get; set; }
//[CustomDisplayName("")]
public string GuestTraveller { get; set; }
}
我有这个错误
错误3属性参数必须是常量表达式typeof 表达式或数组创建表达式的属性参数 类型D:\ CDS_ADMIN \ ClientConfiguration.Web \ ViewModel \ ClientElementsViewModel.cs 22 32 ClientConfiguration.Web