开发者

Registering generic types and services with Castle Windsor IoC

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.DataA开发者_Python百科ccess" />

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


The assembly name can be found in Visual Studio thus:

  • In the solution explorer, double-click the properties node
  • Open the Application tab
  • Assembly name is near the top right corner

Or, if you're compiling at the command line, you use the /out argument.

Also, you need to specify the assembly for the type arguments (inside the square brackets). So, assuming all your types are in the DataAccess assembly, and that the assembly is called (for brevity's sake) "DataAccess":

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

But I agree with other commenters that it's better to do the registrations in code. You don't have to use the verbose type syntax, for one, and you get compiler checking of your types. There are some disadvantages, however: it's harder to tell if you have unused types because the registration call counts as using the type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜