asiftasleem / nbuilder

Automatically exported from code.google.com/p/nbuilder
0 stars 0 forks source link

Feature: Overwrite members which are set in the constructor #64

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

I tried to fill my class with dummy data and called:

Person p = Builder<Person>.CreateNew().Build();

The class looks like this:

public class Person
{
    public Person()
    {
        Name = String.Empty;
        Street = String.Empty;
        HouseNumber = String.Empty;
        City = String.Empty;
    }

    public string Name { get; set; }
    public string Street { get; set; }
    public string HouseNumber { get; set; }
    public int Zip { get; set; }
    public string City { get; set; }
}

I expected that all members will be filled with some values but only 'Zip' was 
set.
Then I realized that all members which are set through the constructor will not 
be touched. This makes sense to ensure integrity of the class. But in my case I 
just want to make sure that the user doesn't access a 'null-string'.
Furthermore it could be helpful to fill a property even if it is set in the 
constructor. Maybe there could be an overloaded Build() method.
E.g.:

Person p = 
Builder<Person>.CreateNew().Build(Creation.IgnoreInitializationInConstructor);

NBuilder 2.2

Bye
Alex

Original issue reported on code.google.com by mystacko...@googlemail.com on 21 Jan 2011 at 9:51

GoogleCodeExporter commented 8 years ago
Hi Alex,

I'm not sure I fully understand why you want to ignore the initialization in 
the constructor, wouldn't that defeat the purpose of the class's constructor?

Wouldn't it be easier to set the individual properties to a non-null value 
explicityly using the .With(...) extention method?

e.g.

Person p = Builder<Person>
.CreateNew()
.With(p => p.Zip = 93837)
.And(p => p.Name = "name")
.Build()

Doing this, you allow the class to be constructed as the author intended as 
well as explicitly set up the properties you want.

Or perhaps you could create a new constructor for the class and use NBuilder's 
method for creating a class using a specific constructor...

Does that help at all? If not, can you explain further so I can better 
understand the problem you are experiencing?

Original comment by joshuajr...@gmail.com on 17 Feb 2011 at 1:19

GoogleCodeExporter commented 8 years ago
I think I have the same issue. The thing is, we will most likely use nbuilder 
for testing. But our classes should be constructed with empty fields. When we 
set values to them inside the constructor, Nbuilder ignores them and only 
changes the null ones (upon construction).

I know how to use the With but that might not be that helpful to big classes 
such as ours. If i'm going to use with, I might as well set these test data 
manually (besides for lists that is).

Hope you can help us and thanks in advance.

Original comment by Quatrei...@gmail.com on 17 Nov 2011 at 8:34

GoogleCodeExporter commented 8 years ago
Hi again,
I've already found the answer to my problem in Issue 77. Thanks again!

Original comment by Quatrei...@gmail.com on 17 Nov 2011 at 8:49

GoogleCodeExporter commented 8 years ago
No response from person who originally raised issue. Since there is a 
workaround for this issue which respects a class' design I'm closing this issue 
as being invalid.

Original comment by joshuajr...@gmail.com on 2 Dec 2011 at 12:57