Thanks for the great suggestions guys! Both gave me some more ideas and I
ended up using a combination of pretty much everything mentioned in this
thread :P
Here's what I got:
internal class EsentResolver : ISubDependencyResolver
{
private readonly IKernel _kernel;
public EsentResolver(IKernel kernel)
{
_kernel = kernel;
_kernel.Register(Component.For(typeof(IRepository<,>)).ImplementedBy(typeof(EsentRepository<,>)));
}
public object Resolve(CreationContext context,
ISubDependencyResolver contextHandlerResolver, ComponentModel model,
DependencyModel dependency)
{
return _kernel.Resolve(dependency.TargetType, new { directory =
"esent\\" + context.Handler.Service.Name });
}
public bool CanResolve(CreationContext context,
ISubDependencyResolver contextHandlerResolver, ComponentModel model,
DependencyModel dependency)
{
return dependency.TargetType.IsGenericType &&
dependency.TargetType.GetGenericTypeDefinition() == typeof(IRepository<,>);
}
}
On Sun, Apr 4, 2010 at 8:37 PM, Mauricio Scheffer <
[email protected]> wrote:
> Wouldn't it be easier to just register the corresponding
> EsentRepository on demand? i.e. when the component model is created
> you just register the repository and bind it to the component. Here's
> a prototype: http://bit.ly/9vCOA3
>
> On Apr 4, 10:31 pm, Bailey Ling <[email protected]> wrote:
> > Yep, I only did it without an argument to illustrate what value needs to
> go
> > into the repository. When everything's set up properly it'll have to be
> an
> > ctor arg or property injection.
> >
> > I gave a quick try at using a custom lifestyle manager. Before it even
> got
> > to Resolve() an exception was thrown trying to satisfy the 'directory'
> > dependency. I configured a dummy Parameters("directory").Eq("") to get
> past
> > that, but it looks like the ComponentModel is completely read-only when
> it
> > gets to the lifestyle manager's Resolve.
> >
> > Hmmmm...maybe this is getting a little more complicated than it's worth.
> > Ugh...really don't wanna use the service locator....
> >
> > 2010/4/4 Krzysztof Koźmic <[email protected]>
> >
> >
> >
> > > welll, first of all you need to actually inject the repo into A, not
> > > instantiate it directly.
> >
> > > You also will probably require custom lifestyle that will manage
> > > instances per dependent type. While it's not very clean, the lifestyle
> > > manager is probably also the best place to add the dependency (name of
> > > the dictionary) to its CreationContext so that Resolver and Activator
> > > will later pick it up and inject into the dictionary instance.
> >
> > > Is there a simpler way? It's late here, so I may miss something
> obvious.
> > > Krzysztof
> >
> > > 2010/4/4 Bailey Ling <[email protected]>:
> > > > Thanks for the suggestion, Mauricio. That would work, but I'm trying
> to
> > > go
> > > > with a more automatic way of accomplishing this. Krzysztof's comment
> is
> > > > spot on, and sent me down the path of looking at lifestyle managers
> and
> > > > activators, but it looks like I'm going to need to write some custom
> > > > extension code to accomplish this.
> >
> > > > To be specific, this is what I want to accomplish:
> >
> > > > public class EsentRepository<TKey, TValue> : IRepository<TKey,
> TValue> {
> > > > public EsentRepository(string directory) { }
> > > > }
> >
> > > > public class A {
> > > > public A() {
> > > > _repo = new EsentRepository(typeof(A).Name); // or any string
> > > directory
> > > > that is unique to instances of A
> > > > }
> > > > }
> >
> > > > How do I inject _repo?
> >
> > > > Thanks again.
> >
> > > > 2010/4/3 Krzysztof Koźmic <[email protected]>
> >
> > > >> Actually it feels to me more like you're looking after custom
> lifestyle.
> >
> > > >> So that all instances of A get single instance of PD, all instances
> of
> > > >> B get single, but different instance of PD, etc... is that correct?
> >
> > > >> 2010/4/4 Mauricio Scheffer <[email protected]>:
> > > >> > Like this?
> >
> > > >> > Component.For<PD>().Named("PDA")
> > > >> > Component.For<PD>().Named("PDB")
> >
> > >
> Component.For<A>().LifeStyle.Transient.ServiceOverrides(ServiceOverride.For
> Key("pd").Eq("PDA"))
> >
> > >
> Component.For<B>().LifeStyle.Transient.ServiceOverrides(ServiceOverride.For
> Key("pd").Eq("PDB"))
> >
> > > >> > On Apr 3, 12:11 am, Bailey Ling <[email protected]> wrote:
> > > >> >> That would work, if I only needed B once.
> >
> > > >> >> I'm gonna
> > > >> >> usehttp://managedesent.codeplex.com/(did<http://managedesent.codeplex.com/%28did>
> <http://managedesent.codeplex.com/%28did>you know Windows has
> > > >> >> a
> > > >> >> built in object database?!?) for local persistance. The
> > > >> >> PersistantDictionary takes in a string argument, which is the
> > > directory
> > > >> >> path
> > > >> >> for where the database files are stored.
> >
> > > >> >> I guess this is only half the question. What I'm trying to
> > > accomplish
> > > >> >> is a
> > > >> >> single instance of PersistantDictionary per dependency.
> >
> > > >> >> So if both A and B required a PD, I'd like all transients
> instances
> > > of
> > > >> >> A to
> > > >> >> get the "single" instance of PD/A, and all transients of B would
> get
> > > >> >> the
> > > >> >> single instance PD/B. Does that make a little more sense?
> >
> > > >> >> Thanks!
> >
> > > >> >> On Fri, Apr 2, 2010 at 5:22 PM, Mauricio Scheffer <
> >
> > > >> >> [email protected]> wrote:
> > > >> >> > Component.For<B>()
> >
> > >
> Component.For<A>().Parameters(Parameter.ForKey("name").Eq(typeof(B).Name))
> >
> > > >> >> > but I'm guessing that's not what you really want, right? Can
> you
> > > >> >> > further explain what you're trying to achieve?
> >
> > > >> >> > --
> > > >> >> > Mauricio
> >
> > > >> >> > On Apr 2, 5:13 pm, bling <[email protected]> wrote:
> > > >> >> > > class A {
> > > >> >> > > public A(string name) { }
> >
> > > >> >> > > }
> >
> > > >> >> > > class B {
> > > >> >> > > public B() {
> > > >> >> > > _a = new A(typeof(B).Name);
> > > >> >> > > }
> >
> > > >> >> > > }
> >
> > > >> >> > > Basically, that's what I'm trying to accomplish. B has a
> > > >> >> > > dependency
> > > >> >> > > on A, but I'd like A's name to be injected with the class
> name of
> > > >> >> > > B.
> > > >> >> > > So it's sort of like a circular dependency, but not directly.
> >
> > > >> >> > > ServiceOverrides, DependsOn, and DynamicProperties don't have
> any
> > > >> >> > > information about who is requesting the component. The
> Factory
> > > >> >> > > facility supplies the CreationContext, but if I use that then
> I
> > > >> >> > > need
> > > >> >> > > to manually bake in other built-in features like
> interceptors.
> >
> > > >> >> > > Short of writing a sub resolver is there a way to do what I
> want
> > > >> >> > > with
> > > >> >> > > fluent configuration? Thanks.
> >
> > > >> >> > --
> > > >> >> > You received this message because you are subscribed to the
> Google
> > > >> >> > Groups
> > > >> >> > "Castle Project Users" group.
> > > >> >> > To post to this group, send email to
> > > >> >> > [email protected]
> > > >> >> > .
> > > >> >> > To unsubscribe from this group, send email to
> >
> > > >> >> > [email protected]<castle-project-users%[email protected]><castle-project-users%2Bun
> [email protected]>
> > > <castle-project-users%2Bun
> > > >> >> > [email protected]>
> > > >> >> > .
> > > >> >> > For more options, visit this group at
> > > >> >> >http://groups.google.com/group/castle-project-users?hl=en.
> >
> > > >> > --
> > > >> > You received this message because you are subscribed to the Google
> > > >> > Groups "Castle Project Users" group.
> > > >> > To post to this group, send email to
> > > >> > [email protected].
> > > >> > To unsubscribe from this group, send email to
> > > >> > [email protected]<castle-project-users%[email protected]><castle-project-users%2Bun
> [email protected]>
> > > .
> > > >> > For more options, visit this group at
> > > >> >http://groups.google.com/group/castle-project-users?hl=en.
> >
> > > >> --
> > > >> You received this message because you are subscribed to the Google
> > > Groups
> > > >> "Castle Project Users" group.
> > > >> To post to this group, send email to
> > > >> [email protected].
> > > >> To unsubscribe from this group, send email to
> > > >> [email protected]<castle-project-users%[email protected]><castle-project-users%2Bun
> [email protected]>
> > > .
> > > >> For more options, visit this group at
> > > >>http://groups.google.com/group/castle-project-users?hl=en.
> >
> > > > --
> > > > You received this message because you are subscribed to the Google
> Groups
> > > > "Castle Project Users" group.
> > > > To post to this group, send email to
> > > [email protected].
> > > > To unsubscribe from this group, send email to
> > > > [email protected]<castle-project-users%[email protected]><castle-project-users%2Bun
> [email protected]>
> > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/castle-project-users?hl=en.
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Castle Project Users" group.
> > > To post to this group, send email to
> [email protected]
> > > .
> > > To unsubscribe from this group, send email to
> > > [email protected]<castle-project-users%[email protected]><castle-project-users%2Bun
> [email protected]>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/castle-project-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Castle Project Users" group.
> To post to this group, send email to [email protected]
> .
> To unsubscribe from this group, send email to
> [email protected]<castle-project-users%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/castle-project-users?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.