2012-03-02 3 views
0

HybridHttpOrThreadLocalScoped 수명주기를 사용하여 제네릭 인스턴스가 생성되도록 닫힌 형식을 어떻게 등록합니까?StructureMap의 스캐너를 사용하여 닫힌 형식 등록

내 클래스 :

public interface IBaseService 
{ 
} 

public interface IAccountService 
{ 
    void Save(Account entry); 
    Account GetById(string id); 
    List<Account> GetList(); 
    void Delete(string id); 
    bool Exists(string id); 
} 

public interface IClientService 
{ 
    void Save(Client entry); 
    Client GetById(string id); 
    List<Client> GetList(); 
    void Delete(string id); 
    bool Exists(string id); 
} 

public class AccountService : IBaseService, IAccountService 
{ 
    Some code for managing accounts 
} 

public class ClientService : IBaseService, IClientService 
{ 
    Some code for managing clients 
} 

종속성 해결 :

public StructureMapContainer(IContainer container) 
    { 
     _container = container; 

     _container.Configure(x => x.Scan(y => 
     { 
      y.AssembliesFromApplicationBaseDirectory(); 
      y.WithDefaultConventions(); 
      y.LookForRegistries(); 
      y.ConnectImplementationsToTypesClosing(typeof(IService<>)) 
       .OnAddedPluginTypes(t => t.HybridHttpOrThreadLocalScoped()); 
     })); 

    } 

자동으로 IBaseService의 인스턴스를 생성하기위한 해결의 구문은 무엇입니까? ConnectImplementationsToTypesClosing 사용은 열린 제네릭에 대해서만 작동합니다. 리졸버를 사용해야합니까? 유형을 등록하는 더 좋은 방법이 있습니까? 지금은

, 이것은 내가 그들을 등록 amhandling 방법은 다음과 같은

container.Configure(x => 
     { 
      x.For<IClientService>() 
       .HybridHttpOrThreadLocalScoped() 
       .Use(new ClientService()); 

      x.For<IEmailAddressService>() 
       .HybridHttpOrThreadLocalScoped() 
       .Use(new EmailAddressService()); 

      x.For<IAccountService>() 
       .HybridHttpOrThreadLocalScoped() 
       .Use(new AccountService()); 
     }); 

답변

0

뭔가 :

Scan(y => 
    { 
     y.AssemblyContainingType<IService>(); 
     y.Assembly(Assembly.GetExecutingAssembly().FullName); 
     y.With(new ServiceScanner()); 
    }); 

그런 다음 당신이 Customscanner이 필요합니다

/// <summary> 
/// Custom scanner to create Service types based on custom convention 
/// In this case any type that implements IService and follows the 
/// naming convention of "Name"Service. 
/// </summary> 
public class ServiceScanner : IRegistrationConvention 
{ 
    public void Process(Type type, StructureMap.Configuration.DSL.Registry registry) 
    { 
     if (type.BaseType == null) return; 

     if (type.GetInterface(typeof(IService).Name) != null) 
     { 
      var name = type.Name; 
      var newtype = type.GetInterface(string.Format("I{0}", name)); 

      registry 
       .For<IService>() 
       .AddInstances(y => y.Instance(new ConfiguredInstance(type).Named(name))) 
       .HybridHttpOrThreadLocalScoped(); 

      registry.For(newtype) 
       .HybridHttpOrThreadLocalScoped().Use(c => c.GetInstance<IService>(name)); 
     } 
    } 
} 
+0

나는 그것을 시도 할 것이다. 감사! – rboarman

관련 문제