I inject a session as a ctor dependency like this
class Service
{
private readonly ISession session;
public Service(ISession session)
{
this.session = session;
}
public void DoWork(int id)
{
var entity = session.Load<Entity>(id);
session.Delete(entity)
}
}
class SessionResolver : ISubDependencyResolver
{
private readonly ISessionFactory factory;
public SessionResolver(ISessionFactory factory)
{
this.factory = factory;
}
public object Resolve(...)
{
return new SessionAdapter(factory);
}
public bool CanResolve(...)
{
return typeof(ISession).IsAssignableFrom
(dependency.TargetType);
}
}
class SessionAdapter : ISession
{
private readonly ISessionFactory factory;
public SessionAdapter(ISessionFactory factory)
{
this.factory = factory;
}
private ISession session { get { return factory.GetCurrentSession
(); } }
private T Load<T>(object id)
{
return session.Load<T>(id);
}
private Delete(object entity)
{
return session.Delete(entity);
}
//the rest of the ISession members...
}
On Sep 18, 9:25 am, Martin Nilsson <
mffmar...@gmail.com> wrote:
> How does your repositories looks like? Because you can't take the dependency
> to ISession as a ctor? I'm replacing Rhino Commons with plain session
> management but I'm not sure how to interact between my services and
> repositories. Can you give a brief example. Something like this
>
> ProductService:
> public void Save(Product product) {
> using(var session = ?.GetSession)
> using(var tx = session.BeginTransaction())
> {
> repository.Add(product);
> tx.Commit();
> }
>
> }
>
> ProductRepository:
> public void Add(Product product) {
> session.SaveOrUpdate(product);
>
> }
>
> Does your repository have a dependency to IKernel maybe?
>
> On Thu, Sep 10, 2009 at 8:35 PM, Ayende Rahien <
aye...@ayende.com> wrote:
> > I am talking about something like:
>
> > kernel.Register( Component.For<ISession>()
> > .FactoryMethod( () => HttpContext.Items["current-session"]) )
> > );
>
> > On Thu, Sep 10, 2009 at 9:33 PM, Tyler Burd <
tylerb...@gmail.com> wrote:
>
> >> When you say "ambient session from the container", do you mean actually
> >> injecting the ISession? Do you accomplish this via child containers when
> >> needing a session-per-web-request or something similar? Or do you use a
> >> per-web-request life-cycle when you register ISession with the container?
>
> >> On Thu, Sep 10, 2009 at 12:20 PM, Ayende Rahien <
aye...@ayende.com>wrote:
>
> >>> Yes, that is a good scenario for Binsor.But it is actually not something
> >>> that I tend to do
>
> >>> On Thu, Sep 10, 2009 at 9:06 PM, Nathan Stott <
nrst...@gmail.com> wrote:
>
> >>>> I disagree. Binsor, on several occasions, has saved me a lot of
> >>>> headache by allowing me to reconfigure my apps in production without having
> >>>> to recompile them. Windsor Fl does not allow for this.
>
> >>>> On Thu, Sep 10, 2009 at 2:02 PM, Ayende Rahien <
aye...@ayende.com>wrote:
>
> >>>>> To be absolutely honest, with the Windsor FI, I am not sure there is a
> >>>>> lot of place for Binsor.
>