Are Factory Methods Better Than Constructors?

  Рет қаралды 4,998

Codewrinkles

Codewrinkles

Күн бұрын

Пікірлер: 44
@allannielsen4752
@allannielsen4752 Жыл бұрын
Agree that factory methods have their use but I propose it is to define behaviour and therefore should reflect the domain behaviour , ie instead of create from shopping cart which you must admit was obvious in the constructor too, instead LoadFromExistingCart may be better. Whilst code intent is ok, most devs can read code and glean intent, but domain language translates business behaviour which is often obfuscated by technical languages.
@Codewrinkles
@Codewrinkles Жыл бұрын
I fully agree.
@yeerno
@yeerno Жыл бұрын
looks like Rust 'match' and Option and Result
@Codewrinkles
@Codewrinkles Жыл бұрын
Could be. I have never tries out Rust, although I see it's popular and therefore also with the appropriate amount of drama with the Rust Foundation.
@starterdev
@starterdev Жыл бұрын
Because of this video, I will always use factory methods I'm a java dev, but this is the only C# channel I always watch
@Codewrinkles
@Codewrinkles Жыл бұрын
Wow. I don't know how you got here, but I totallt appreciate. I think some of the topics I cover on this channel are also applicable to Java as well. Thanks again.
@alpsavasdev
@alpsavasdev Жыл бұрын
Your explanations are so clear and to the point! Thanks!
@Codewrinkles
@Codewrinkles Жыл бұрын
Glad you like them!
@abhayprince
@abhayprince Жыл бұрын
I love factory methods. I use them very often
@Codewrinkles
@Codewrinkles Жыл бұрын
Yep, me too!
@henkhavenga570
@henkhavenga570 Жыл бұрын
Dan, Excellent and well-presented tutorial. Factory methods are also a must for Inversion of Control. OneOf is new to me. I have been throwing custom exceptions in my domain model factory methods. OneOf is cleaner and I believe a better way to manage validation exceptions. Will use it in future development. Great stuff, please continue lecturing, and thanks for all your knowledge-sharing.
@Codewrinkles
@Codewrinkles Жыл бұрын
Many thanks for your thought on this. I have showed OneOff because I think it's something worth taking into consideration. Yet I'm not sure if I want to use it in a customer app or not. I think it would be great when discriminated unions will come to C#, but nobody knows exactly when that will happen.
@branislavpetrovic7486
@branislavpetrovic7486 Жыл бұрын
Great video! I fully agree with your 2 points on using static factory methods. Thanks!
@Codewrinkles
@Codewrinkles Жыл бұрын
Glad you enjoyed it! And thanks for stopping by and commenting.
@krccmsitp2884
@krccmsitp2884 Жыл бұрын
Especially in rich domain models, e.g. when one applies DDD, I prefer the approach you showed here.
@Codewrinkles
@Codewrinkles Жыл бұрын
I also prefer this approach. Btw, I have a video only on the idea of rich domain model. Plus the entire DDD FUndamentals playlist. Let me know what youthink about those videos.
@samerelsahih
@samerelsahih Жыл бұрын
It's a very interesting approach. Always learning new stuff from you, I'll be using that now :)
@Codewrinkles
@Codewrinkles Жыл бұрын
Glad you found it interesting.
@simplepycodes
@simplepycodes Жыл бұрын
Hum!! Nice, very Interesting and useful as always. Thank you.
@bojanpavlovic5038
@bojanpavlovic5038 Жыл бұрын
Great video Dan, thanks! Got loads of ideas for improvement! 😀
@Codewrinkles
@Codewrinkles Жыл бұрын
Great to hear!
@1kevinson
@1kevinson Жыл бұрын
earned a subscriber, good work
@Codewrinkles
@Codewrinkles Жыл бұрын
Thank you! Hope you'll also enjoy the other videos on the channel.
@МихайлоКос-р2р
@МихайлоКос-р2р Жыл бұрын
Thank you for the great information.
@Codewrinkles
@Codewrinkles Жыл бұрын
Glad it was helpful!
@spyroskatsios
@spyroskatsios Жыл бұрын
Also in a factory method you can await for things! Love your videos!
@Codewrinkles
@Codewrinkles Жыл бұрын
True. I forot to mention that. Thank you for your addition. And for the feedback ofc :)
@modernsanskari4398
@modernsanskari4398 Жыл бұрын
Its really a good thought of using factory methods over constructors. But dont you feel the heavy usage of factory methods leads to OCP(Open closed principle) Violation. We need to rethink again on the usage of the factories.
@Codewrinkles
@Codewrinkles Жыл бұрын
Can you please expand on this? How are factory methods violating OCP?
@Codewrinkles
@Codewrinkles Жыл бұрын
Thinking about this, I feel more like factory methods enforce OCP than violate it.
@ChrisByram
@ChrisByram Жыл бұрын
The only problem I have with static factory method is you can't mock them. So if someone breaks one it'll cause other tests to potentially fail. As opposed to a factory through DI that I can inject and then mock methods. Unless I'm missing something :)
@Codewrinkles
@Codewrinkles Жыл бұрын
I'm not sure I want to enter this discussion because it's usually very opinionated :)). But the way I see things, you can definitely unit test the factory methods without the need to mock anything. That proves that your factories are doing the job. If the static method doesn't interact with external dependencies and it doesn't hold state, unit tests are easy. So, why would you want to mock them? If you already know that your factories are working, you can just use them to create your objects within the test. In practice I never felt the need to really mock them. If you static methods depende on external dependencies or hold state and you want to unit test it, you can create wrapper classes around them that you add to the DI when you're testing.
@ChrisByram
@ChrisByram Жыл бұрын
@@Codewrinkles totally understand the desire to avoid potentially opinated sections. Guess my worry is a developer changing the code in a factory that causes incidental failures. But that's probably me being more of a worry wort then anything. Thank you!
@Codewrinkles
@Codewrinkles Жыл бұрын
No, I'm glad you brought this up. And in fact I'm still thinking about this. Maybe I'm just missing a practical use cases. But in my experience till now, factory methods are usually used on domain models. For everything else you rely on DI. Now, for the domain models, you can easily unit test the factories. If somebody changes something in a factory that breaks things, it would be caught by the unit tests.
@allannielsen4752
@allannielsen4752 Жыл бұрын
I would suggest that finding out someone broke a factory method is a good thing, that afterall is why we write tests. Ok it may not be the specific reason for that given test but it is still a test. I don't see the concern. Mocking is generally used to remove external resources like file systems, clocks, databases, etc. Not other code generally speaking.
@ChrisByram
@ChrisByram Жыл бұрын
@@allannielsen4752 Interesting, perhaps I have been over mocking my tests. 🤔
@obinnaokafor6252
@obinnaokafor6252 Жыл бұрын
I love your videos so much and thank you very much. I love static factory, but I do not like OneOf library - as there is another better library that is simple such as Optional
@Codewrinkles
@Codewrinkles Жыл бұрын
Thanks for sharing!
@pavelromashuk237
@pavelromashuk237 Жыл бұрын
Thanks for the video. Does it still relevant for the real world when there is DI with all its nice features in place?
@Codewrinkles
@Codewrinkles Жыл бұрын
It's of course still relevant as you don't have all your classes in DI and most probably you don't have your models added to the DI container.
@vitorcotta3704
@vitorcotta3704 Жыл бұрын
@@Codewrinkles The FluentValidation normally is in the DI container, so is there a problem to depend on the Validator inside the model class?
@Codewrinkles
@Codewrinkles Жыл бұрын
Here I tend to disagree. I guess I have talked about this in different videos, but in my opinion it's wrong to add everything to the DI conainer just because we can. When it comes to validation in real apps we have multipe validations scopes/contextx. Client side validation, API contract validation, application level validation, domain model validation. Of course, we could use the Fluent validation library for all different types of validation. Also in real applications you'd probably validate different classes at each stage. Like ProductContract for API contract validation, a ProductDto for application level validation and a Product for domain model validation. I know this became a little bit long but I would strongly recommend against adding the domain model validators to the DI container. The domain should be self-contained, self-sufficient and it shouldn't depend on other parts of the application (like a DI container).
@vitorcotta3704
@vitorcotta3704 Жыл бұрын
@@Codewrinkles I see, thank u for share your knowledge
@mensiuscho
@mensiuscho Жыл бұрын
But we can use comment, Constructors do not have to express the intent of the code. i mean that can't be a valid reason to use factory method over constructor, beside the point that it would depends on other factors as well.
DRY Is Bad!
13:14
Codewrinkles
Рет қаралды 3,8 М.
How To Learn C# in 2023 - Learning Path, Tips & Tricks, and More
46:37
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 53 МЛН
Haunted House 😰😨 LeoNata family #shorts
00:37
LeoNata Family
Рет қаралды 14 МЛН
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 2,6 МЛН
Human vs Jet Engine
00:19
MrBeast
Рет қаралды 208 МЛН
#43 Constructor in Java
7:05
Telusko
Рет қаралды 184 М.
Nullable Reference Types - The Good, The Bad, The Ugly
26:24
Codewrinkles
Рет қаралды 4,2 М.
When to use Factory and Abstract Factory Programming Patterns
12:13
Understanding The Factory Design Pattern
22:40
Ryan Schachte
Рет қаралды 18 М.
Don't Use AutoMapper in C#! Do THIS Instead!
16:17
Codewrinkles
Рет қаралды 70 М.
THIS Will Keep Your HttpClient CLEAN
13:03
Codewrinkles
Рет қаралды 5 М.
The Ultimate Guide to C# Records
12:55
Zoran Horvat
Рет қаралды 21 М.
Factory Method Pattern - Design Patterns (ep 4)
27:21
Christopher Okhravi
Рет қаралды 552 М.
Object-Oriented Programming is Bad
44:35
Brian Will
Рет қаралды 2,3 МЛН
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 53 МЛН