本文整理汇总了C#中WindsorContainer.AddComponentLifeStyle方法的典型用法代码示例。如果您正苦于以下问题:C# WindsorContainer.AddComponentLifeStyle方法的具体用法?C# WindsorContainer.AddComponentLifeStyle怎么用?C# WindsorContainer.AddComponentLifeStyle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WindsorContainer
的用法示例。
在下文中一共展示了WindsorContainer.AddComponentLifeStyle方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: tt
public void tt()
{
using (var container = new WindsorContainer()) {
container.AddFacility<StartableFacility>();
container.AddComponentLifeStyle<A>(LifestyleType.Singleton);
container.AddComponentLifeStyle<B>(LifestyleType.Singleton);
}
}
示例2: Main
private static void Main()
{
IWindsorContainer container = new WindsorContainer();
// The container is needed by SecurityInterceptor
container.Kernel.AddComponentInstance("container", typeof(IWindsorContainer), container);
// SubDependencyResolver decides which implementation of ISecurityService should be returned,
// but both implementations have to be registered
container.Kernel.Resolver.AddSubResolver(new SubDependencyResolver(container.Kernel));
container.AddComponent<ISecurityService, SecureSecurityService>("security1");
container.AddComponent<ISecurityService, InsecureSecurityService>("security2");
// ModelInterceptorsSelector selects the interceptors to be added, but these have to be registered
container.Kernel.ProxyFactory.AddInterceptorSelector(new ModelInterceptorsSelector());
container.AddComponent("logging.interceptor", typeof(LoggingInterceptor));
container.AddComponent("security.interceptor", typeof(SecurityInterceptor));
// Every resolved instance of Person will be the same. It represents the 'currently logged-in user'
container.AddComponentLifeStyle("user", typeof(Person), LifestyleType.Singleton);
// This service needs to be transient or the SubDependencyResolver will only be called once
container.AddComponentLifeStyle("foo", typeof(Service), LifestyleType.Transient);
Person person = container.Resolve<Person>();
person.UserName = "richard";
// This FooService will be instantiated with an InsecureSecurityService
Service service1 = container.Resolve<Service>();
// Notice the output of LoggingInterceptor and the output of Service itself,
// which depends upon the implementation of ISecurityService
service1.Do();
// Pretend another user has logged in
person.UserName = "someone_else";
// This FooService will be instantiated with an SecureSecurityService
Service service2 = container.Resolve<Service>();
try
{
// SecurityInterceptor will throw here
service2.Do();
}
catch (SecurityException e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine();
Console.WriteLine("Press [Enter] to continue...");
Console.ReadLine();
}
示例3: release
public void release()
{
var container = new WindsorContainer(new MockProxyFactory());
//container.Kernel.ReleasePolicy = new AllComponentsReleasePolicy();
container.Kernel.ComponentDestroyed += Kernel_ComponentDestroyed;
container.AddComponentLifeStyle<SomeComponent>(LifestyleType.Transient);
var c1 = container.Resolve<SomeComponent>();
//container.Kernel.ReleaseComponent(c1);
container.Release(c1);
var c2 = container.Resolve<SomeComponent>();
Assert.AreNotSame(c1, c2);
}
示例4: WindsorControllerFactory
public WindsorControllerFactory()
{
_container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof (IController).IsAssignableFrom(t)
select t;
foreach (Type t in controllerTypes)
{
_container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
}
}
示例5: WindsorControllerFactory
// The constructor:
// 1. Sets up a new IoC container
// 2. Registers all components specified in web.config
// 3. Registers all controller types as components
public WindsorControllerFactory()
{
// Instantiate a container, taking configuration from web.config
_container = new WindsorContainer(
new XmlInterpreter(new ConfigResource("castle"))
);
// Also register all the controller types as transient
var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;
foreach (Type t in controllerTypes)
_container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
}
示例6: InitializeWindsor
/// <summary>
/// Instantiate the container and add all Controllers that derive from
/// WindsorController to the container. Also associate the Controller
/// with the WindsorContainer ControllerFactory.
/// </summary>
protected virtual void InitializeWindsor()
{
if (_container == null)
{
_container = new WindsorContainer();
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(Container));
_container
.RegisterControllers(typeof(HomeController).Assembly)
.AddComponent<IService, Service>()
.AddComponent<IPersonRepository, PersonInMemoryRepository>()
.AddComponentLifeStyle<ISessionProvider, SessionProvider>(LifestyleType.Singleton);
_container.AddComponentLifeStyle<IUnitOfWork, UnitOfWork>(LifestyleType.PerWebRequest);
}
}
示例7: tt
public void tt()
{
var tw = new StringWriter();
var wr = new SimpleWorkerRequest("/", Directory.GetCurrentDirectory(), "default.aspx", null, tw);
var module = new PerWebRequestLifestyleModule();
var ctx = HttpModuleRunner.GetContext(wr, new[] {module});
HttpContext.Current = ctx.Key;
var container = new WindsorContainer(new MockProxyFactory());
container.Kernel.ComponentDestroyed += Kernel_ComponentDestroyed;
container.AddComponentLifeStyle<SomeComponent>(LifestyleType.PerWebRequest);
var c1 = container.Resolve<SomeComponent>();
HttpModuleRunner.ProcessRequest(ctx.Value, ctx.Key);
container.Release(c1);
var c2 = container.Resolve<SomeComponent>();
Assert.AreNotSame(c1, c2);
}
示例8: InitializeWindsor
/// <summary>
/// Instantiate the container and add all Controllers that derive from
/// WindsorController to the container. Also associate the Controller
/// with the WindsorContainer ControllerFactory.
/// </summary>
protected virtual void InitializeWindsor()
{
if(_container == null)
{
_container = new WindsorContainer();
_container.AddComponent("ViewFactory", typeof(IViewEngine), typeof(BrailViewFactory));
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));
Type[] assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();
foreach(Type type in assemblyTypes)
{
if(typeof(IController).IsAssignableFrom(type))
{
_container.AddComponentLifeStyle(type.Name.ToLower(), type, LifestyleType.Transient);
}
}
}
}
示例9: SetUp
public void SetUp()
{
container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.AddComponentLifeStyle<IDummyComponent, Component1>(LifestyleType.Transient);
}
示例10: SubDependencyResolverHasHigherPriorityThanHandlerSelector
public void SubDependencyResolverHasHigherPriorityThanHandlerSelector()
{
IWindsorContainer container = new WindsorContainer();
container
.AddComponentLifeStyle<Person>(LifestyleType.Transient)
.AddComponent<IWatcher, BirdWatcher>("bird.watcher")
.AddComponent<IWatcher, SatiWatcher>("astronomy.watcher");
WatcherSelector selector = new WatcherSelector();
container.Kernel.AddHandlerSelector(selector);
container.Kernel.Resolver.AddSubResolver(new WatchSubDependencySelector());
selector.Interest = Interest.Biology;
Assert.IsInstanceOfType(typeof(SatiWatcher), container.Resolve<Person>().Watcher,
"sub dependency should resolve sati");
Assert.IsInstanceOfType(typeof(BirdWatcher), container.Resolve<IWatcher>(), "root dependency should resolve bird");
}
示例11: SelectUsingBusinessLogic_SubDependency
public void SelectUsingBusinessLogic_SubDependency()
{
IWindsorContainer container = new WindsorContainer();
container
.AddComponentLifeStyle<Person>(LifestyleType.Transient)
.AddComponent<IWatcher, BirdWatcher>("bird.watcher")
.AddComponent<IWatcher, SatiWatcher>("astronomy.watcher");
WatcherSelector selector = new WatcherSelector();
container.Kernel.AddHandlerSelector(selector);
Assert.IsInstanceOfType(typeof(BirdWatcher), container.Resolve<Person>().Watcher, "default");
selector.Interest = Interest.Astronomy;
Assert.IsInstanceOfType(typeof(SatiWatcher), container.Resolve<Person>().Watcher, "change-by-context");
selector.Interest = Interest.Biology;
Assert.IsInstanceOfType(typeof(BirdWatcher), container.Resolve<Person>().Watcher, "explicit");
}