fluent-interface

Fluent Interfaces - Method Chaining

你。 提交于 2019-11-27 02:58:28
Method chaining is the only way i know to build fluent interfaces. Here's an example in C#: John john = new JohnBuilder() .AddSmartCode("c#") .WithfluentInterface("Please") .ButHow("Dunno"); Assert.IsNotNull(john); [Test] public void Should_Assign_Due_Date_With_7DayTermsVia_Invoice_Builder() { DateTime now = DateTime.Now; IInvoice invoice = new InvoiceBuilder() .IssuedOn(now) .WithInvoiceNumber(40) .WithPaymentTerms(PaymentTerms.SevenDays) .Generate(); Assert.IsTrue(invoice.DateDue == now.AddDays(7)); } So how do others create fluent interfaces. How do you create it? What language/platform

Tips for writing fluent interfaces in C# 3

拟墨画扇 提交于 2019-11-26 23:56:48
问题 I'm after some good tips for fluent interfaces in C#. I'm just learning about it myself but keen to hear what others think outside of the articles I am reading. In particular I'm after: when is fluent too much? are there any fluent patterns? what is in C# that makes fluent interfaces more fluent (e.g. extension methods) is a complex fluent interface still a fluent one? refactoring to arrive at a fluent interface or refactoring an existing fluent interface any good examples out there that you

Conditional Builder Method Chaining Fluent Interface

两盒软妹~` 提交于 2019-11-26 23:47:59
问题 I was wondering what would be the best way to implement a .When condition in a fluent interface using method chaining in a Builder object? For instance how would I implement the .WithSkill() and .When() methods in the following example: var level = 5; var ninja = NinjaBuilder .CreateNinja() .Named("Ninja Boy") .AtLevel(level) .WithShurikens(10) .WithSkill(Skill.HideInShadows) .When(level > 3) .Build() Update - A sample solution can be found here. 回答1: What I'd do is have NinjaBuilder keep the

Self bound generic type with fluent interface and inheritance

℡╲_俬逩灬. 提交于 2019-11-26 23:36:35
问题 I am using a fluent interface with inheritance. I declared the base class Constructor protected so you cant create a Foo<Bar> which would result in a ClassCastException on calling add(). But i am having trouble with the static method that returns a new Foo instance. public class Foo<T extends Foo<T>> // if i change to extends Foo i only get warnings { public static Foo<Foo> createFoo() // <-- error { return new Foo<Foo>(); // <-- error } protected Foo() {} public T add() { //... return (T

Understanding of How to Create a Fluent Interface

人走茶凉 提交于 2019-11-26 21:35:35
问题 Hi i'm trying to understand how i could build a readable and also error preventing Fluent-API without to much restriction for the User to hold it simple let's say we want to change the following class to be fluent public class Car { public int Gallons { get; private set; } public int Tons { get; private set; } public int Bhp { get; private set; } public string Make { get; private set; } public string Model { get; private set; } public Car(string make, string model) { Make = make; Model =

Difference between .WithMany() and .WithOptional()?

核能气质少年 提交于 2019-11-26 19:36:16
问题 Below are two similar fluent API configurations: WithMany() modelBuilder.Entity<Country>() .HasRequired(cou => cou.Currency) .WithMany() .WillCascadeOnDelete(false); WithOptional() modelBuilder.Entity<Country>() .HasRequired(cou => cou.Currency) .WithOptional() .WillCascadeOnDelete(false); What I am trying to express here is: Every Country requires a concrete Currency , but a Currency can be assigned to zero, one or many Countries. Which of the above statements would I have to use? Or in

MEF Plugins and EF CodeFirst - How?

一世执手 提交于 2019-11-26 19:06:43
问题 Background: We have a project with many modules. We're using EntityFramework 4.2 with FluentAPI (CodeFirst). There is a central project named Diverto.ORM.EntityFramework.SQLServer which contains partial classes that build the context using the FluentAPI (and which has references to every other project on the solution). Recently we received a request from the customer to implement many other features and the solution will need several other projects. Some of these projects will come from

How do I map a char property using the Entity Framework 4.1 “code only” fluent API?

回眸只為那壹抹淺笑 提交于 2019-11-26 18:23:22
问题 I have an object that has a char property: public class Product { public char Code { get; set; } } Entity Framework doesn't seem to be able to map chars (this field is missing from the database when I create the database schema from my model objects). Is there anyway I can map the char (e.g. to a string) using the fluent API? I don't want to change the model objects as they are part of a legacy shared library. 回答1: Char is not valid primitive type for entity framework = entity framework doesn

EF6.0 “The relationship could not be changed because one or more of the foreign-key properties is non-nullable”

佐手、 提交于 2019-11-26 17:38:01
问题 If I try to delete a "child" row I always get an exception. Here is a snipset: using (var context = new CompanyContext()) { ItemType itemType = context.ItemTypes.FirstOrDefault(i => i.Name == "ServerType"); ItemTypeItem itemTypeItem = itemType.Items.FirstOrDefault(i => i.Name == "DatabaseServer"); itemType.Items.Remove(itemTypeItem); context.SaveChanges(); <=== exception! } The following exception is thrown on the SaveChanges() method. "The relationship could not be changed because one or

How to create a fluent query interface?

依然范特西╮ 提交于 2019-11-26 16:18:02
问题 I know how to chain class methods (with the "return $this" and all), but what i am trying to do is to chain them in a smart way, have a look at this: $albums = $db->select('albums')->where('x', '>', '20')->limit(2)->order('desc'); What i could understand from this code sample is that the first 3 methods (select, where, limit) build the query statement that will be executed, and the last one (order) comes to finish the statement and then executes it and throws back the result, right ? But,