DalFactory NullReferenceException - Type.GetType("string type") returning null

DalFactory NullReferenceException - Type.GetType("string type") returning null

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


Mar72Vin posted on Friday, March 15, 2013

Hi All,

I'm am using a DalFactory (shown below) but Type.GetType(dalTypeName); always returns null. The only way I am able to make it work is by adding a reference to the "MyProject.DalSql" project from "MyProject.Dal"

I also need to instantiate an object of the type first; e.g. MyProject.DalSql.DalManager _dalmanager = new MyProject.DalSql.DalManager(); before the Type.GetType will work as expected.

 

Does anyone have any idea why this might be happening?

 

Thanks in advance!

 

//from web.config file.

<add key=".DalManagerType" value="MyProject.DalSql.DalManager, MyProject.DalSql, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

// I have also simply tried with the same result

<add key=".DalManagerType" value="MyProject.DalSql.DalManager, MyProject.DalSql/>

 

 

(namespace MyProject.Dal)

public static class DalFactory
    {
        private static Type _dalType;

        /// <summary>Gets the  DAL manager type that must be set
        /// in the <strong>appSettings</strong> section of the application .config file.</summary>
        /// <returns>A new <see cref="IDalManager"/> instance</returns>
        public static IDalManager GetManager()
        {
            if (_dalType == null)
            {

               //dodgy work around//MyProject.DalSql.DalManager _dalmanager = new MyProject.DalSql.DalManager();       

      
                var dalTypeName = ConfigurationManager.AppSettings[".DalManagerType"];
                if (!string.IsNullOrEmpty(dalTypeName))
                    _dalType = Type.GetType(dalTypeName);
                else
                    throw new NullReferenceException(".DalManagerType");
                if (_dalType == null)
                    throw new ArgumentException(string.Format("Type {0} could not be found", dalTypeName));
            }
            return (IDalManager) Activator.CreateInstance(_dalType);
        }
    }

Mar72Vin replied on Tuesday, July 02, 2013

Has anyone else experienced anything like this?

JonnyBee replied on Tuesday, July 02, 2013

Mar72Vin

I'm am using a DalFactory (shown below) but Type.GetType(dalTypeName); always returns null. The only way I am able to make it work is by adding a reference to the "MyProject.DalSql" project from "MyProject.Dal"

When you have no direct reference to the DalSql assembly it is not copied to your bin folder and thus not available for your application. Then when you add a reference it will be copied to the output folder by the compiler. You must make sure to get all the necessary assemblies into the bin folder of your app - with the correct build/version number so they can be loaded. 

 

Mar72Vin replied on Thursday, July 04, 2013

JonnyBee, you are absolutely right! I can't believe I missed that.

What method do you use to ensure the correct version is copied to your bin folder during development?

e.g. in an MVC application an easy solution would be to just add MyProject.DalSql project as a reference, but that kind of defeats the purpose of separating the data access from the front end developer.

Adding the reference to MyProject.Dal creates a circular reference.

Do you just use a Post Build event and copy the file to the correct location?

JonnyBee replied on Thursday, July 04, 2013

Yes,either a post build script or as part of the build script (I typically use TFS).

If you crate an installer package you must also make sure to add the DAL to the deploy assemblies.

Mar72Vin replied on Thursday, July 04, 2013

Thanks JonnyBee! your a champion!

Copyright (c) Marimer LLC