2016-07-25 4 views
0

내 프로젝트에서 자동 주입 DI가 있습니다. 내 프로젝트의 다른 모든 인터페이스가 상속하는 기존 인터페이스로 인터페이스를 공개하고 싶습니다. 시작 레벨에서 상속 된 인터페이스의 구성 요소를 자동으로 등록 할 수 있습니까? 예 :Autofac에서 기존 인터페이스를 등록하는 방법

Public interface IConvetionInterface {} 
public interface IImplementationA:IConvetionInterface 
{ 
public void DoSomethingA(); 
} 

public interface IImplementationB:IConvetionInterface 
{ 
public void DoSomethingB(); 
} 

생성자를 통해 주입.

public class ConsumerA 
    { 
     private readonly IImplementationA _a; 

     public DealerRepository(IImplementationA A) 
     { 
      _a= A; 
     } 

     public Act() 
     { 
      _a.DoSomethingA(); 


     } 

    } 

은 어떻게 Autofac의 모든 종속성 해결을 위해 IConvetionInterface을 등록 할.

+0

응용 프로그램 시작시 구성 요소를 등록하는 것과 같은 일을하지 않고 자동으로 무엇을 의미합니까? – Prashant

+0

감사합니다. Prashant. 그것이 내가 묻고있는 질문이다. 이러한 인터페이스 및 그 종속성을 시작 레벨에서 어떻게 등록합니까? – vicosoft

+0

당신이 발견 한 것은 DI 컨테이너 나 autofac을 사용하는 방법입니다. 응용 프로그램 시작시에 전혀 등록하고 싶지 않다고 생각하면 해피 코딩 솔루션을 찾았습니다. – Prashant

답변

0
Autofac Documentation Page

내가

public interface IRepository<TEntity, TPrimaryKey> where TEntity : class, IEntity<TPrimaryKey> 
    { } 

public class Repository<TEntity, TPrimaryKey> : RepositoryBase<TEntity, TPrimaryKey> 
     where TEntity : class, IEntity<TPrimaryKey>{} 
에 의해 구현 된 오픈 일반적인 인터페이스를 가지고 자신의 문서 페이지에 규정 된 나는 조립 스캔 구성을 autofac 사용하여이 솔루션을 마련 할 수 있었다

그런 다음 비어있는 인터페이스를 만들었습니다.

public interface IConventionDependency 
    { 

    } 

15,이 방법은 시작 단계 내에서 부품을 등록 불렸다 : 상기 등록함으로써

public static void RegisterAPSComponents(ContainerBuilder builder) 
     { 
      builder.RegisterType<APSContext>().InstancePerRequest(); 
      builder.RegisterGeneric(typeof(Repository<,>)).As(typeof(IRepository<,>)).InstancePerLifetimeScope(); 




    builder.RegisterAssemblyTypes(typeof(IConventionDependency).Assembly).AssignableTo<IConventionDependency>().As<IConventionDependency>().AsImplementedInterfaces().AsSelf().InstancePerLifetimeScope(); 



     } 

, IConventionDependency 용기에 자동으로 등록 할 수있는 인터페이스를 상속.

예 : 인터페이스를 만들 :

public interface IDealerRepository : IConventionDependency 
    { 
     List<Dealers> GetDealers(); 
    } 

다음 인터페이스를 구현 : 결론에

public class DealerRepository : IDealerRepository 
    { 
     private readonly IRepository<VTBDealer, int> _repository; 

     public DealerRepository(IRepository<VTBDealer, int> repository) 
     { 
      _repository = repository; 
     } 

     public List<Dealers> GetDealers() 
     { 
      return _repository.GetAllList().MapTo<List<Dealers>>(); 


     } 

    } 

을 명시 적으로 IDealerRepository를 등록하지 않고, 그것은 MVC 컨트롤러 생성자에서 해결됩니다.

관련 문제