views:

48

answers:

0

Hello again stackoverflowians,

I thought it was about time that I learnt how to use a DI framework. I've heard a lot of good things about Castle Windsor so I decided to go with that. Now there are PLENTY of tutorials out there on how to use it, however, I cannot find much useful information about what to do when Generics get involved. Here is my issue.

I have a BaseDAO

namespace Utilities.DataAccess
{
    public class BaseDAO<T> : IBaseDAO<T>
    {
        public BaseDAO(IConnectionProvider _connectionProvider)
        {
           // Stuff
        }
    }
} 

Im a little bit new to generics in this context and I have seen some tutorials which have a 'BaseDAO' with no generic declaration and simply the interface it implements with the generics on it. I have used the above way of doing things on many previous projects (without IoC) and its worked fine for me...anyways, onwards to the App.config !

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section
      name="castle"
      type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"></section>
  </configSections>

  <castle>
    <components>

      <component
        id="BaseDAO"
        service="Utilities.DataAccess.Interfaces.IBaseDAO`1, Utilities.DataAccess"
        type="Utilities.DataAccess.BaseDAO`1, Utilities.DataAccess" />

      <component
        id="NHibernateConnection"
        service="Utilities.DataAccess.ConnectionProviders.IConnectionProvider, Finchtils"
        type="Utilities.DataAccess.ConnectionProviders.NHibernateConnection" />

      <component
        id="XMLConnection"
        service="Utilities.DataAccess.ConnectionProviders.IConnectionProvider, Finchtils"
        type="Utilities.DataAccess.ConnectionProviders.XMLConnection, Utilities" />
    </components>
  </castle>
</configuration>

Now as some of you may of figured by now, this is a utility library. I intend to use this assembly for each project I create so that I don't have to write the same data access code which remains the same across all solutions. The implications of such of course is that I cannot tell castle exactly what type parameter I will pass to the BaseDAO, in one project it might be a Customer object, another entirely different. I have read on other forums that this is entirely possible as when you request the object from the container you can specify the type then like;

BaseDAO<Customer> baseDao = container.Resolve<BaseDAO<Customer>>();

Although it is against my design efforts, I have tried to use the following notation in the App.config

<component
    id="BaseDAO"
    service="Utilities.DataAccess.Interfaces.IBaseDAO`1[[Utilities.DataInterface.IEntity]], Finchtills.DataAccess"
    type="Utilities.DataAccess.BaseDAO`1[[Utilities.DataInterface.IEntity]], Finchtils.DataAccess" />

However, this has not worked either, in any case I get the following error:

Utilities.Testing.DataAccess.Unit.Testing_BaseDAO (TestFixtureSetUp):
System.Exception : The type name Utilities.DataAccess.BaseDAO`1, Utilities.DataAccess could not be located.
  ----> System.IO.FileNotFoundException : Could not load file or assembly 'Utilities.DataAccess' or one of its dependencies. The system cannot find the file specified.

Reading this error, I think it could be one of two things:

  1. I am missing something from the config file to do with the generics of the types and services.

  2. I have named something incorrectly I.E an assembly name.

I have treated the assembly name as the project that item is contained within, in other words, at no point have i used <solution name>.<project name>.<item folder>.<item name> but merely started at the project level...I assume that any config option would know what solution it is being called from.

Thank you for any help you may be able to give on this subject.