标签: fluent-interface

为什么要使用Fluent界面?

与经典属性相比,使用它有什么好处?

我知道实例名称的重复已经消失,但这就是全部?

public class PropClass
{
  public Object1 object1 { get; set; }
  public Object2 object2 { get; set; }
}

PropClass propClass = new PropClass();
propClass.object1 = o1;
propClass.object2 = o2;

public class FluentClass
{
    public Object1 object1 { get; private set; }
    public Object2 object2 { get; private set; }

    public FluentClass SetObject1(Object1 o1)
    {
        object1 = o1;
        return this;
    }

    public FluentClass SetObject2(Object1 o2)
    {
        object1 = o2;
        return this;
    }
}

FluentClass fluentClass = new FluentClass().SetObject1(o1).SetObject1(o2);
Run Code Online (Sandbox Code Playgroud)

c# fluent-interface

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

将属性映射到Entity Framework CTP5中的(不同名称)外键字段

我正在尝试使用Entity Framework CTP5 Fluent API来映射现有数据库.我有以下课程:

public class Shop
{
    public long Id
    {
        get;
        set;
    }
}

public class Sale
{
    public long Id
    {
        get;
        set;
    }

    public virtual Shop Shop
    {
        get;
        set;
    }
}
Run Code Online (Sandbox Code Playgroud)

相应的表称为"商店"和"销售".Sales有一个StoreId外键,指向Stores表中的Id字段.

我正在努力将Sale.Shop.Id映射到表中的StoreId.我无权将其更改为ShopId,因此需要对其进行映射.

在CTP4中,我使用:

modelBuilder.Entity<Sale>().MapSingleType(x =>
    new
    {
        Id = x.Id,
        StoreId = x.Shop.Id
    });
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法:

modelBuilder.Entity<Sale>().Property(x => x.Shop.Id).HasColumnName("StoreId");
Run Code Online (Sandbox Code Playgroud)

但是,它似乎只适用于原始类型.

如何指定此映射?

entity-framework fluent-interface foreign-keys map entity-framework-ctp5

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

EF4 CodeFirst CTP5 nvarchar(max)via属性

有没有办法创建一个自定义属性,使EF CodeFirst 在分配给poco类的属性时使用nvarchar(max)作为数据类型?我知道这可以通过流畅的api实现,但我们希望将所有定义放在一个地方,这就是元数据类.

流畅的API:

modelBuilder.Entity<Event>().Property(p => p.TicketText).HasColumnType("nvarchar(max)");
Run Code Online (Sandbox Code Playgroud)

fluent-interface code-first entity-framework-4 data-annotations ef4-code-only

3
推荐指数
2
解决办法
1028
查看次数

实体框架4.1中具有不同名称的唯一列和导航属性的Fluent Mappings/Data Annotations?

首先,有没有办法通过使用数据注释或流畅的API告诉EF 4.1列需要是唯一的?

其次,关于导航属性,我有2个类Document和User.在文档类中,我有OwnerId(UserId)的属性和Owner(User)的属性.如何告诉EF 4.1 OwnerId是否真的是UserId而Owner是导航属性回用户?

文件类:

public abstract class Document: BaseEntity
    {
        public bool IsActive { get; set; }
        public string Description { get; set; }
        //The UserId
        public Guid OwnerId { get; set; }
        //The User
        public User Owner { get; set; }

    }
Run Code Online (Sandbox Code Playgroud)

mapping fluent-interface data-annotations entity-framework-4.1

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

返回未指定类型的通用

是否有可能创建一个返回未指定类型的泛型的函数,像这样?

Public Function Create(ByVal type As Type) As DeserializationHelper(Of )
    Dim DynamicHelper = GetType(DeserializationHelper(Of )).MakeGenericType(type)

    Return Activator.CreateInstance(DynamicHelper)
End Function
Run Code Online (Sandbox Code Playgroud)

除了强烈输入返回值之外,一切都在这里工作.我可以将它作为对象,非泛型接口或非泛型基类返回,但我正在尝试制作一个非常简洁流畅的样式API.

我正在尝试创建这样的语法:

Dim Loader As New XLoader(parentContainer, Map)
Dim MyCreature As Creature = Loader.Create(GetType(Creature)).From(xml.<Creature>)
Run Code Online (Sandbox Code Playgroud)

...是的,我知道有内置的xml序列化程序 - 我希望它们能为我工作,但我需要更多的控制权.;)

如果我将这部分移动到另一个项目并用C#编写(然后从VB.NET项目中使用它),C#的Dynamic关键字会帮助我吗?

谢谢.:)

c# vb.net generics fluent-interface dynamic

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

Python中的递归类定义

我正在使用Python中的流畅接口进行实验.

一个流畅的SQL查询生成器的示例在使用中看起来像这样:

sql.select('foo').select('bar').from('sometable').tostring() 
Run Code Online (Sandbox Code Playgroud)

我很快意识到递归定义嵌套类的能力可能会有所帮助.

class sql:
    class select:        
        class select   # <--  HERE
        def __init__(self, dbcolumn, astype=None, asname=None):
            self.dbcolumn = dbcolumn
            self.astype = astype
            self.asname = asname
Run Code Online (Sandbox Code Playgroud)

在标有注释'#< - HERE'的行中:
我希望这个嵌套类引用引用包含类的相同'select'类定义.

这有可能吗?也许使用一些我不知道的关键字?

python recursion fluent-interface class python-2.x

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

EF Code First:主键与外键相同

我有两节课

public class Product
{
    public Guid Id { get; set; }
    public string ProductDetails { get; set; }
}

public class SpecialProductDetails
{
    public Guid Product_Id { get; set; } // PK and FK to Product class
    public string SpecialName { get; set; }
    public string SpecialDescription { get; set; }

    public virtual Product Product { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

SpecialProductDetailsProduct类映射1-1 并且是可选的.它共享相同的PrimaryKey和ForeignKey.

在Fluent API中我正在映射这种关系(内部SpecialProductDetails)

public SpecialProductDetails()
{
    HasKey(p => p.Product_Id);
    HasRequired(p => p.Product).WithMany().HasForeignKey(p => …
Run Code Online (Sandbox Code Playgroud)

entity-framework fluent-interface ef-code-first

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

BaseFoo不能用不同的参数继承:<T,X.Bar <T >>和<T,X.Foo <T >>

这是Java在多级层次结构中继承Fluent方法返回类型的简化版本.

给出以下代码:

public enum X {
    ;
    static interface BaseFoo<T, S extends BaseFoo<T, S>> {
        S foo();
    }

    static interface Foo<T> extends BaseFoo<T, Foo<T>> {
        void foo1();
    }

    static interface BaseBar<T, S extends BaseBar<T, S>> extends BaseFoo<T, S> {
        S bar();
    }

    static interface Bar<T> extends BaseBar<T, Bar<T>>, Foo<T> {
        void bar1();
    }

}
Run Code Online (Sandbox Code Playgroud)

运行javac X.java我收到错误消息:

X.java:15: error: BaseFoo cannot be inherited with different arguments: <T,X.Bar<T>> and <T,X.Foo<T>>
    static interface Bar<T> extends BaseBar<T, Bar<T>>, Foo<T> { …
Run Code Online (Sandbox Code Playgroud)

java generics inheritance fluent-interface fluent

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

如何设计Fluent界面?

我一直试图为我的一个框架设计一个流畅的界面,似乎我无法理解这个难题的一部分.我知道我可以使用接口和类来驱动我想要调用哪些方法.但请考虑以下方案.

让我说我有一个Person类,我希望能够做类似的事情

Person.WithName("Steve").WithAge(18).Save();
Run Code Online (Sandbox Code Playgroud)

同样地,我也希望我的API的调用者做类似的事情

Person.WithName("Steve").Save();

or

Person.WithAge(18).Save();
Run Code Online (Sandbox Code Playgroud)

但我不希望用户单独调用save方法

Person.Save();
Run Code Online (Sandbox Code Playgroud)

现在,如果我想设计这样的API,我该如何实现它?如果我从WithName和WithAge方法返回Person类的实例,那么我必须将Save方法放在Person类中,这意味着用户可以直接调用它.

c# fluent-interface fluent

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

与 Python 的流畅界面

我有一个 Python 函数“send_message”,它接受三个参数:

send_message("i like windmills", to="INBOX", from="OUTBOX")
Run Code Online (Sandbox Code Playgroud)

我正在考虑在它上面放置一个流畅的界面。理想情况下,我想编写以下任何内容:

send_message("i like windmills").to("INBOX").from("OUTBOX")

send_message("i like windmills").from("OUTBOX").to("INBOX")

# The `to()` information is mandatory but the `from()` is not (as with real letters), so this one would also be a valid call:
send_message("i like windmills").to("INBOX")
Run Code Online (Sandbox Code Playgroud)

任何想法如何实现这一点或类似的东西?

我理解让对象的方法返回“self”的一般方法,但在我的理解中,这将导致如下结果:

message = Message("i like windmills")
message.to("INBOX").from("OUTBOX").send()
Run Code Online (Sandbox Code Playgroud)

但是这个不如前面的例子好,那么我实际上更喜欢带有命名参数的原始版本。

任何帮助表示赞赏。

python dsl fluent-interface

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