您好我正在使用Simple Injector DI库并且已经关注了一些关于围绕命令模式设计的架构模型的非常有趣的材料:
容器将管理生命周期UnitOfWork,我使用命令来执行数据库的特定功能.
我的问题是,如果我有一个命令,例如一个AddNewCustomerCommand,它反过来执行另一个服务的另一个调用(即发送文本消息),从设计的角度来看这是可接受的还是应该在更高的层次上完成,如果是的话最好这样做?
示例代码如下:
public class AddNewBusinessUnitHandler
: ICommandHandler<AddBusinessUnitCommand>
{
private IUnitOfWork uow;
private ICommandHandler<OtherServiceCommand> otherHandler;
AddNewBusinessUnitHandler(IUnitOfWork uow,
ICommandHandler<OtherServiceCommand> otherHandler)
{
this.uow = uow;
this.otherHandler = otherHandler;
}
public void Handle(AddBusinessUnitCommand command)
{
var businessUnit = new BusinessUnit()
{
Name = command.BusinessUnitName,
Address = command.BusinessUnitAddress
};
var otherCommand = new OtherServiceCommand()
{
welcomePostTo = command.BusinessUnitName
};
uow.BusinessUnitRepository.Add(businessUnit);
this.otherHandler.Handle(otherCommand);
}
}
Run Code Online (Sandbox Code Playgroud) 我有多个服务,每个服务都UnitOfWork使用Simple Injector IoC容器注入构造函数.
目前我可以看到每个UnitOfWork实例都是一个单独的对象,这很糟糕,因为我使用的是Entity Framework,并且需要在所有工作单元中使用相同的上下文引用.
如何确保UnitOfWork每个解析请求将相同的实例注入到所有服务中?UnitOfWor命令完成后,我将由外部命令处理程序装饰器保存.
请注意,这是一个公共库,将用于MVC和Windows Forms,如果可能的话,为两个平台提供通用解决方案会很不错.
代码如下:
// snippet of code that registers types
void RegisterTypes()
{
// register general unit of work class for use by majority of service layers
container.Register<IUnitOfWork, UnitOfWork>();
// provide a factory for singleton classes to create their own units of work
// at will
container.RegisterSingle<IUnitOfWorkFactory, UnitOfWorkFactory>();
// register logger
container.RegisterSingle<ILogger, NLogForUnitOfWork>();
// register all generic command handlers
container.RegisterManyForOpenGeneric(typeof(ICommandHandler<>),
AppDomain.CurrentDomain.GetAssemblies());
container.RegisterDecorator(typeof(ICommandHandler<>),
typeof(TransactionCommandHandlerDecorator<>));
// register …Run Code Online (Sandbox Code Playgroud) .net c# dependency-injection entity-framework-4 simple-injector
想象一下,我有以下内容:
public interface IocInterface1 { }
public interface IocInterface2 { }
public class IocImpl : IocInterface1, IocInterface2 { }
Run Code Online (Sandbox Code Playgroud)
我想如果我尝试通过IoC获取上述类/接口的任何实例,我得到完全相同的实例,而不是每个类型一个单例.例如,b1与b2以下应该是真实的:
_container.RegisterSingle<IocInterface1, IocImpl>();
_container.RegisterSingle<IocInterface2, IocImpl>();
_container.RegisterSingle<IocImpl, IocImpl>();
var test1 = _container.GetInstance<IocInterface1>();
var test2 = _container.GetInstance<IocInterface2>();
var test3 = _container.GetInstance<IocImpl>();
bool b1 = test1 == test2;
bool b2 = test2 == test3;
Run Code Online (Sandbox Code Playgroud)
这可能吗?
c# dependency-injection ioc-container inversion-of-control simple-injector
我有下面的代码,我试图围绕PerlinNoise(x,z),所以我把它等于Yscale并试图绕过它.问题是我得到了该行的错误"当前上下文中不存在名称`Math'".有任何想法吗?
using UnityEngine;
using System.Collections;
public class voxelcreate : MonoBehaviour {
private int origin = 0;
private Vector3 ChunkSize = new Vector3 (32,6,32);
private float Height = 10.0f;
private float NoiseSize = 10.0f;
private float Yscale=0;
private GameObject root;
public float PerlinNoise(float x, float y)
{
float noise = Mathf.PerlinNoise( x / NoiseSize, y / NoiseSize );
return noise * Height;
}
// Use this for initialization
void Start () {
for(int x = 0; x < 33; x++){
bool …Run Code Online (Sandbox Code Playgroud) 到目前为止,我一直在使用Xcode 5.x进行构建.我的应用程序大小(估计应用程序商店大小)约为55MB.刚刚更新到Xcode 6.0.1之后.相同的应用程序现在显示145MB.我在几个地方看到它显示下载应用程序大小.但这只是一种猜测.
这是面向同一问题的人们的链接 http://forum.unity3d.com/threads/estimated-app-size-increased-considerably-on-newest-unity.269245/
这是一款Unity游戏.这只与Unity的导出项目有关,或者这是Xcode的"估计应用商店大小"的新行为?
我试图在统一中实现firebase时有点挣扎,firebase还没有提供统一sdk而我正在考虑这些选项:
1)为iOS和Android创建本机插件,包装firebase sdk和我需要的方法
2)使用IKVM从firebase .jar创建一个dll,我知道有人已经完成了但是我还没有读过100%的工作方法
3)在firebase REST api之上从头开始构建所有东西
你有什么建议?你对这个话题有一些经验,有人在那里使firebase与团结一致吗?:d
谢谢
我使用Simple Injector进行测试,但是在OOP上使用了新的.我正在努力创造松散的夫妻班.这是我的情景.
我有这样的用户回购和界面.
public class UserRepository:IUserRepository
{
public void Add(Model.User user)
{
Console.WriteLine("Name:"+user.Name+"\n"+"SurName:"+user.SureName);
}
public void Delete(int id)
{
throw new NotImplementedException();
}
}
public interface IUserRepository
{
void Add(User user);
void Delete(int id);
}
Run Code Online (Sandbox Code Playgroud)
我的TestInjectedClass类和界面就像我计划在Programe Main中使用的那样.
public class TestInjectedClass:ITestInjectedClass
{
private readonly IUserRepository _userRepository;
public TestInjectedClass(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public void UserRepoRun()
{
var user = new User() {Id = 1,Name = "ada",SureName = "stack"};
_userRepository.Add(user);
}
}
public interface ITestInjectedClass
{
void UserRepoRun(); …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,serviceProvider.GetService<DocumentDbConnection>()解析为null:
public void ConfigureService(IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();
services.AddSingleton<DocumentDbConnection>(
x => new DocumentDbConnection(uri, authKey));
// service is null?
var connection = serviceProvider.GetService<DocumentDbConnection>();
services.AddTransient<IStopRepository, StopRepository>(
x => new StopRepository(connection, databaseId, collectionId));
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?该类型在GetService被调用之前被注册,所以它不能解析为单例吗?
我试图指示我的ASP.NET Core MVC应用程序使用第三方DI容器.而不是写一个适配器,我试图按照这篇文章中的建议插入库
这很好用 - 我可以IControllerActivator用我自己的内置替换使用DI容器的内置.但是,在尝试实例化依赖注入依赖项的自定义中间件时,我遇到了障碍.ASP.NET无法解决这些依赖关系,因为它没有使用我的第三方DI容器 - 是否有相当于IControllerActivator中间件,或者我是否坚持使用内置DI或编写适配器?
**编辑**
这是我的一些代码 - 我实际上是尝试使用上面的模式使用Ninject.
internal sealed class NinjectControllerActivator : IControllerActivator
{
private readonly IKernel _kernel;
public NinjectControllerActivator(IKernel kernel)
{
_kernel = kernel;
}
[DebuggerStepThrough]
public object Create(ActionContext context, Type controllerType)
{
return _kernel.Get(controllerType);
}
}
Run Code Online (Sandbox Code Playgroud)
我发现我有两个问题:
有关第一个问题的示例,这里是一个无法实例化的控制器,因为我正在使用IUrlHelper(还要注意ILogger,它也无法实例化):
public class SystemController : Controller
{
public SystemController(ILogger logger, IUrlHelper urlHelper)
{
/*...*/
}
}
Run Code Online (Sandbox Code Playgroud)
以下是自定义中间件的第二个问题示例:
public class CustomMiddleware
{
private …Run Code Online (Sandbox Code Playgroud) 我正在创建一个可重用的库,它针对几个平台(.NET 4.0,.NET 4.5,.NETStandard 1.0和.NETStandard 1.3).此项目的.NET 4.5版本包含.NET 4.0版本下不可用的一些功能.
引用此库项目的单元测试项目有一个单一的目标平台,即NET 4.5.1.该测试项目显然包含一些代码,用于测试核心库的.NET 4.5特定功能.
但不幸的是,测试项目没有编译,因为Visual Studio似乎引用了.NETStandard 1.0版本,显然不包含此功能.
为了演示我的问题,我将其简化为以下两个项目:
核心库:
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.0": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
},
"net40": {},
"net45": {}
}
}
Run Code Online (Sandbox Code Playgroud)
代码文件:
namespace CoreLibrary
{
#if NETSTANDARD1_0
public class ClassNetStandard { }
#endif
#if NET40
public class ClassNet40 { }
#endif
#if NET45
public class ClassNet45 { }
#endif
}
Run Code Online (Sandbox Code Playgroud)
测试库:
{
"version": "1.0.0-*",
"dependencies": {
"CoreLibrary": { "target": "project" }
},
"frameworks": {
"net451": {}
} …Run Code Online (Sandbox Code Playgroud) c# ×9
.net ×3
asp.net-core ×2
architecture ×1
firebase ×1
ios ×1
ninject ×1
project.json ×1
rounding ×1
unit-of-work ×1
xcode6 ×1