Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Lambda Builder Pattern

Tags:

c#

lambda

builder

I have a class and it has 11 properties (most are inherited). I don't quite like passing in 11 parameters. I know I could create a ModelBuilder class and do the following:

new ModelBuilder().WithProp1(prop1).WithProp2(prop2).Build();

But I was thinking of only one method generic enough to accept a Func which you can then specify the prop to assign:

public Car With<TOut>(Func<Car, TOut> lambda)
{
    lambda.Invoke(this);
    return this;
}

Usage:

var car = new Car()
        .With(x => x.VehicleType = "Sedan")
        .With(x => x.Wheels = 4)
        .With(x => x.Colour = "Pink")
        .With(x => x.Model = "fancyModelName")
        .With(x => x.Year = "2007")
        .With(x => x.Engine = "Large")
        .With(x => x.WeightKg = 2105)
        .With(x => x.InvoiceNumber = "1234564")
        .With(x => x.ServicePlanActive = true)
        .With(x => x.PassedQA = false)
        .With(x => x.Vin = "blabla");

This seems to work. My question: is there anything I'm missing here in terms of implementation (barring the obvious - dragging this method to an interface or helper class)? Are there any gotchas that may surface with this implementation that I am overlooking?

like image 414
ethane Avatar asked Apr 13 '18 11:04

ethane


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

If you like to stick to something like your original approach, I suggest the following, which simplifies it:

public static T With<T>(this T obj, Action<T> action)
{
     action(obj);
     return obj;
}

This extension method lets you initialize the properties of your object in a cleaner way:

var car = new Car().With(c =>
{
    c.VehicleType = "Sedan";
    c.Model = "fancyModelName";
    //and so on
});
like image 123
Sebastian Hofmann Avatar answered Sep 30 '22 01:09

Sebastian Hofmann