2017-07-13 5 views
1

。私は2つのマーカーのインターフェイスを持って大会https://github.com/castleproject/Windsor/blob/master/docs/registering-components-by-conventions.md登録インタフェース

を使用して、インターフェイスを登録します。私は達成しようとしています何

は今、私はユニティを経由していることをやりたい、私は過去に城ウィンザーに行っているものです。 DI用

public interface IPersonService:ISingletonDependency{ 
... 
...  
} 

public class PersonService: IPersonService{ 
... 
... 
... 
} 

私は同じように私のクラスのすべてのためにこれを行うと:私は私のリポジトリやサービスのクラスについては

public interface ISingletonDependency{} 
public interface ITransientDependency{} 

:私のサービスクラスで

public interface IPersonRepository:ISingletonDependency{ 
... 
... 

} 

public class PersonRepository: IPersonRepository{ 
... 
... 
... 
} 

登録時に私は以前にやっていました:

container.Register(
    Classes.NamedAssembly("SolutionName.Repository") 
     .BasedOn<ISingletonDependency>() 
     .WithService.FromInterface().LifestyleSingleton() 
); 


container.Register(
    Classes.NamedAssembly("SolutionName.Service") 
     .BasedOn<ISingletonDependency>() 
     .WithService.FromInterface().LifestyleSingleton() 
); 


container.Register(
    Classes.NamedAssembly("SolutionName.Repository") 
     .BasedOn<ITransientDependency>() 
     .WithService.FromInterface().LifestyleTansient() 
); 


container.Register(
    Classes.NamedAssembly("SolutionName.Service") 
     .BasedOn<ITransientDependency>() 
     .WithService.FromInterface().LifestyleTansient() 
); 

この方法では、これを毎回行う必要はありません。ユニティではアプローチがありましたが、命名規則 に基づいており、シングルトンまたは一時的なライフスタイルを個別に指定することはできません。

https://blogs.msdn.microsoft.com/agile/2013/03/12/unity-configuration-registration-by-convention/

あなたはマーカーインタフェースに基づいて、私が与えた上記の例のように、城ウィンザーで何ができるかを行う方法はありますか?

+1

'私は今、私はユニティ経由でそれをやりたい、過去に城ウィンザーに行っています。..あなたはなぜ完全にサポートされているDIコンテナから*デッド・ワンに移行していますか? Unityは[もはやサポートされていません](https://github.com/unitycontainer/unity/issues)、それが機能していても機能が大幅に遅れていました。マイクロソフトは[それをサポートしなかった](https://blogs.msdn.microsoft.com/dotnet/2015/08/21/the-future-of-unity/)、新しいオーナーはそれをサポートする気にはならないどちらかといえば、1年半でコミットされていない。 – NightOwl888

+0

+100 @ NightOwl888のコメント:) – Steven

答えて

0

は考える:

public interface ISingletonDependency { } 
public interface ITransientDependency { } 

public interface IPersonRepository : ISingletonDependency { } 
public class PersonRepository : IPersonRepository { } 

public interface IPersonService : ITransientDependency { } 
public class PersonService : IPersonService { } 

を次に、あなたが使用してタイプを登録できます。

var container = new UnityContainer(); 

container.RegisterTypes(
    AllClasses.FromAssembliesInBasePath().Where(
     // Could also match on namespace etc. here 
     t => t.Assembly.GetName().Name.StartsWith("SolutionName")), 
    WithMappings.FromMatchingInterface, 
    WithName.Default, 
    WithLifetime.FromMarkerInterface); 

// Singleton 
Debug.Assert(ReferenceEquals(container.Resolve<IPersonRepository>(), container.Resolve<IPersonRepository>())); 

// Transient 
Debug.Assert(!ReferenceEquals(container.Resolve<IPersonService>(), container.Resolve<IPersonService>())); 

WithLifetime.FromMarkerInterfaceは正しいLifetimeManagerを選択するために、マーカーインターフェイスを使用するカスタム規則です:

public static class WithLifetime 
{ 
    public static LifetimeManager FromMarkerInterface(Type type) 
    { 
     if (typeof(ISingletonDependency).IsAssignableFrom(type)) 
     { 
      return new ContainerControlledLifetimeManager(); 
     } 

     return new TransientLifetimeManager(); 
    } 
} 

この場合、すべてがISingletonDependency以外の場合は一時的ですが、ルールをより明示的にすることができます(例:有効なマーカーインターフェイスが見つからない場合はスローします)。

はあなたがAllClasses.FromAssembliesInBasePath().Whereであなたのwhere句を絞り込むことができれば、あなたはおそらく1つのライナーで逃げることができます:

container.RegisterTypes(
AllClasses.FromAssembliesInBasePath().Where(
    t => t.Namespace.StartsWith("SolutionName.Repository") || 
     t.Namespace.StartsWith("SolutionName.Service")), 
    WithMappings.FromMatchingInterface, 
    WithName.Default, 
    WithLifetime.FromMarkerInterface); 
関連する問題