StructureMap:不同程序集中的PluginType和PluggedType

时间:2012-07-23 08:13:38

标签: structuremap

我用这个撞到了墙上,我无法弄明白:

我有以下界面:

namespace SupplyOrder.Objects.Interfaces
{
    public interface ILoginRepository<T>
    {
         T GetUser(string username, string password);
    }
}

namespace SupplyOrder.Objects.Interfaces
{
    public interface ILoginService
    {
        User GetUser(string username, string password);
    }
}

以及阻止此接口的类:

服务类:

namespace SupplyOrder.Service
{
    public class LoginService : ILoginService
    {
        readonly ILoginRepository<User> _loginRepository;
        public LoginService(ILoginRepository<User> loginRepository)
        {
            _loginRepository = loginRepository;
        }

        public User GetUser(string username, string password)
        {
            return _loginRepository.GetUser(username, password);
        }

    }
}

UserRepository类:

namespace SupplyOrder.Dal
{
    public class UserRepository : ILoginRepository<User>
    {
        public User GetUser(string username, string password)
        {
            // call to DB
        }
    }
}

在Bootstraper.cs文件中我有这个:

public class StructureMapControllerFactory : DefaultControllerFactory
{
    protected override IController
        GetControllerInstance(RequestContext requestContext,
        Type controllerType)
    {
        try
        {
            if ((requestContext == null) || (controllerType == null))
                return null;

            return (Controller)ObjectFactory.GetInstance(controllerType);
        }
        catch (StructureMapException)
        {
            System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
            throw new Exception(ObjectFactory.WhatDoIHave());
        }
    }
}

public static class Bootstrapper
{
    public static void Run()
    {
        ControllerBuilder.Current
            .SetControllerFactory(new StructureMapControllerFactory());

        ObjectFactory.Initialize(x =>
        {
            x.AddConfigurationFromXmlFile("StructureMap.xml");
        });
    }
}

StructureMap.xml配置:

<?xml version="1.0" encoding="utf-8" ?>
<StructureMap>
    <DefaultInstance
        PluginType="SupplyOrder.Objects.Interfaces.ILoginRepository`1, SupplyOrder.Objects"
        PluggedType="SupplyOrder.Dal.UserRepository, SupplyOrder.Dal"
    />

    <DefaultInstance
        PluginType="SupplyOrder.Objects.Interfaces.ILoginService, SupplyOrder.Objects"
        PluggedType="SupplyOrder.Service.LoginService, SupplyOrder.Service"
    />
</StructureMap>

当我运行我的MVC3网站时,它给了我这个错误:

System.TypeLoadException: Could not load type 'SupplyOrder.Dal.UserRepository' from     assembly 'SupplyOrder.Dal'.

如果所有类和接口都在同一个程序集中,那么它可以工作,但我想拥有 这一切都是有条理的。

感谢您的帮助, 贾尼

0 个答案:

没有答案