nbuilder icon indicating copy to clipboard operation
nbuilder copied to clipboard

Overwriting the default value of a type

Open ArnaudB88 opened this issue 7 years ago • 13 comments

Hi,

Is it possible to overwrite the default value given to a type? If not, can this be implemented?

It would be very handy for the nullable Guid type. These properties are now set with the guid value '00..1', which gives a FK exception when saved to a database. I have to fix this by setting null to each Guid? type property.

Example: the ParentId property is the foreign key (nullable Guid) for the 'Parent' property:

var productStructure = Builder<Domain.ProductStructure>.CreateNew()
                .With(ps => ps.Level = level)
                .With(ps => ps.ParentId = null)
                .Build();

ArnaudB88 avatar Jul 13 '18 13:07 ArnaudB88

@ArnaudB88 I realize this is an extremely late response, but perhaps it may be helpful for someone else who comes along.

I just took a stab at implementing this. In doing so, I found that it seems you may be able to accomplish this by using the following code.

BuilderSetup.DisablePropertyNamingFor<Domain.ProductStructure, Guid?>(x => x.ParentId);

By doing so, a default value is not generated for the ParentId property.

Suppose I have the following class

    public class NBuilderDummy
    {
        public string Foo { get; set; }

        public Guid? Bar { get; set; }

        public int? Baz { get; set; }
    }
BuilderSetup.DisablePropertyNamingFor<NBuilderDummy, Guid?>(x => x.Bar);
var nullableGuidSample = Builder<NBuilderDummy>.CreateNew().Build();
Console.WriteLine($"{JsonConvert.SerializeObject(nullableGuidSample, Formatting.Indented)}");

outputs

{
  "Foo": "Foo1",
  "Bar": null,
  "Baz": 1
}

If you omit the first line with DisablePropertyNamingFor then the output is

{
  "Foo": "Foo1",
  "Bar": "00000000-0000-0000-0000-000000000001",
  "Baz": 1
}

Doug-Murphy avatar Jan 05 '20 17:01 Doug-Murphy

@Doug-Murphy I was aware that it was possible with that method, but with a huge database and many nullable Guids it is not exactly user-friendly. Also, changing the database would imply you must change the nbuilder setup. Therefor I would suggest disabling the default value based on a type.

Thank you for looking into this! :)

ArnaudB88 avatar Jan 06 '20 11:01 ArnaudB88

Arnaud,

That's what I was thinking. :) Which is why I implemented what I did yesterday. Feel free to check out the PR I opened up yesterday to see if that would satisfy your use case.

On Mon, Jan 6, 2020, 6:34 AM ArnaudB88 [email protected] wrote:

@Doug-Murphy https://github.com/Doug-Murphy I was aware that it was possible with that method, but with a huge database and many nullable Guids it is not exactly user-friendly. Also, changing the database would imply you must change the nbuilder setup. Therefor I would suggest disabling the default value based on a type.

Thank you for looking into this! :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nbuilder/nbuilder/issues/86?email_source=notifications&email_token=AAKRXMYUSDWCRD7DV4PSULDQ4MJNJA5CNFSM4FJ3XU42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIFGOMA#issuecomment-571107120, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKRXM33MHQ2UDVRZGCVDMTQ4MJNJANCNFSM4FJ3XU4Q .

Doug-Murphy avatar Jan 06 '20 11:01 Doug-Murphy

Just went through the PR, looks very good! Thank you for this :D I hope it gets approved soon.

ArnaudB88 avatar Jan 06 '20 11:01 ArnaudB88

I'm glad that it fits your use case! :) My last PR went in after 2 weeks or so I think? And it got updated on NuGet not long after. Stay on the lookout I guess. :)

In the meantime, should this issue be closed?

On Mon, Jan 6, 2020, 6:51 AM ArnaudB88 [email protected] wrote:

Just went through the PR, looks very good! Thank you for this :D I hope it gets approved soon.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nbuilder/nbuilder/issues/86?email_source=notifications&email_token=AAKRXM4AYBBUV4YZMRHWYGDQ4MLMTA5CNFSM4FJ3XU42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIFHP3Y#issuecomment-571111407, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKRXM6OQA3MDZN6ZUEHS5DQ4MLMTANCNFSM4FJ3XU4Q .

Doug-Murphy avatar Jan 06 '20 12:01 Doug-Murphy

IMO, nope ... leave this open. Using the Github closing commit keywords, when the PR is merged, it will auto closed linked issues, now that i've linked the two.

PureKrome avatar Jan 06 '20 12:01 PureKrome

I see that the PR is merged into the develop branch. Is it possible to merge it to the master branch and release a new nuget version?

ArnaudB88 avatar Oct 14 '20 07:10 ArnaudB88

@crmckenzie

Doug-Murphy avatar Oct 14 '20 11:10 Doug-Murphy

@Doug-Murphy @crmckenzie Can anyone look into this please.

It very silent in this repository ... Maybe it is an idea to add owners? (just a suggestion)

ArnaudB88 avatar Feb 10 '21 19:02 ArnaudB88

@ArnaudB88 another suggestion (due to the silent repo) is to possibly look at using https://github.com/bchavez/Bogus as a replacement for this repo.

PureKrome avatar Feb 10 '21 22:02 PureKrome

@ArnaudB88 I agree. :) It seems that this repo has died off. As such, I have decided to stop contributing.

As @PureKrome said, I think you may be better served using a different package moving forward.

Doug-Murphy avatar Feb 14 '21 01:02 Doug-Murphy

yeah - maybe this repo should go into archive mode and suggest people to goto Bogus, instead.

PureKrome avatar Feb 14 '21 02:02 PureKrome

Hi - I'm the original author. I haven't used .NET for many years so this project is not something that I can justify spending any time on and I'm out of touch with it now anyway, but I'm happy to appoint someone to look after the repository if anyone wishes.

garethdown44 avatar Feb 14 '21 17:02 garethdown44