我刚刚爱上了NHibernate和流畅的界面.后者支持非常好的映射和重构支持(不再需要xml文件).
但没有人是完美的,所以我错过了流利的多对多映射.有人知道它是否已经存在吗?如果是这样,一行简单的代码就会很好.
但要坚持问题的标题,有没有办法结合流畅和正常的NHibernate映射.
目前我使用以下行进行我的测试设置WITH Fluent,以及第二个代码块用于我的测试WITHOUT流畅(使用XML映射).如何判断流利使用流畅的IF AVAILABLE和XML否则......
var cfg = new Configuration();
cfg.AddProperties(MsSqlConfiguration.MsSql2005.ConnectionString.Is(_testConnectionstring).ToProperties());
cfg.AddMappingsFromAssembly(typeof(CatMap).Assembly);
new SchemaExport(cfg).Create(true, true);
var persistenceModel = new PersistenceModel();
persistenceModel.addMappingsFromAssembly(typeof(CatMap).Assembly);
IDictionary<string, string> properties = MsSqlConfiguration.MsSql2005.UseOuterJoin().ShowSql().ConnectionString.Is(_testConnectionstring).ToProperties();
properties.Add("command_timeout", "340");
session = new SessionSource(properties, persistenceModel).CreateSession();
Run Code Online (Sandbox Code Playgroud)
没有流利......
config = new Configuration();
IDictionary props = new Hashtable();
props["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
props["dialect"] = "NHibernate.Dialect.MsSql2005Dialect";
props["connection.driver_class"] = "NHibernate.Driver.SqlClientDriver";
props["connection.connection_string"] = "Server=localhost;initial catalog=Debug;Integrated Security=SSPI";
props["show_sql"] = "true";
foreach (DictionaryEntry de in props)
{
config.SetProperty(de.Key.ToString(), de.Value.ToString());
}
config.AddAssembly(typeof(CatMap).Assembly);
SchemaExport se = new SchemaExport(config);
se.Create(true, true);
factory = …Run Code Online (Sandbox Code Playgroud) 你能给我举个例子,在 PHP 中,展示闭包如何有助于创建 DSL(流畅的接口)?
编辑:以下问题中接受的答案讲述了嵌套闭包。如果有人可以将该示例转换为 PHP,那也会有帮助: 流畅的界面经验?我需要你的意见!
鉴于代码
[Test]
public void Test1()
{
var a = new A();
a
.Method1()
.Method2();
}
Run Code Online (Sandbox Code Playgroud)
是否可以设置一个断点,以便在执行Method1()之后暂停执行,但是在没有进入Method2的定义并在那里放置断点的情况下在Method2之前暂停?当我这样做时,断点出现在'a'.
debugging fluent-interface breakpoints visual-studio-2010 method-chaining
我可以在实体框架中为我的实体使用代码优先属性和fluent-API配置吗?
谢谢.
我对流利的二传手的概念有一些问题。我创建了 2 个从同一个父级扩展的类。我将它们之间的公共属性放在父类中,我想将 setter 放在那里,以避免在每个子类上重复相同的代码。
例如:
<?php
class Vehicle
{
protected $color;
protected $wheels;
public function setColor($color)
{
$this->color = $color;
return $this;
}
public function setWheels($wheels)
{
$this->wheels = $wheels;
return $this;
}
}
class Motorbike extends Vehicle
{
protected $engine;
public function setEngine($engine)
{
$this->engine = $engine;
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是当我这样做时:
$motorbike = new Motorbike();
$motorbike->setColor('blue')
->setEngine(4.2) // Here the returned '$this' referres to the parent class Vehicle, so the setEngine doesn't exist.
->setWheels(4)
Run Code Online (Sandbox Code Playgroud)
父类是否有可能返回引用子类的 $this ?或者有更好的方法来做到这一点? …
我对JavaScript/jQuery有些新意,但是当我看到方法链接的例子时,它立刻让我感到很熟悉.像LINQ其它接口做同样的事情,其中一组方法的返回类型是一样的,因为他们所操作的类型(TweetSharp确实非常类似的东西).这是流畅编程的一个例子吗?我读到的关于jQuery的大部分内容都说其他库已经"借用"了这种方法链接的想法 - 这个想法是否源于jQuery?
我已经阅读过有关Fluent API的内容,其中代码可以像英语一样阅读,但我似乎无法找到它们的任何示例,因为我想知道它们是否是一种易于使用的界面的合理方法由非全职程序员组成的系统.有没有人有流畅的界面的例子?
Fluent构建器是一种众所周知的模式,用于构建具有许多属性的对象:
Team team = teamBuilder.CreateTeam("Chelsea")
.WithNickName("The blues")
.WithShirtColor(Color.Blue)
.FromTown("London")
.PlayingAt("Stamford Bridge");
Run Code Online (Sandbox Code Playgroud)
但是,由于一个特殊原因,使用它对我来说似乎不太清楚:
Team对象都有其最小的操作状态,换句话说,必须设置的属性集(必需),以便对象可以使用.现在,Fluent builder考虑到你必须保持这种状态,应该如何使用这种方法?
With_XYZ成员是否应该修改对象的一部分,这不会影响这种状态?
也许这种情况有一些一般规则?
如果该CreateTeam方法应该将强制属性作为参数,接下来会发生什么?
如果我(例如)省略WithNickName呼叫会怎样?
这是否意味着昵称应该默认为某些DefaultNickname?
这是否意味着该示例(请参阅链接)不好,因为该对象可能处于无效状态?
而且,我怀疑在这种情况下,流畅的建筑方法实际上失去了它的"美丽",不是吗?
我正在使用流畅的界面模式.
首先,我写了类似的东西:
class C
{
public:
C() { }
C* inParam1(int arg1){ param1 = arg1; return this; }
C* inParam2(int arg2){ param2 = arg2; return this; }
private:
int param1;
int param2;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用std :: unique_ptr,但后来我意识到我不知道如何沿着链"移动"指针(this).我尝试过类似的东西:
return std::move(this);
Run Code Online (Sandbox Code Playgroud)
那当然不行.
我怎样才能做到这一点?做这样的事有什么问题吗?
为了回复如下评论:"不要使用指针":没有(还)任何实际原因,因为我用指针做这个,只是我想知道是否可以这样做.
这是我目前正在做的事情:
modelBuilder.Entity().Property(e => e.Name).IsRequired();
modelBuilder.Entity().Property(e => e.UPC).IsRequired();
modelBuilder.Entity().Property(e => e.Price).IsRequired();
modelBuilder.Entity().Property(e => e.Description).IsRequired();
以下是我想做的事情:
modelBuilder.Entity().属性(e => e.Name).IsRequired().属性(e => e.UPC).IsRequired().属性(e => e.Price).IsRequired().属性( e => e.Description).IsRequired()
但后者不起作用.有没有其他方法不必每次重复modelBuilder.Entity()?
这是目前最精辟的选择:
var e = modelBuilder.Entity(); e.Property(e => e.Name).IsRequired();
e.Property(e => e.UPC).IsRequired();
e.Property(e => e.Price).IsRequired();
e.Property(e => e.Description).IsRequired();
fluent-interface ×10
c# ×2
fluent ×2
php ×2
breakpoints ×1
builder ×1
c++ ×1
c++11 ×1
closures ×1
code-first ×1
constructor ×1
debugging ×1
dsl ×1
jquery ×1
nhibernate ×1
unique-ptr ×1