我用这个撞墙,我不知道:
我有以下接口:
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'.
如果所有类和接口都在同一个程序集中,它可以工作,但我希望以结构化的方式拥有所有这些。
谢谢你的帮助,贾尼