对于我的一个客户,我目前正在构建一个与传统Microsoft Access数据库通信的应用程序.遗憾的是,迁移到SQL服务器不是一个选项.我现在写使用的查询OleDbConnection,OleDbCommand并根据-良好的文字OLD-查询.你可以想象我使用现代的O/RM工具有点被宠坏了,我觉得我回到过去了.
哪个O/RM工具支持Microsoft Access,所以我可以摆脱这种丑陋?
我正在使用MVC3网站,试图使用Ninject来解决我的依赖关系.我有以下场景:
public class UserModelBinder : IModelBinder
{
//[Inject]
public UserDataService userData { get; set; }
public object BindModel(
ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
Guid UserID =
(Guid)Membership.GetUser().ProviderUserKey;
//userDataService = DependencyResolver.Current
// .GetService<UserDataService>();
User user = userDataService.GetUser(UserID);
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
注意到注释的代码行?
我确实将活页夹注册Global.asax为
ModelBinders.Binders[typeof(User)] = new UserModelBinder();
Run Code Online (Sandbox Code Playgroud)
所以我不能真正通过施工注射.
UserDataService有一连串的依赖:UserDataService -> UserRepository -> Context.所以在这里使用Ninject会很好.
问题是,当我取消注释[Inject]上面的userData声明,并尝试让Ninject注入对象作为参数时,它由于某种原因不起作用:我得到空引用异常.
(可能是UserDataService没有接口,我将对象绑定到自身:kernel.Bind<UserDataService>().ToSelf();??)
我在代码中有另一个注释行:
userDataService = DependencyResolver.Current
.GetService<UserDataService>();
Run Code Online (Sandbox Code Playgroud)
如果取消注释,设置工作,我会插入正确的对象,但现在我们依赖DependencyResolver,这并不比说 userDataService = new UserDataService()
我错过了什么吗?是否有另一种方法将对象作为参数注入,而不是引入对Ninject或DependencyResolver的依赖?
我有一个界面:
public interface IService
{
void DoStuff(int parm1, string parm2, Guid gimmeABreakItsAnExampleK);
}
Run Code Online (Sandbox Code Playgroud)
我想配置Ninject(v3)绑定,以便我可以有一个"调度程序"shuffle方法调用多个实例IService,如下所示:
public sealed class DispatcherService : IService
{
private IEnumerable<IService> _children;
public DispatcherService(IEnumerable<IService> children)
{
this._children = children.ToList();
}
public void DoStuff(int parm1, string parm2, Guid gimmeABreakItsAnExampleK)
{
foreach(var child in this._children)
{
child.DoStuff(parm1, parm2, gimmeABreakItsAnExampleK);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我看起来像这样的绑定最终会在运行时抛出异常,表明存在循环依赖:
this.Bind<IService>().To<DispatcherService>();
this.Bind<IService>().To<SomeOtherService>()
.WhenInjectedExactlyInto<DispatcherService>();
this.Bind<IService>().To<YetAnotherService>()
.WhenInjectedExactlyInto<DispatcherService>();
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果是这样,我做错了什么?忍者可以逃脱这种周期性的依赖厄运吗?
我是Unity Game引擎的初学者.
我想将一个物体从他的位置移动到另一个物体的位置.
我有一个新的MVC Web项目,我在MVC和WebApi中使用.我在我的全局文件中使用以下代码设置了Simple Injector(来自nuGet的2.5.2版本)
// Register Injectors
SimpleInjectorConfig.Register();
Run Code Online (Sandbox Code Playgroud)
在我的SimpleInjectorConfig.cs文件中
public class SimpleInjectorConfig
{
public static void Register() {
// Create the container as usual.
Container container = new Container();
// services
container.Register<IService, MyService>();
// data
container.Register<IRepository, MyRepository>();
// Register your types, for instance using the RegisterWebApiRequest
// extension from the integration package:
container.RegisterMvcControllers(
System.Reflection.Assembly.GetExecutingAssembly());
container.RegisterMvcAttributeFilterProvider();
// This is an extension method from the integration package.
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
// verify its all ok
container.Verify();
// add dependency
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
} …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc dependency-injection controller simple-injector
机器:Mac UnityVer:4.6.2
当你制作了一个统一的jenkins构建时,会出现以下错误.
----- Total AssetImport总时间:6.818127s,AssetImport时间:6.808909s,资产哈希值:0.005416s [60.9 KB,10.972655 mb/s]
平台程序集:/Applications/Unity4.6.2/Unity.app/Contents/Frameworks/Mono/lib/mono/2.0/System.Data.dll(此消息无害)以前使用的系统内存:71.7 MB.卸载137未使用的序列化文件(现在加载的序列化文件:0 /脏序列化文件:0)使用后的系统内存:66.8 MB.
卸载0个未使用的资产以减少内存使用量.现在加载的对象:6940.总计:18.863014 ms(FindLiveObjects:0.248059 ms CreateObjectMapping:0.096515 ms MarkObjects:1.745080 ms DeleteObjects:0.012070 ms)
脚本有编译器错误.
(文件名:/Users/builduser/buildslave/unity/build/Runtime/Utilities/Argv.cpp行:127)
由于失败而中止批处理模式:脚本存在编译器错误.
线程'UnityLookForNewInputDevices'仍在运行!
(文件名:/Users/builduser/buildslave/unity/build/Runtime/Threads/Thread.cpp行:68)
线程没有清理干净!
(文件名:/Users/builduser/buildslave/unity/build/Runtime/Threads/Posix/PlatformThread.cpp行:45)
致命:Unity3d命令行执行失败,状态为1构建步骤'调用Unity3d编辑器'标记构建为失败FTP:当前构建结果为[FAILURE],不会运行.完成:失败
我的服务接口有一个名称空间 Services.Interfaces
Service Interfaces的实现具有名称空间 Web.UI.Services
例如,我有2个服务实现
这就是我目前使用SimpleInjector注册这些服务的方法.
container.Register<IUserService, UserService> ();
container.Register<ICountryService, CountryService> ();
Run Code Online (Sandbox Code Playgroud)
问题:如果我有超过100个服务夸大了一点.我需要为每项服务添加一行.
如何使用Simple Injector将所有实现从一个程序集注册到所有接口形成另一个程序集?
c# asp.net-mvc ioc-container inversion-of-control simple-injector
我有一个Web API,它是一个非常薄的基础结构,只包含两个DelegatingHandler实现,它们将传入的消息分派给业务层中定义的消息处理程序实现.这意味着没有控制器,也没有控制器动作; API仅基于消息定义.这意味着在实现新功能时,不需要此基础结构层中的代码更改.
例如,我们有以下消息:
委托处理程序根据url确定确切的消息,并将请求内容反序列化为该消息类型的实例,之后该消息将转发到相应的消息处理程序.例如,这些消息(当前)映射到以下URL:
可以想象,这种使用Web API的方式简化了开发并提高了开发性能; 编写的代码更少,测试代码更少.
但由于没有控制器,我在Swashbuckle中引导这个问题; 阅读完文档后,我没有找到在Swashbuckle中注册这些网址的方法.有没有办法以这样的方式配置Swashbuckle它仍然可以输出API文档?
为了完整起见,可以在此处找到演示此内容的参考架构应用程序.
在ASP.NET Core 2.0中,有一种方法可以通过实现IHostedService接口来添加后台任务(请参阅https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosted-services?view=aspnetcore-2.0).通过本教程,我能够使它工作的方式是在ASP.NET Core容器中注册它.我的目标是从队列中读取消息并在后台处理作业; 消息将发布到队列(通过控制器操作),然后在定时间隔内在后台处理.
// Not registered in SimpleInjector
services.AddSingleton<IHostedService, MyTimedService>();
Run Code Online (Sandbox Code Playgroud)
当我将此注册放在ASP.NET Core容器中时,它会在应用程序启动时自动启动该过程.但是,当我在SimpleInjector中注册时,服务不会自动启动.我相信这是因为我们只使用MvcControllers和MvcViewComponents注册SimpleInjector容器:
// Wire up simple injector to the MVC components
container.RegisterMvcControllers(app);
container.RegisterMvcViewComponents(app);
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是当我想开始从SimpleInjector注入组件寄存器(例如存储库,带有装饰器的通用处理程序......)到IHostedService的实现中时,如下所示:
public class TimedService : IHostedService, IDisposable
{
private IJobRepository _repo;
private Timer _timer;
public TimedService(IJobRepository repo)
{
this._repo = repo;
}
...
...
...
}
Run Code Online (Sandbox Code Playgroud)
由于IHostedService在ASP.NET Core而非Simple Injector中注册,因此在运行定时后台服务时收到以下错误:
未处理的异常:System.InvalidOperationException:尝试激活"Optimization.API.BackgroundServices.TimedService"时,无法解析类型"Optimization.Core.Interfaces.IJobRepository"的服务.
所以我的问题是,在Simple Injector中实现后台任务的最佳方法是什么?这是否需要一个独立的集成包而不是标准的MVC集成?我怎样才能将我的Simple Injector注册注入IHostedService?如果我们可以在Simple Injector中注册后自动启动服务,我认为这样可以解决这个问题.
感谢您在此提出任何建议以及有关此主题的任何建议!我可能做错了什么.在过去的一年里,我非常喜欢使用Simple Injector.
我正在观看有关依赖注入的课程视频,并且讲师讨论了di-container但没有详细解释,现在我读了一些文章,并且想确认一下我现在做对了。下面是简单的程序,我的问题是,
下面的Program类是最简单的di容器吗?如果不是这样的话,简单的di-container会怎么样
interface Implementable {
void doSmth();
}
class A implements Implementable {
@Override
public void doSmth() {
}
}
class B {
private Implementable i;
public B(Implementable implementable) {
this.i= implementable;
}
public void doSmth(){
i.doSmth();
}
}
Run Code Online (Sandbox Code Playgroud)
这是类双层容器吗?
class Program {
B b = new B(new A());
b.doSmth();
}
Run Code Online (Sandbox Code Playgroud)