我们将从一个独立的故事开始,只是让你理解为什么:我想要处理任何针对同一个接口更改数据的操作:ICommand存在一些名为ICommandHandlers的东西,可以处理我想要的任何命令.所以,如果我想要一个CreatePersonCommand,我需要一个CreatePersonCommandHandler.
所以这是控制台应用程序的主体,它演示了这一点:(需要简单的注入器)
// The e.g. CreatePersonCommand, with TResult being Person, as an example.
public interface ICommand<TResult>
{
}
//This handles the command, so CreatePersonCommandHandler
public interface ICommandHandler<in TCommand, out TResult>
where TCommand : ICommand<TResult>
{
TResult Handle(TCommand command);
}
// Imagine a generic CRUD set of operations here where we pass
// in an instance of what we need made
public class CreateBaseCommand<TModel> : ICommand<TModel>
{
public TModel ItemToCreate { get; set; }
}
public class DeleteBaseCommand<TModel> : …Run Code Online (Sandbox Code Playgroud) c# generics dependency-injection inversion-of-control simple-injector
我正在尝试切换到Simple Injector Dependency Injection框架,因为我对它的速度印象深刻.
private static void RegisterServices(Container container)
{
container.RegisterPerWebRequest<IDbContext, DbContext1>();
////container.RegisterPerWebRequest<IDbContext, DbContext2>();
container.RegisterPerWebRequest<IUnitOfWork, UnitOfWork>();
container.RegisterPerWebRequest<IColourRepository, ColourRepository>();
Run Code Online (Sandbox Code Playgroud)
其中DbContext1和DbContext2继承自BaseDbContext类
public class BaseDbContext<TContext> : DbContext, IDbContext where TContext : DbContext
Run Code Online (Sandbox Code Playgroud)
它实现了一个相当简单的IDbContext接口(就像在SO上提供的许多接口),例如:
public interface IDbContext
{
IQueryable<TEntity> Find<TEntity>() where TEntity : class;
DbSet<TEntity> Set<TEntity>() where TEntity : class;
int SaveChanges();
void Dispose();
}
Run Code Online (Sandbox Code Playgroud)
如果我只使用一个DbContext类,它可以正常工作 - 存储库被注入,数据被拉出等.
但是,我还想在每个中使用带有少量DbSet的有界上下文(使用DDD有界上下文缩小EF模型),因为我的Code-First DbContext将包含数百个类
private static void RegisterServices(Container container)
{
container.RegisterPerWebRequest<IDbContext, DbContext1>();
container.RegisterPerWebRequest<IDbContext, DbContext2>();
container.RegisterPerWebRequest<IUnitOfWork, UnitOfWork>();
container.RegisterPerWebRequest<IColourRepository, ColourRepository>();
Run Code Online (Sandbox Code Playgroud)
然后我得到一个例外:
System.InvalidOperationException未由用户代码处理HResult = -2146233079消息=类型IDbContext已经注册,并且容器当前未配置为允许覆盖注册.要允许覆盖当前注册,请将Container.Options.AllowOverridingRegistrations设置为true.Source = …
我正在戴尔桌面上安装Ubuntu 13.10.我已经安装了sqldeveloper并在我的Desktop/dir中创建了一个sqldeveoper.desktop文件来启动该程序.见下面的代码.我的问题是:在我的桌面屏幕上,我看到.desktop文件是一个sqldeveloper图标(带有绿色箭头的圆形db图标).我双击图标,程序启动.图标出现在我的Unity栏中,然后图像变为'?' 问号符号.
任何想法为什么会这样?icon.png(图像)位于桌面文件指向的位置.也许文件没有从Unity栏中正确看到路径?
sqldeveloper.desktop代码:
[Desktop Entry]
Type=Application
Version=1.0
Name=SQL Developer
GenericName=Oracle Development Environment
Comment=Proprietary environment for managing Oracle databases
Exec=sqldeveloper %F
Icon=/opt/sqldeveloper/icon.png
Terminal=false
Categories=Development;IDE;
StartupNotify=true
Run Code Online (Sandbox Code Playgroud)
感谢您的任何指导.JohnC
在大多数现代JS框架中,松散耦合UI组件的最佳实践是Pub/Sub的一些实现.
我的问题是这不会使调试,维护你的应用程序更加困难,而依赖注入可以实现相同的结果(松散耦合)?
例如,我的组件应该在单击时打开一个对话框.这需要发生,否则UI将显示为已损坏.对我来说,让组件显式调用某些对话框服务似乎更具可读性.在野外,我看到这个问题解决了更多与pub sub,所以也许我错过了一些东西.
当同时使用这两种方法时,哪些地方可以在事件触发事件或使用注入服务实现该操作时绘制线?
c# dependency-injection ioc-container inversion-of-control light-inject
我有一个ServiceStack API应用程序,它使用Simple Injector作为其IoC容器.我需要某些组件才能拥有"按网络请求"的生活方式.
我在这里查看了Simple Injector文档,发现它没有一个,而是两个与'每个web请求'相对应的生活方式:
这让我很困惑,因为我一直认为所有ASP.NET应用程序都使用相同的基本管道,并且通过插入HTTP模块,每个Web请求实现了IoC容器.为什么Web API应用程序会有所不同?
任何人都可以了解哪个最适合ServiceStack API应用程序?
dependency-injection perwebrequest servicestack simple-injector
这真的是对这个问题的跟进.其中一个Simple Injector开发人员指出了一个关于Simple Injector的有用信息,我认为让它更易于访问是件好事.
那么,使用Web API 1和.NET 4.0支持Simple Injector是否有任何技术障碍?源代码很容易下载和编译.它似乎工作得很好.
我正在尝试模仿Unity中的伺服工作,为我的项目 - 一个物理精确的人形机器人建模,我需要模拟一定程度上转动机器人的部分(腿,膝).我尝试使用铰链接头,但很快就变得明显,它使机器人变得晃动,有点不可预测.现在我正在尝试使用零投影(模式位置和角度)的可配置关节,但它有时会让机器人连续摇动他的部件.
如果我需要考虑每个部分的重量,我还有哪些其他选项可以使机器人或多或少真实(或者像在Microsoft机器人开发工作室中那样)?
我正在尝试将依赖项注入到一个控制器中,该控制器继承自Umbraco的RenderMvcController并获取错误
无法注册RenderMvcController类型,也无法进行隐式注册.为了能够创建RenderMvcController的容器,它应该只有一个公共构造函数:它有3个.有关更多信息,请参阅https://simpleinjector.org/one-constructor.
下面是我连接DI的代码
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
InitializeContainer(container);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
private static void InitializeContainer(Container container)
{
container.Register<ICacheProvider, CacheProvider>(Lifestyle.Transient);
container.Register<ICacheService, CacheService>(Lifestyle.Transient);
}
Run Code Online (Sandbox Code Playgroud)
这是接收依赖的类的一个例子,它继承自我写的基类
public class NewsroomController : BaseRenderMvcController
{
public NewsroomController(ICacheService cacheService) : base(cacheService) { }
Run Code Online (Sandbox Code Playgroud)
基类扩展了RenderMvcController,它是一个Umbraco控制器
public class BaseRenderMvcController : RenderMvcController
{
public ICacheService CacheService { get; set; }
public BaseRenderMvcController(ICacheService cacheService)
{
CacheService = cacheService;
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,基础Umbraco控制器实际上有3个不同的构造函数
public class RenderMvcController : UmbracoController, IRenderMvcController, IRenderController, IController
{
public RenderMvcController();
public …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以通过Microsoft.Extensions.Logging将ThreadId和ProcessId添加到日志中,而无需编写自己的 LoggingProvider?
只需遵循代码即可
Logger.LogInformation("This is a test of the emergency broadcast system.");
Run Code Online (Sandbox Code Playgroud)
应该产生以下或类似的输出。(请假设我正在谈论 ConsoleLogger)
info: [{ProcessId}:{ThreadId}] This is a test of the emergency broadcast system.
Run Code Online (Sandbox Code Playgroud)
- 对于那些熟悉serilog的人,我正在寻找称为输出模板和定义全局属性的功能
- 请不要推荐任何第三方库
c# ×6
.net-core ×1
asp.net-core ×1
events ×1
generics ×1
icons ×1
javascript ×1
light-inject ×1
microsoft-extensions-logging ×1
modeling ×1
physics ×1
servicestack ×1
ubuntu-13.10 ×1
ubuntu-unity ×1
umbraco ×1