2013-03-26 2 views
0

내 모델 및 DataContext는 내 솔루션의 Web MVC 프로젝트에 대한 다른 프로젝트에 있습니다. AppHostConfigure 메서드 내 DataContext를 등록하려고하면 내 서비스가 사용할 리포지토리에서 사용할 때 DataContext가 여전히 null입니다.다른 프로젝트의 종속성 등록

Configure에 : 다음

container.Register<WarrantyContext>(c => new WarrantyContext()); 

내가 지금처럼 저장소에 종속성을 사용하려고하면

public WarrantyContext _db { get; set; } 

그것은 여전히 ​​null입니다. 다른 프로젝트의 종속성을 등록하려고 할 때해야 할 일이 있습니까?

+0

여러 프로젝트를 사용하는 특별한 필요가 없습니다. 그러나 프로퍼티 인젝션을 사용하고 싶다면'container.Register () .As (). PropertiesAutowired()'처럼 저장소를 등록하거나 생성자 인젝션을 대신 사용해야합니다. – nemesv

답변

1

을 조사 할 몇 가지 ...

  • 당신은 당신이있는 저장소 클래스를 가지고있는 것처럼 또한 그 소리는 here

  • 를 나열된 모든 단계를 수행했는지 확인 autofac를 사용하는 경우 WarrantyContext를 속성으로 사용합니다. 참고 here 상태 인 "위 메서드를 사용할 때 등록 된 형식의 속성과 생성자는 자동으로 연결되지 않습니다 (즉, 속성과 생성자가 삽입되지 않음). 수동으로 수행해야합니다 그 ". 따라서 서비스 (아래처럼)에 Repository (WarrantyContext 속성 포함) 클래스가 삽입되어있는 경우 수동으로 등록하지 않으면 WarrantyContext가 null이됩니다.

    //To inject WarrantyContext into a Repository that is injected to Service 
    container.Register<Repository>(c => new Repository() { _db = c.Resolve<WarrantyContext>() }); 
    
    
    public class Repository 
    { 
        public WarrantyContext _db { get; set; } 
    } 
    
    
    public class AService : Service 
    { 
        public Repository repo { get; set; } 
    
        public object Any(ARequest request) 
        { 
         //CODE 
        } 
    } 
    
관련 문제