2012-03-02 9 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

関連する問題