Repository approach using IoC...

Repository approach using IoC...

Old forum URL: forums.lhotka.net/forums/t/8419.aspx


ballistic posted on Thursday, January 28, 2010

I am basing my new project on Rocky's DpRepos example from the Csla 3.8 videos. In there, Rocky uses a DalFactory to determine which DalManager to use, based on a setting in the config file.

Once the DalManager has been found, the DalManager then create the repository using the following code:
    private void DataPortal_Fetch(SingleCriteria<TestObject, int> criteria)
    {
      using (var dalFactory = DataAccess.DalFactory.GetManager())
      {
        var dal = dalFactory.GetProvider<DataAccess.ITestObjectDal>();
        using (var dr = new SafeDataReader(dal.Fetch(criteria.Value)))
        {
                    dr.Read();
                    ...
        }
      }
    }

Instead of using the config file to determine which DalManager to create, I would like to use IoC to inject the appropriate repository.  However, I would still like to keep the DalManager since it has the ConnectionManager<SqlConnection>, which according to the video, is used to maintain a connection open and avoid opening another connection if one already exists (which would cause us to default to using EnterpriseServices).

            using (var dalManager = IoC.Resolve<Repository.IDalManager>())
            {
                var dal = IoC.Resolve<Repository.Security.ITestObjectDal>(); 
                using (var dr = new SafeDataReader(dal.Fetch(criteria)))
                {
                    dr.Read();
                    ...
                }
            }

I would like to know if my approach is "correct" in the sense that the connections are still being reused.

Thank you,

 - Andy

RockfordLhotka replied on Thursday, January 28, 2010

I suspect this will work. You'll of course need to change all your IoC config entries to return the appropriate concrete types.

I personally dislike having a ton of config entries, which is why I prefer the approach I use in the video. Use config to get to the right DAL, and let that DAL manage its own concrete types as it sees fit.

But the IoC use-config-for-everything approach is trendy right now, and it should work fine.

xAvailx replied on Thursday, January 28, 2010

Hi Rocky: Will the repository examples be available on the next book or elsewhere?

RockfordLhotka replied on Thursday, January 28, 2010

The samples I created for the video series are part of the video series - they are (I think) a big part of the value of that product.

But as I create content products for CSLA 4 I'll almost certainly use that technique - it is a good technique, yes.

I don't know the form the content products will take for CSLA 4 at this time. They may be ebooks, a paper book or more videos. Probably some combination of those.

ballistic replied on Thursday, January 28, 2010

Thank you Rocky.

I just wanted to make sure I the approach I took did not remove the connection savings that the ConnectionManager provides.

 - Andy

RockfordLhotka replied on Thursday, January 28, 2010

ballistic:


I just wanted to make sure I the approach I took did not remove the connection savings that the ConnectionManager provides.

Ultimately that depends on how you implement all this stuff, but on the surface it seems like it should work fine.

Copyright (c) Marimer LLC