我正在尝试使用Simple Injector学习依赖注入,所以我创建了一个简单的程序.我查看了大量的例子,但没有找到类似的东西.Ninject有一个类似的例子,他们使用实现自我绑定实现
Bind<Samurai>().ToSelf();
Run Code Online (Sandbox Code Playgroud)
但我没有在简单的注射器中找到任何自我约束的例子.目前该程序运行良好,我得到了我正在寻找的预期结果,但这是正确的方法吗?该程序是最后一个代码示例.
接口
public interface ICar
{
void Move(string direction, int distance);
void Stop();
}
Run Code Online (Sandbox Code Playgroud)
类
public class Driver
{
private readonly ICar _car = null;
public Driver(ICar car)
{
this._car = car;
}
public void Drive(string direction, int distance)
{
_car.Move(direction, distance);
}
public void Brake()
{
_car.Stop();
}
}
Run Code Online (Sandbox Code Playgroud)
实现
public class Ferrari : ICar
{
public void Move(string direction, int distance)
{
Console.WriteLine("I am driving {0} really fast for {1} miles", direction, distance);
}
public …Run Code Online (Sandbox Code Playgroud) c# console-application constructor-injection simple-injector
会是怎样一个快速的方法来拷贝/转换一个array的Color32[]值的byte[]缓冲?
Color32是Unity 3D中包含的结构4 bytes, R, G, B and A respectively。我要完成的工作是将渲染的图像从整体通过管道发送到另一个应用程序(Windows Forms)。目前,我正在使用以下代码:
private static byte[] Color32ArrayToByteArray(Color32[] colors)
{
int length = 4 * colors.Length;
byte[] bytes = new byte[length];
IntPtr ptr = Marshal.AllocHGlobal(length);
Marshal.StructureToPtr(colors, ptr, true);
Marshal.Copy(ptr, bytes, 0, length);
Marshal.FreeHGlobal(ptr);
return bytes;
}
Run Code Online (Sandbox Code Playgroud)
谢谢,抱歉,我是StackOverflow的新手。Marinescu Alexandru
我正在使用ASP.NET MVC 4应用程序.
家庭控制器的构造函数参数化为2参数(Iservice1 service1,Iservice2 service2)并非所有代码路径都使用任何Service(service1,service2),只在某些代码路径中我需要service1 instance/object或service2 instance/object.
我不想使用container.Resolve < <Lazy<IService1>>();
从这个链接(http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx)我明白unity.mvc 4使用unity 3,它具有Lazy loading支持,但是如何在ASP.NET MVC 4中执行此操作.
asp.net-mvc lazy-loading inversion-of-control unity-container deferred-loading
最新版本的SimpleInjector引入了MVC和WebApi之间的区别.两个请求注册是否为同一个东西的别名?或者是否存在潜在的差异?
谢谢
我想通过 SimpleInjector 注入 Container 属性。我没有找到 SimpleInjector 的任何功能。
然后我想将self Container注册到自己,但是Container没有接口。
我想要这个功能,因为我不通过构造函数传输 Container 对象 - 因为如果我可以使用注册对象的自动注入。
我的使用思路:
var container = new Container();
container.Options.AutowirePropertiesWithAttribute<InjectableProperty>();
container.Register<ISomething, Something>(Lifestyle.Singleton);
Run Code Online (Sandbox Code Playgroud)
ISomething:
public interface ISomething
{
void SomeMethod();
}
Run Code Online (Sandbox Code Playgroud)
东西类:
public class Something : ISomething
{
public void SomeMethod()
{
var environment = _container.GetInstance<IEnvironment>();
environment.DoSomething();
}
[InjectableProperty] // - maybe it is not possible (I don't know it)
Container Container {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
你有什么想法来实现这一目标吗?
非常感谢。
我有一种情况,我想使用Identity框架中的自定义UserStore.所以我的控制器看起来像这样.
public AccountController()
: this(new UserManager<ApplicationUser>(new MyUserStore()))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
}
Run Code Online (Sandbox Code Playgroud)
现在Simpleinjector不允许多个构造函数.它可以允许用一个构造函数注册这些类.当情况与上述情况类似时,我该如何解决这个问题.使用Simpleinjector注册.
asp.net-mvc dependency-injection simple-injector asp.net-mvc-4 asp.net-identity
我将.png(800 x 294)尺寸37.2 KB导入统一但导入后显示它的大小为0.9MB,这比原来的大.我在导入设置中将最大尺寸更改为1024到512,将分辨率从原始尺寸降低到512 x 188,尺寸为94KB.如果没有任何图像分辨率变化,.png大小如何增加(从37.2K到0.9MB)?
我希望能够将装饰器与Autofac混合搭配.
例如,假设我有一个由Repository类实现的IRepository接口.
我可以拥有以下装饰器:RepositoryLocalCache,RepositoryDistributedCache,RepositorySecurity,RepositoryLogging ...,你得到了ideea.
基于配置设置,我想用所需的装饰器来装饰基本实现.那可以是无,一个或多个装饰器.
我熟悉注册一个装饰器的语法,或者按固定顺序注册它们的链,但我怎样才能使这个动态化呢?
我正在使用Simple Injector,希望查看是否可以注册/解析继承抽象类的类的集合。
情况如下:我具有以下抽象/具体类:
public abstract class Animal
{
public string Type { get; set; }
public abstract string MakeSound();
}
public class Dog : Animal
{
public override string MakeSound()
{
return "woof";
}
}
public class Cat : Animal
{
public override string MakeSound()
{
return "meow";
}
}
public class Pig : Animal
{
public override string MakeSound()
{
return "oink";
}
}
Run Code Online (Sandbox Code Playgroud)
此外,我有一个应接收an的类,IEnumerable<Animal>并MakeSound为每个动物调用该函数,如下所示:
public class Zoo
{
private IEnumerable<Animal> _animals;
public Zoo(IEnumerable<Animal> animals) …Run Code Online (Sandbox Code Playgroud) 我一直在使用Symfony开发我的Web应用程序,但是我一直遇到一个问题。由于我希望能够正确地对我的服务进行单元测试,所以我总是在服务的构造函数中陷入混乱。
假设我需要一个服务,该服务允许我处理XML文件并将其内容保存到数据库中。
<?xml version="1.0" encoding="UTF-8" ?>
<users>
<user>
<id>1234</id>
<username>Example User</username>
<email>user@example.com</email>
<usergroup>
<id>567</id>
<name>Example User Group</name>
</usergroup>
<permissions>
<item>ALLOWED_TO_CREATE</item>
<item>ALLOWED_TO_UPDATE</item>
<item>ALLOWED_TO_DELETE</item>
<item>ALLOWED_TO_view</item>
</permissions>
</user>
</users>
Run Code Online (Sandbox Code Playgroud)
您已经想到了很多需要注入到此服务中的东西:
我正在使用的真实XML文件包含更多数据,这需要我注入更多处理某些逻辑的存储库和其他服务。
将Doctrine服务目录注入到我的服务中,并通过获取存储库 $doctrine->getRepository(User::class)
优点
缺点
从构造函数中删除所有服务和存储库,并创建setter方法,然后在services.yml中进行调用
services:
AppBundle\Service\MyImportService:
calls:
- [setUserRepository, ['@app.user_repository']]
Run Code Online (Sandbox Code Playgroud)
优点
缺点
有什么解决方案可使我的服务中的参数列表在可读性和单元测试能力方面更易于维护?
一长串的论点甚至被认为是不好的做法吗?
c# ×5
asp.net-mvc ×2
.net ×1
autofac ×1
containers ×1
copy ×1
lazy-loading ×1
marshalling ×1
php ×1
sprite ×1
struct ×1
symfony ×1
unit-testing ×1