Dal Factory Failure

Dal Factory Failure

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


st3fanus posted on Wednesday, December 07, 2011

Hi all

I wrote a DalFactory here, I think I have followed from ebook,

But there is still an error , which is

if (_dalType == null)
                    throw new ArgumentException(string.Format("Type {0} could not be found", dalTypeName));

_dalType is NULL.

 

public static class DalFactory
    {
        private static Type _dalType;
        public static IDalManager GetManager()
        {
            if (_dalType == null)
            {
                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);
        }
    }

 

anyone have suggestions or anything..

I'm in the middle of run a prototyping my small application use CSLA.NET 4

but this error arise :(

thanks a lot

st3fanus replied on Thursday, December 08, 2011

Hi all,

 

I have look on example source, I think my code have same with example,

but there is an error that :

_dalType = Type.GetType(dalTypeName);

_dalType always null !!!

this is my config :
    <add key="DalManagerType" value="SetiaMotor.MockDal.DalManager, SetiaMotor.MockDal" />

Project 1 => SetiaMotor.Dal project contain DalFactory implementation

AssemblyName = SetiaMotor.Dal

Namespace = SetiaMotor.Dal

 

Project 2 => SetiaMotor.MockDal contain DalManager Implementation

AssemblyName = SetiaMotor.MockDal

Namespace = SetiaMotor.MockDal

 

Is there something that I'missing ?

Could you give me a suggestion ??

let me know if you need additional info

 

thanks a lot

JonnyBee replied on Thursday, December 08, 2011

Hi,

You must make sure that SetiaMotor.MockDal.dll is in your bin-folder (or test folder).

If missing then add a reference to the mock assembly or add a build task that will copy the assembly to the bin folder.

st3fanus replied on Thursday, December 08, 2011

Hi jonny,

thanks a lot

that worked :)

 

Could you explain to me why it must be like that ?

thanks alot

 

 

st3fanus replied on Thursday, December 08, 2011

Hi jonny..

I'm sorry, I have forgotten about : How to do :

add a build task that will copy the assembly to the bin folder as your mean ? do you have an example ?

 

thanks

JonnyBee replied on Friday, December 09, 2011

Hi,

You cannot load a type from an assambly unless the .NET runtime is able to find that assembly. The default assembly resolver will look in the bin folder (actually current folder when running) or global assembly cache. You can hook into this to look other places.

Build tasks can be configured on the project properties - build events. You can add pre-build and post-build command line commands (lika a command script) when building the project - such as a custom task to copy an assembly from another project without having reference to the actual component in your project.

st3fanus replied on Saturday, December 10, 2011

Hi jonney..

 

thanks a lot jon..

It's solved :) , and I learn a bit more things

 

Gbu

kyuze replied on Tuesday, December 20, 2011

I added a post-build command that copies TechSlice.DAL.Mock.dll (which contains DALManager) to ..\TechSlice.DAL\bin\Debug.  TechSlice.DAL.dll contains DALFactory. The file is copied successfully but _dalType is still null.  What am I missing?

My config entry is still <add key="DalManagerType" value="TechSlice.DAL.Mock.DALManager,TechSlice.DAL.Mock"> 

Does the assembly still need to be fully qualified?

JonnyBee replied on Tuesday, December 20, 2011

Yes, the assembly must still be fully qualified. This is a requirement for System.Type.GetType to load the actual type.

See: http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx

lazaroms replied on Friday, January 04, 2013

13 months before: THANKS A LOT!!!!!

This is what I call a "very complete post".

There's not place left for mistakes.

Copyright (c) Marimer LLC