How to use Llbl 2.6 with Structuremap on a Pipe/Filters Architecture

Posts   
 
    
Posts: 9
Joined: 19-May-2006
# Posted on: 18-Aug-2010 15:22:05   

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).

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Aug-2010 17:06:39   

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).

Are you sure this has to do with LLBLGen Pro?

Posts: 9
Joined: 19-May-2006
# Posted on: 18-Aug-2010 17:23:02   

Walaa,

Of course i´m not sure, i´m asking for your opinion about how i´m using llbl in this context and if something sounds wrong for you.

Thank´s !

EDIT

I just re-read your post, about the application pool, definitely has something to do with the problem i describe (we have a lot of websites running in the same server, some with classic llbl, with no issues)

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Aug-2010 17:52:16   

All the time i´m getting error messages about connection closed, transaction already running, null references, etc. and i cannot find why.

We can't help unless we have a specific error to trace or a use case to follow. So let's start by sending us all the possible information about a specific error/exception.

Posts: 9
Joined: 19-May-2006
# Posted on: 18-Aug-2010 18:49:23   

For example, i jave three ajax calls from the view to the controller, which calls a service to get the answer for this calls. When i execute the ToList() method over an IQueryable returned by the repository i get:

SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException: An exception was caught during the execution of a retrieval query: Invalid operation. The connection is closed.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception. ---> System.InvalidOperationException: Invalid operation. The connection is closed.
   at System.Data.SqlClient.SqlConnection.GetOpenConnection()
   at System.Data.SqlClient.SqlConnection.get_Parser()
   at System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters)
   at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.Execute(CommandBehavior behavior)
   --- End of inner exception stack trace ---
   at SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.Execute(CommandBehavior behavior)
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchDataReader(IRetrievalQuery queryToExecute, CommandBehavior readerBehavior)
   at AgileTravel.Catalog.Database.DatabaseSpecific.DataAccessAdapter.FetchDataReader(IRetrievalQuery queryToExecute, CommandBehavior readerBehavior) in D:\Code\AgileTravel\src\AgileTravel.Catalog.Database\DatabaseSpecific\DataAccessAdapter.cs:line 292
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchProjection(List`1 valueProjectors, IGeneralDataProjector projector, IRetrievalQuery queryToExecute, Dictionary`2 typeConvertersToRun)
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchProjection(List`1 valueProjectors, IGeneralDataProjector projector, IEntityFields2 fields, IRelationPredicateBucket filter, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IGroupByCollection groupByClause, Boolean allowDuplicates, Int32 pageNumber, Int32 pageSize)
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProvider2.ExecuteValueListProjection(QueryExpression toExecute)
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.ExecuteExpression(Expression handledExpression)
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression)
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute(Expression expression)
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Aug-2010 07:18:31   

phantomlord wrote:

And to get a collection of products:


        public IQueryable<ProductEntity > GetProducts()
        {
            return _linqMetaData.Product;
        }

Here for example, when you return this IQueryable, the query isn't executed yet, then the metaData goes out of scope, so the adapter, and the connection as well. Thats why you receive that error.

I recommend that you call .ToList in your GetProducts method, so the final executed result is returned.

David Elizondo | LLBLGen Support Team