Let your container be your factory. :)
If you are using LinqToSql and dependency injection, then you have probably created a factory with which you create DataContexts. But what if you could just let your IOC Container do that work for you? You can!
If you are using Unity then you can inject a Func<T> of any registered type. Unity will automatically bind the injected Func to the container's resolve method, thus preserving the resource Lifetime Manager.
Example Code
public class FuncInjectionTests
{
[Fact]
public void TransientLifetimeFuncIsThreadsafe()
{
var container = new UnityContainer();
container
.RegisterType<IUserService, UserService>(
new ContainerControlledLifetimeManager())
.RegisterType<IDataContext, DataContext>(
new TransientLifetimeManager());
var parallelOptions = new ParallelOptions {MaxDegreeOfParallelism = 100};
Parallel.For(0, 1000, parallelOptions, i =>
{
var userService = container.Resolve<IUserService>();
Parallel.For(0, 1000, parallelOptions, j =>
{
userService.Update();
});
});
Assert.Equal(1, UserService.Count);
Assert.Equal(1000000, DataContext.Count);
}
}
public interface IUserService
{
void Update();
}
public interface IDataContext : IDisposable
{
void UpdateUser();
}
public class UserService : IUserService
{
public static int Count;
private readonly Func<IDataContext> _dataContextFactory;
public UserService(Func<IDataContext> dataContextFactory)
{
_dataContextFactory = dataContextFactory;
Interlocked.Increment(ref Count);
}
public void Update()
{
using (var dataContext = _dataContextFactory())
dataContext.UpdateUser();
}
}
public class DataContext : IDataContext
{
public static int Count;
public DataContext()
{
Interlocked.Increment(ref Count);
}
public void UpdateUser()
{
Trace.WriteLine(Thread.CurrentThread.ManagedThreadId + " - " + Count);
}
public void Dispose()
{
}
}
Enjoy,
Tom
Didn't knew that IOC Container can manage this so well. But still I think there will be some limitation to it. I am new to ASP.NET and it would be great help if you let me know this clearly.
ReplyDeleteThis pattern has worked flawlessly for me. What limitations are you concerned about?
ReplyDeleteGreat write-up, I am a big believer in commenting on blogs to inform the blog writers know that they’ve added something worthwhile to the world wide web!.. 2k injection molding
ReplyDeleteHi, constantly i used to check blog posts here in the early hours in the daylight, as i love
ReplyDeleteto gain knowledge of more and more wecare riteaid com
well written post. It`s so useful…thank you
ReplyDeletepandoralistens net
Nice article,Having concern on wild life..Its’s good to think on wild life because we human ready acquired all over earth. It means we taking land if wild animal for human usage.. That DNA talk is perfect..Thank you introduce some new concepts in mind..keep it up..
ReplyDeletegreatpeople me kroger employee login-portal
play on our online poker game and get the blessings
ReplyDeletejudi togel
slot gacor hari ini
Every week, Pizza Hut gives its patrons the opportunity to win a $1000 cash reward. All that's required is that you take part in their Tellpizzahut online poll. tellpizzahut.com
ReplyDeleteparticipating in the survey, you not only get a delicious perk but also help Chick-fil-A continue to provide exceptional food and service. MyCFAVisit
ReplyDeleteEnter the revelation desired in front of the survey, hindering that moment of truth and ending of a shop visit, in addition to clicking “next”. -
ReplyDeletehttps://talktowendysus.shop/