我想将我的灯具迁移到Rails中的"Factory Girl".
有没有简单的方法来转换factories.rb文件中的所有yml文件?
我有一个工厂类,它决定它应该实例化和返回的四个可用子类中的哪一个.正如您所料,所有子类都实现相同的接口:
public static class FooFactory{
public IFoo CreateFoo(FooEnum enum){
switch (enum)
{
case Foo1:
return new Foo1();
case Foo2:
return new Foo2();
case Foo3:
return new Foo3(IBar);//has a constructor dependency on IBar
case Foo4:
return new Foo4();
default:
throw new Exception("invalid foo!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,其中一个子类在其构造函数中定义了依赖项.
一些兴趣点:
IFoo都是域对象,因此不会被Spring.NET实例化.如果可能的话,我想保持这种方式.我试图找出如何最好地将IBar依赖关系传递Foo3给FooFactory.我觉得这可能是通过IoC最好解决的问题,但我不能理解如何.我还希望尽可能保持FooFactory单元可测试:即我不希望在测试代码中依赖Spring.NET.
谢谢阅读.
嗨,我正在尝试按照DDD进行应用.我有以下实体:
public class Item
{
public Category Category { get; protected set; }
...
}
public class SpecificItem : Item
{
...
}
public class Category
{
public static int IdOfCategoryForSpecificItem = 10;
public int Id { get; set; }
}
现在我想用创建ObjectItem类型对象的方法创建工厂.但是这个特定项目必须属于特定类别.所以我创建了这样的工厂:
public class ItemFactory
{
public static SpecificItem CreateSpecificItem(object someArguments)
{
IRepository<Category> repository = null // How to get repository?
return new SpecificItem
{
Category = repository.FirstOrDefault(i => i.Id == Category.IdOfCategoryForSpecificItem),
// additional initialization
}; … 有没有一种简单的方法可以使用Ninject将所有Factory接口绑定到ToFactory()扩展方法?
public class Foo
{
readonly IBarFactory barFactory;
public Foo(IBarFactory barFactory)
{
this.barFactory = barFactory;
}
public void Do()
{
var bar = this.barFactory.CreateBar();
...
}
}
public interface IBarFactory
{
Bar CreateBar();
}
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,我可以使用:
kernel.Bind<IBarFactory>().ToFactory();
Run Code Online (Sandbox Code Playgroud)
如果我有10或20个需要绑定的IFactory接口,我该怎么办?
我正在尝试从文本文件中运行'Recipe'读取并逐行解析以动态调用一系列方法.我想在做了一些谷歌搜索后我需要实现一个工厂,但我缺少一些关键细节.这是我最接近的例子:
http://simpleprogrammer.com/2010/08/17/pulling-out-the-switch-its-time-for-a-whooping/
以下代码是现在的代码片段.
internal static void Run(int Thread_ID, List<StringBuilder> InstructionSet, List<double>[] Waveforms)
{
//Init
List<double>[] Register = new List<double>[10];
for (int i = 0; i < Waveforms.Length; i++) { Register[i] = new List<double>(Waveforms[i]); }
for (int i = 0; i < Register.Length; i++) { if (Register[i] == null) { Register[i] = new List<double>(); } }
//Run Recipe Steps
foreach (var item in InstructionSet)
{
Step Op = Step.Parse(item.ToString());
switch (Op.TaskName)
{
case "SimpleMovingAverage":
Register[Convert.ToInt32(Op.Args[0])] = Signal_Filters.SimpleMovingAverage(Register[Convert.ToInt32(Op.Args[1])], Convert.ToInt32(Op.Args[2]));
break;
case "RollingSteppedStdDeviation": …Run Code Online (Sandbox Code Playgroud) 我的工厂在我的控制器中未定义,我无法弄清楚原因.我创建了一个简单的例子来说明.
在这里我创建了应用程序:
var ruleApp = angular
.module( "ruleApp", [
"ngRoute",
"ruleApp.NewFactory1",
"ruleApp.NewFactory2",
] );
Run Code Online (Sandbox Code Playgroud)
在这个虚拟的例子中,我想建立一个简单的工厂,显示一个警告框.我将展示两种方法(一种方法有效,一种方法无效).
工厂1:
angular
.module('ruleApp.NewFactory1', [])
.factory('NewFactory1', function() {
return function(msg) {
alert(msg);
};
});
Run Code Online (Sandbox Code Playgroud)
工厂2:
angular
.module('ruleApp.NewFactory2', [])
.factory('NewFactory2', function() {
var showMessageFunction = function(msg) {
alert(msg);
};
return
{
showMessage: showMessageFunction
};
});
Run Code Online (Sandbox Code Playgroud)
请注意,工厂1的返回类型是一个函数,工厂2的返回类型是一个具有属性的对象(函数类型).
现在看看我如何在我的控制器中使用这两个工厂:
ruleApp.controller( "RuleController", function( $scope, NewFactory1, NewFactory2 ) {
NewFactory1("1");
NewFactory2.showMessage("2");
});
Run Code Online (Sandbox Code Playgroud)
这就是问题暴露的地方.在执行期间,我被提示使用警告框NewFactory1("1");,但它在执行期间失败,NewFactory2.showMessage("2");因为NewFactory2未定义(TypeError:无法调用未定义的方法'showMessage').
你能帮我发现一下这个问题吗?我希望能够使用像NewFactory2这样的工厂,因为我希望工厂能够做的不仅仅是一件事(即具有多个单一功能).顺便说一下,我正在使用Angular 1.2.1版.
我正在使用Visual Studio 2010并且有工厂用于创建抽象基类的两个实现之一.工厂Create方法接受bool标志并返回shared_ptr中的两个impl之一.使用if语句对我来说很好,但是当我尝试使用带有make_shared调用的三元组时,编译器会抱怨.
class Base {
public:
Base() {};
};
class Foo : public Base {
public:
Foo() {};
};
class Bar : public Base {
public:
Bar() {};
};
class Factory {
public:
static std::shared_ptr<Base> Create(bool isFoo) {
return isFoo ?
std::make_shared<Foo>() :
std::make_shared<Bar>();
}
};
int main() {
std::shared_ptr<Base> instance = Factory::Create(true);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
VS给出的错误是'没有可以执行此转换的用户定义转换运算符,或者运算符不能被调用c:\ path\file.h(78):错误C2668:'std :: tr1 :: shared_ptr <_Ty> :: shared_ptr':使用[_Ty = Base]对重载函数进行模糊调用
注意
static std::shared_ptr<Base> Create(bool isFoo) {
if (isFoo)
return std::make_shared<Foo>();
return std::make_shared<Bar>(); …Run Code Online (Sandbox Code Playgroud) 你怎么称呼工厂?如下定义.
angular.module('fb.services', []).factory('getQueryString', function () {
return {
call: function () {
var result = {}, queryString = qs.substring(1),
re = /([^&=]+)=([^&]*)/g,
m;
while (m = re.exec(queryString))
result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
return result;
}
}
});
alert(getQueryString.call('this=that&me=you'));
Run Code Online (Sandbox Code Playgroud) 我希望得到如下服务
public SomeService(IMongoDatabase mongoDatabase) {
DB = mongoDatabase;
}
Run Code Online (Sandbox Code Playgroud)
我想使用工厂来解决IMongoDatabase,只是为了封装IConfiguration用法
public static IMongoDatabase GetMongoDatabase(IConfiguration config)
{
var connectionString = config.Get("SomeConnectionString");
// MongoClient handles connection pooling internally
var client = new MongoClient(connectionString);
var db = client.GetDatabase(config.Get("SomeDbName"));
return db;
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何处理注册,以便MongoDbFactory.GetMongoDatabase在任何类需要时调用IMongoDatabase.IConfiguration将被注册.
我真的很想在我的服务中使用一个IMongoDatabase而不是一个Func<IConfiguration, IMongoDatabase>.后者似乎过于迟钝,要求消费者实施我应该能够为他们实施的步骤.
好吧,假设我有一个带有属性的Post模型name,slug和content.我想用我的ModelFactory生成模型,但想设置一个特定的名称,我通过覆盖该值来完成:
factory(App\Post::class)->create(["name" => "Something here"]);
Run Code Online (Sandbox Code Playgroud)
但是现在我希望slug通过使用(新)名称自动生成并且不将其作为参数传递.喜欢
"slug" => str_slug($name);
Run Code Online (Sandbox Code Playgroud)
这是可能的还是我需要手动编写slug?使用下面的工厂时,->create(['name' => 'anything']);不会创建slug.
$factory->define(App\Post::class, function (Faker\Generator $faker) {
static $name;
return [
'name' => $faker->name,
'slug' => $name ?: str_slug($name),
'content' => $faker->sentences(),
];
});
Run Code Online (Sandbox Code Playgroud) factory ×10
c# ×3
angularjs ×2
autofac ×1
c++ ×1
dictionary ×1
factory-bot ×1
fixtures ×1
laravel ×1
laravel-5 ×1
laravel-5.3 ×1
ninject ×1
php ×1
refactoring ×1
repository ×1
spring.net ×1
testing ×1