In-line service dependencies rewrite up to new revision

28 views
Skip to first unread message

JustUser

unread,
Feb 21, 2012, 4:03:28 PM2/21/12
to Castle Project Users
Since Castle.Windsor was updated to 3.0.0.4001 ServiceOverrides method
became obsolete. So I need help to rewrite my old code to new style
with using DependsOn method.

container.Register(AllTypes.FromAssemblyContaining(typeof(IRepository))
.BasedOn<IRepository>()
.If(Component.IsInSameNamespaceAs<BaseEntityRepository<BaseEntity>>())
.If(t =>
t.Name.EndsWith("Repository") &&
(t.Name.StartsWith("Foo") || t.Name.StartsWith("Bar")))
.Configure(c =>

c.ServiceOverrides(ServiceOverride.ForKey("Session").Eq("db.session.second"))
.ServiceOverrides(ServiceOverride.ForKey("SessionFactory").Eq("db.sessionfactory.second"))
.LifeStyle.PerThread));
I tried to rewrote this code like this:

container.Register(Component.For<IRepository>().ImplementedBy<FooRepository>()
.DependsOn(new Dictionary<string, string>
{{"Session", "db.session.second"},
{"SessionFactory", "db.sessionfactory.second"}})
.LifeStyle.PerThread);

but got null when tried to Resolve FooRepository object. Also with
this approach I can't override services for several components (like
first approach with If using), so I need write individual Register
line for each component. All other code stay same and work fine.

JustUser

unread,
Feb 21, 2012, 4:42:13 PM2/21/12
to Castle Project Users
Found a solution:


container.Register(Classes.FromAssemblyContaining(typeof(IRepository))
.BasedOn<IRepository>()
.If(Component.IsInSameNamespaceAs<BaseEntityRepository<BaseEntity>>())
.If(t => t.Name.EndsWith("Repository") &&
(t.Name.StartsWith("Foo") || t.Name.StartsWith("Bar")))
.Configure(c =>
c.DependsOn(ServiceOverride.ForKey("Session").Eq("db.session.second")).DependsOn(ServiceOverride.ForKey("SessionFactory").Eq("db.sessionfactory.second"))).LifestylePerThread());

> Since Castle.Windsor was updated to 3.0.0.4001 ServiceOverrides method
> became obsolete. So I need help to rewrite my old code to new style
> with using DependsOn method.
>
> container.Register(AllTypes.FromAssemblyContaining(typeof(IRepository))
>   .BasedOn<IRepository>()
>   .If(Component.IsInSameNamespaceAs<BaseEntityRepository<BaseEntity>>())
>   .If(t =>
>       t.Name.EndsWith("Repository") &&
>           (t.Name.StartsWith("Foo") || t.Name.StartsWith("Bar")))
>   .Configure(c =>
>
> c.ServiceOverrides(ServiceOverride.ForKey("Session").Eq("db.session.second" ))
>       .ServiceOverrides(ServiceOverride.ForKey("SessionFactory").Eq("db.sessionfa ctory.second"))
Reply all
Reply to author
Forward
0 new messages