标签: fluent-interface

如何保证Fluent API中方法的顺序?

我想为我作为框架的一部分构建的一些类创建流畅的界面。我已经创建了这些方法,并且能够成功链接方法。现在我想确保我可以处理不正确的方法调用顺序。

我正在做的事情类似于 CreateWorkflow -> OpenConfiguration -> ChangeUserName 在上面的场景中,如果首先调用 ChangeUserName 就没有意义,因为它依赖于 OpenConfiguration。

我很困惑我为这种情况创建流畅的方法链是否正确以及如何使该序列起作用。对我来说,这个场景似乎非常适合创建流畅的 API。

c# fluent-interface fluent

3
推荐指数
1
解决办法
2206
查看次数

方法链和流畅接口之间的区别

我想知道方法链接和流畅接口之间的确切区别。据我了解,方法链只是运行先前方法返回对象的方法,同时避免临时变量。这方面的一个例子可能是

Integer.parseInt(str).intValue()
Run Code Online (Sandbox Code Playgroud)

相对于流式接口,对象的每个方法都由一个点链接起来,而不必与前一个方法相关这两种技术使修饰符方法返回到宿主对象,以便可以在单个表达式中调用多个修饰符,如下所示:

new Car().StartsEngine().OpenWindow()
Run Code Online (Sandbox Code Playgroud)

它是否正确?

design-patterns fluent-interface fluent method-chaining

3
推荐指数
1
解决办法
2841
查看次数

使用return语句效果很好!

当我使用返回值创建方法时,我通常会尝试进行设置,以便在以某种方式调用方法时必须返回一些默认值.当我开始时,我会经常编写做某事的方法,并且要么返回他们所做的,要么如果他们没有做任何事情,就会返回null.但我讨厌if(!null)在我的代码中发表丑陋的陈述,

我正在读一本关于ruby的重新指南,我读过许多月前,由务实的程序员,我注意到他们经常返回self(ruby's this)时他们通常不会返回任何东西.他们说,这是为了能够链接方法调用,就像在这个例子中使用返回其设置属性的对象的setter一样.

tree.setColor(green).setDecor(gaudy).setPractical(false)
Run Code Online (Sandbox Code Playgroud)

最初我发现这种东西很有吸引力.有几次我很高兴能够链接方法调用,Player.getHand().getSize()但这有点不同,因为方法调用的对象从一步到另一步变化.

Stack Overflow对返回值的看法是什么?当您想到返回值时,是否有任何模式或习语会浮现在脑海中?有什么好方法可以避免沮丧和增加美感?

methods coding-style fluent-interface return-value

2
推荐指数
1
解决办法
456
查看次数

流畅的实体框架映射

有没有办法像Fluent NHibernate一样为NHibernate执行Fluent EF Mapping?

.net c# entity-framework fluent-interface

2
推荐指数
1
解决办法
3269
查看次数

使用Castle Fluent界面注册拦截器

我试图通过拦截器实现nhibernate事务处理,并无法弄清楚如何通过流畅的机制注册接口.

我看到了

Component.For<ServicesInterceptor>().Interceptors
Run Code Online (Sandbox Code Playgroud)

但不知道如何使用它.有人可以帮我吗?这个例子看起来有点复杂.

c# castle-windsor fluent-interface iinterceptor

2
推荐指数
1
解决办法
2109
查看次数

用于Fluent界面的IDictionary AddAndReturn扩展

什么好看的JQuery这是一个伟大的JavaScript库是为了能够得到你的工作作为返回值的元素.这是我所指的一个例子:

$(function() { 
    $("#poo").css("clear", "both").css("margin", "10px");
});
Run Code Online (Sandbox Code Playgroud)

我想在这里尝试实现的内容与上面的C#代码完全相同IDictionary interface.我的意图是能够正确编码如下:

IDictionary<string, string> myDictionary = 
    new Dictionary<string, string>().
        Add("key1", "value1").
        Add("key2", "value2").
        Add("key3", "value3");
Run Code Online (Sandbox Code Playgroud)

所以我创建了一个扩展方法,如下所示IDictionary:

public static IDictionary<string, string> AddAndReturn(this IDictionary<string, string> dictionary, string key, string value) {

    dictionary.Add(key, value);
    return dictionary;
}
Run Code Online (Sandbox Code Playgroud)

现在我可以像以下一样使用它:

IDictionary<string, string> poo = 
    new Dictionary<string,string>().
        AddAndReturn("key1", "value1").
        AddAndReturn("key2", "value2").
        AddAndReturn("key3", "value3");
Run Code Online (Sandbox Code Playgroud)

所以,至于这里的问题,我想知道的是我是否在这里遵循正确的道路.

我正在做一个穷人流畅的界面实现还是完全无关?

在C#中是否有任何已知的此实现案例?

c# dictionary fluent-interface idictionary

2
推荐指数
1
解决办法
374
查看次数

为什么流利的执行速度比不流利的执行速度慢?

我写了一些测试代码,它将使用8次append()方法 的速度StringBuilder作为一个流畅的接口进行比较,而不是分别在8行中调用它.

流利:

StringBuilder s = new StringBuilder();
s.append(x)
.append(y)
.append(z); //etc
Run Code Online (Sandbox Code Playgroud)

由于不流利:

StringBuilder s = new StringBuilder();
s.append(x)
s.append(y)
s.append(z); //etc
Run Code Online (Sandbox Code Playgroud)

每种方法都被调用了1000万次.在每个块之间调用GC.执行版本的顺序颠倒了相同的结果.

我的测试显示,代码的流畅版本慢了大约10%(fyi,测试代码是公平的,匹配但不可预测的附加,我给了时间JVM热身等).

这是一个惊喜,因为流畅的代码是一行.

为什么不流畅的代码会更快?

java performance fluent-interface

2
推荐指数
1
解决办法
593
查看次数

在实体框架中使用Fluent API创建多对多关系

使用实体框架的API,我不断遇到以下两种方式来映射多对多关系?我从未使用过第二个选项...有什么区别?

选项1:

modelBuilder.Entity<Student>()
    .HasMany( p => p.Lessons)
    .WithMany();
Run Code Online (Sandbox Code Playgroud)

选项2:

modelBuilder.Entity<Student>()
.HasMany(p => p.Lessons)
.WithMany() 
.Map(m =>
{
    m.MapLeftKey("Id");
    m.MapRightKey("Id");
    m.ToTable("StudentAndLessons");
});
Run Code Online (Sandbox Code Playgroud)

这到底是什么MapLeftKeyMapRightKey做什么?您何时使用它并获得什么好处?

c# entity-framework fluent-interface

2
推荐指数
1
解决办法
1267
查看次数

如何编写通用扩展方法转换C#中的类型

我正在编写一个静态防护类/ api来验证发送给方法的参数.

到目前为止的代码如下:

public static class Guard
{
    public static GuardArgument<T> Ensure<T>(T value, string argumentName)
    {
        return new GuardArgument<T>(value, argumentName);
    }

    public static T Value<T>(this GuardArgument<T> guardArgument)
    {
        return guardArgument.Value;
    }

    // Example extension method
    public static GuardArgument<T> IsNotNull<T>(this GuardArgument<T> guardArgument, string errorMessage)
    {
        if (guardArgument.Value == null)
        {
            throw new ArgumentNullException(guardArgument.Name, errorMessage);
        }

        return guardArgument;
    }
}
Run Code Online (Sandbox Code Playgroud)

它可以这样使用:

public void Test(IFoo foo) {

     Guard.Ensure(foo, "foo").IsNotNull();
}
Run Code Online (Sandbox Code Playgroud)

现在的情况要求我需要从提供的界面转换为具体类型.不要问为什么,我只需要!

我想添加一个As扩展方法GuardArgument来执行此操作,例如:

public static GuardArgument<TOut> As<TOut, TIn>(this GuardArgument<TIn> guardArgument, …
Run Code Online (Sandbox Code Playgroud)

c# generics fluent-interface

2
推荐指数
1
解决办法
714
查看次数

使用c ++链接具有父类的子代的方法

有没有办法从子类链接调用超类而不强制转换,重写方法或使用接口.比如做的时候

class A {
public: 
    A& foo() { return *this; }
};

class B : public A {
public:
    B& bar() { return *this; }
};

int main(void) {
    B b;
    b.foo().bar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

用clang编译时我得到了错误

main.cpp:13:10: error: no member named 'bar' in 'A'
        b.foo().bar();
        ~~~~~~~ ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

我可以看到为什么(因为A返回对self的引用),但我希望它返回它的子类B类,因为它在该上下文中被调用.这可能吗?或者我需要将B定义为

class B : public A {
public:
    B& bar() { return *this; }
    B& foo() { A::foo(); return *this; }
};
Run Code Online (Sandbox Code Playgroud)

并使foo()虚拟?

c++ templates fluent-interface crtp chaining

2
推荐指数
1
解决办法
68
查看次数