我可以AbstractValidators使用a 注册FluentValidation FluentValidatorFactory.但是,它感觉不对,因为并非所有IoC容器注册都在bootstrap/composition root期间发生.相反,流利的验证器由一个单独的工厂注册:
所述组合物根:
public class SimpleDependencyInjector : IServiceProvider
{
public readonly Container Container;
public SimpleDependencyInjector()
{
Container = Bootstrap();
}
internal Container Bootstrap()
{
var container = new Container();
container.Register< // ...register all non-fluent-validator types, then
container.Verify();
return container;
}
public object GetService(Type serviceType)
{
return ((IServiceProvider)Container).GetService(serviceType);
}
}
Run Code Online (Sandbox Code Playgroud)
一个抽象的流利验证工厂,仅依赖于IServiceProvider
public abstract class FluentValidatorFactory : ValidatorFactoryBase
{
private IServiceProvider Injector { get; set; }
protected FluentValidatorFactory(IServiceProvider injector) …Run Code Online (Sandbox Code Playgroud) dependency-injection fluentvalidation fluentvalidation-2.0 open-generics simple-injector
我正在使用这个ninject绑定:
kernel.Bind<ICurrentUser>().To<CurrentUser>()
.InRequestScope()
.WithConstructorArgument("principal",
context => (RolePrincipal HttpContext.Current.User);
Run Code Online (Sandbox Code Playgroud)
在我的服务层的一个装饰器中,我只需将"ICurrentUser currentUser"添加到构造函数中以获取对象及其工作.
这种实现是否存在任何缺点,或者是否有更好的方法来实现其环境上下文?对于每个请求,都会创建用户对象 - 如果是匿名用户也是如此.
我使用的是MonoDevelop版本2.8.2以及.NET 4.0框架中提供的默认参数.我的第一个问题是,每次重新加载MD时,.NET运行时都将重置为3.0,我将不得不再次更改它.这不是太烦人,但我也想知道为什么会一直发生以及我如何永久地设置它.
我现在的真正问题是,不再出现" 选项"下拉列表中的.NET运行时(或其他具体的,我记不住的).相反,我看到一个禁用的项目选项.
(我会把我在这里看到的图像,但显然我需要声誉.)
即使其中一个问题得到解决,我也应该没问题.但是现在我不能依赖来自IDE的错误消息,而必须切换回Unity.
我是编码和Unity的新手
我的健康栏出现在我的屏幕上,但我不确定如何将健康栏的健康脚本链接到我的播放器脚本和播放器的健康脚本.简单地说,我想这样做,当我的球员被击中时,我的健康棒会失去一颗心
我的健康栏脚本
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public int startHealth;
public int healthPerHeart;
private int maxHealth;
private int currentHealth;
public Texture[] heartImages;
public GUITexture heartGUI;
private ArrayList hearts = new ArrayList();
// Spacing:
public float maxHeartsOnRow;
private float spacingX;
private float spacingY;
void Start () {
spacingX = heartGUI.pixelInset.width;
spacingY = -heartGUI.pixelInset.height;
AddHearts(startHealth/healthPerHeart);
}
public void AddHearts(int n) {
for (int i = 0; i <n; i ++) {
Transform newHeart = ((GameObject)Instantiate(heartGUI.gameObject,this.transform.position,Quaternion.identity)).transform; // …Run Code Online (Sandbox Code Playgroud) 接口,命令和命令处理程序按照Simpleinjector wiki中的说明进行设置.
public interface ICommand
{
string Name { get; set; }
}
public class Command1 : ICommand
{
public string Name { get; set; }
}
public class Command2 : ICommand
{
public string Name { get; set; }
}
public interface ICommandHandler<TCommand>
{
void Execute(TCommand Command);
}
public class Command1Handler : ICommandHandler<Command1>
{
public void Execute(Command1 Command) {
Console.WriteLine(Command.Name);
}
}
public class Command2Handler : ICommandHandler<Command2>
{
public void Execute(Command2 Command) {
Console.WriteLine(Command.Name + "Hello");
}
}
Run Code Online (Sandbox Code Playgroud)
装饰: …
c# dependency-injection dependency-properties simple-injector
通过研究纯C#,我发现Unity中的脚本(使用C#)有点不直观.
例如,似乎人们(初学者?)在Unity中使用脚本的最常见方式是创建"单一用途"类型脚本并将其附加到实际游戏对象(通过单击并将其拖动到脚本组件).以这种方式,似乎很少用于继承.或许我倒退了.我应该在附加到游戏对象之外创建类/继承,然后只将脚本附加到引用那些外部类的游戏对象吗?我相信你们中的一些人可能只会说"做你想做的事!" 或"做什么有效",但我不想从糟糕的编码方法开始,只是为了最后重构它.
我很困惑的另一件事是Unity中的类实例.无论出于何种原因,似乎我可以使用像Animator这样的内置类而无需实例化它[我几乎从未见过一个=新的Class(); ]然而这些类不是静态的.这是如何运作的?如果我构建自己的游戏对象引用的类,我是否必须创建与它们一起工作的实例,或者这一切都是由引擎在引擎盖下完成的吗?
所有这些脚本中的游戏对象(编辑器中的实际项目)在哪里?是否有为每个游戏对象制作的类?而且,如果是这样,我如何找到它并偷看它发生了什么?
嗨,我正在使用Simple Injector作为我的应用程序DI.我保留了框架DI的默认DI.
但我需要注册DbContextSimpleInjector.当我运行应用程序时
container.Verify()
Run Code Online (Sandbox Code Playgroud)
给出以下错误
ActivationException:UsersDbContext类型的构造函数包含名称为'options'的参数,并键入未注册的DbContextOptions.请确保已注册DbContextOptions,或更改UsersDbContext的构造函数.
我正在函数中使用SimpleInjectore注册DbContext SimpleInjectorConfig.InitializeContainer(app, _container)
// DbContext
container.Register<DbContext, UsersDbContext>();
Run Code Online (Sandbox Code Playgroud)
而我的创业公司就是
public void ConfigureServices(IServiceCollection services)
{
var conString = Configuration.GetConnectionString("DefaultConnection");
// Add framework services.
services.AddDbContext<UsersDbContext>(options => options.UseSqlServer(conString));
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<UsersDbContext>()
.AddDefaultTokenProviders();
IdentityConfigurationService.ConfigureIdentityOptions(services);
services.AddMvc();
// Add application services
services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(_container));
services.AddSingleton<IViewComponentActivator>(new SimpleInjectorViewComponentActivator(_container));
services.UseSimpleInjectorAspNetRequestScoping(_container);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
SimpleInjectorConfig.InitializeContainer(app, _container);
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
_container.Verify();
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
我知道问题在于选项,但我不知道Simple Injector如何需要注册到容器的默认连接字符串.这可以传递给Simple Injector吗?或者我应该使用DbContextFactory传递连接字符串UserDbContext?
我试图理解装饰器模式,从实例中我理解装饰器对象如何通过覆盖装饰对象的方法以及如何通过新方法实现添加其他功能来在运行时扩展现有功能.
我有点困惑的地方是使用多个装饰器以及如何访问扩展功能.通常,在查看装饰器模式的示例时,会显示以下内容:
IBusinessObject businessObject = new MailDecorator(
new SmsDecorator(
new FaxDecorator(
new BusinessObject()
)
)
);
Run Code Online (Sandbox Code Playgroud)
想法是采取一个对象并动态添加邮件,短信和传真功能.现在,如果您想发送邮件,这是MailDecorator类提供的方法,您可以执行以下操作:
If(businessObject is MailDecorator)
{
((MailDecorator) businessObject).SendMail();
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您想要发送短信或传真,就像在装饰器模式的正常实现中那样,您将无法访问装饰器引用的对象,这将无效.
这是装饰器模式的限制还是超出了你可以期望用这种模式实现的范围,还是我完全误解了这里的东西?
不同的模式会更合适吗?
标准定义
"动态地将附加职责附加到对象.装饰器为子类提供了灵活的替代方案,可以扩展功能"
意味着这应该可以通过这种模式实现,但在一个对象上使用多个装饰器时似乎会崩溃.
我正在努力确保我的编码遵循正确的对象处理方式,因此我将这些规则强制执行为错误.但我在使用这段代码时遇到了麻烦
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
class MyClass
{
public String ToXml()
{
var objSerializer =
new DataContractSerializer(GetType());
var objStream = new MemoryStream();
StreamReader objReader;
String strResult;
try
{
// Serialize the object
objSerializer.WriteObject(objStream, this);
// Move to start of stream to read out contents
objStream.Seek(0, SeekOrigin.Begin);
objReader = new StreamReader(objStream);
try
{
// Read Contents into a string
strResult = objReader.ReadToEnd();
}
finally
{
objReader.Dispose();
}
}
finally
{
if (objStream != null)
{
// objStream.Dispose(); …Run Code Online (Sandbox Code Playgroud) 调用container.Verify()生产代码是最佳做法吗?我想要搬到:
#IF Debug
container.Verify();
#ENDIF
Run Code Online (Sandbox Code Playgroud)
我没有任何真正的理由做出改变,只是好奇一般共识/最佳实践是什么.