Hi, i´m having some big trouble (and maybe confusion) about using llbl with DI.
All the time i´m getting error messages about connection closed, transaction already running, null references, etc. and i cannot find why.
I have a MVC 2 webapplication, where Controllers contructors receive by injection new instance of a service class. That service class also receive by injection new instances of a repository class and finally this repository is the one that performs the call to llbl adapter.
Let me clarify this with some code:
public ProductController(IProductService service){...}
public ProductService(IProductRepository repository){...}
public ProductRepository(LinqMetaData linqMetaData : base(linqMetaData){...}
where BaseRepository is:
public abstract class BaseProductRepository : IDisposable
{
protected LinqMetaData _linqMetaData;
protected BaseProductRepository (LinqMetaData linqMetaData)
{
_linqMetaData = linqMetaData;
}
/*I´m no sure about this but anyway the problems also occur without this*/
public void Dispose()
{
_linqMetaData.AdapterToUse.Dispose();
}
}
Then, to save a product i just do:
public void SaveProduct(ProductEntity product)
{
_linqMetaData.AdapterToUse.SaveEntity(product);
}
And to get a collection of products:
public IQueryable<ProductEntity > GetProducts()
{
return _linqMetaData.Product;
}
Finally, the configuration of Structuremap:
public static class Bootstrapper
{
public static void ConfigureStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.AddRegistry(new ServicesRegistry());
x.AddRegistry(new RepositoryRegistry());
});
}
}
public class ServicesRegistry : Registry
{
public ServicesRegistry()
{
For<IProductRepository>().Use<ProductRepository>();
}
}
public class RepositoryRegistry: Registry
{
public RepositoryRegistry()
{
For<LinqMetaData>().HybridHttpOrThreadLocalScoped().Use(new LinqMetaData(new DataAccessAdapter()));
}
}
I would like to know if i have something really wrong and how can i solve this.
By the way, sometimes i have to restart Application Pool because all queries to database stop working (for example, users cant authenticate anymore via custom implemented membership service).