IoC and Session

S

shapper

Hello,

I have a few repositories that share the same session:

public UserRepository(ISession session) {}
public RoleRepository(ISession session) {}

When I save do session.Commit() all the changes created by the
repositories will be submitted.

Now I am injecting the two repositories into a class using IoC.
How should I do this since they both should share the same session?

Thanks,
Miguel
 
A

Arne Vajhøj

I have a few repositories that share the same session:

public UserRepository(ISession session) {}
public RoleRepository(ISession session) {}

When I save do session.Commit() all the changes created by the
repositories will be submitted.

Now I am injecting the two repositories into a class using IoC.
How should I do this since they both should share the same session?

If your entire app only needs one session, then your IoC
framework should support singletons.

If you need to manage multiple sessions, then I think you
should create the repositories without session and then
set a session property that has the knowledge about which
should belong to what session.

Arne
 
S

shapper

If your entire app only needs one session, then your IoC
framework should support singletons.

If you need to manage multiple sessions, then I think you
should create the repositories without session and then
set a session property that has the knowledge about which
should belong to what session.

Arne

Hello,

My application has only one session. All repositories share that
session. I was able to do it this was:

Bind<IUserRepository>().To<UserRepository>().InTransientScope();

Bind<ISession>().To<Context>().InSingletonScope().WithConstructorArgument("path",
"~/Data");

Do you think that having the Repository in Transient Ccope and the
Session in Singleton Scope and the session as a parameter in the
Repository Constructor correct?

Thanks,
Miguel
 
A

Arne Vajhøj

My application has only one session. All repositories share that
session. I was able to do it this was:

Bind<IUserRepository>().To<UserRepository>().InTransientScope();

Bind<ISession>().To<Context>().InSingletonScope().WithConstructorArgument("path",
"~/Data");

Do you think that having the Repository in Transient Ccope and the
Session in Singleton Scope and the session as a parameter in the
Repository Constructor correct?

I don't know the IoC framework you are using.

I do know Spring.NET - in Spring you specify in the config whether
an object should be singleton (in this context this just means
the same instance used everywhere - it is not the GoF singleton)
or a new instance should be created for each reference.

Arne
 
A

Arne Vajhøj

I don't know the IoC framework you are using.

I do know Spring.NET - in Spring you specify in the config whether
an object should be singleton (in this context this just means
the same instance used everywhere - it is not the GoF singleton)
or a new instance should be created for each reference.

Here comes a Spring.NET example.

code
----

using System;

using Spring.Context;
using Spring.Context.Support;

namespace E
{
public interface I
{
string WhoAmI();
}
public class X : I
{
private static int totno = 0;
private int no;
private I o;
public X(I o)
{
this.o = o;
totno++;
no = totno;
}
public string WhoAmI()
{
return "I am X " + no + "/" + totno + " with: " + o.WhoAmI();
}
}
public class Y : I
{
private static int totno = 0;
private int no;
public Y()
{
totno++;
no = totno;
}
public string WhoAmI()
{
return "I am Y " + no + "/" + totno;
}
}
public class Program
{
public static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
for(int i = 0; i < 5; i++)
{
I o = (I)ctx.GetObject ("X");
Console.WriteLine(o.WhoAmI());
}
Console.ReadKey();
}
}
}

app.config
----------

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context"
type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects"
type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<object name="X" type="E.X" singleton="false">
<constructor-arg ref="Y"/>
</object>
<object name="Y" type="E.Y" singleton="true"/>
</objects>
</spring>
</configuration>

Output
------

I am X 1/1 with: I am Y 1/1
I am X 2/2 with: I am Y 1/1
I am X 3/3 with: I am Y 1/1
I am X 4/4 with: I am Y 1/1
I am X 5/5 with: I am Y 1/1

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top