question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why should UsingFactory be avoided and what improves on it?

See original GitHub issue

My application has low level general-purpose services that depend on SqlConnection and DbConnection and a high level UserAccountService with a CreateConnection method. (UserAccountService manages SecureString credentials.)

It took a long time searching through the documentation to figure out a way to make this work. The low-level services cannot take a dependency on UserAccountService and they also cannot take a dependency on a typed factory. They must be injected with SqlConnection and DbConnection.

I finally found the ideal configuration method for this. This is precise and minimalist and I could not have asked for a better config function:

Component.For<DbConnection, SqlConnection>()
    .UsingFactory((UserAccountService userAccountService) => userAccountService.CreateConnection())
    .LifestyleTransient()

My concern is that the documentation also states that UsingFactory should be avoided and is going away in future versions.

Why is this? First, what’s the downside to UsingFactory that I should be aware of? Second, is there a paradigm that satisfies the criteria I have in an even more precise and minimalist fashion?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
jnm2commented, May 5, 2016

@dalefrancis88 Those options are not feasible like I said because I can’t modify those lower-level services to accept a UserAccountService or an IConnectionFactory because they are in a different codebase. I do actually need to inject a straight-up SqlConnection.

@IanYates that’s good to know. I think that should be specifically documented here and everywhere you can explicitly Resolve without pairing with an explicit Release. That’s an important rule of thumb to understand and know the boundaries of.

So what you’re saying is that it behaves exactly as I would hope- the factory lives as long as the generated component. So far so good. So to deal with my remaining concerns:

I love this. It feels good and is immediately familiar, easy and comprehensible because it is in actuality dependency injection. It purely and almost declaratively defines the relationship between SqlConnection and UserAccountService:

Component.For<DbConnection, SqlConnection>()
    .UsingFactory((UserAccountService userAccountService) => userAccountService.CreateConnection())
    .LifestyleTransient()

This is strongly distasteful because it is in actuality service location. I want to follow the 3 R’s pattern; I want to write Resolve one time in my entire codebase. Not here. On top of that, it’s just more clunky to type and read:

Component.For<DbConnection, SqlConnection>()
    .UsingFactoryMethod(kernel => kernel.Resolve<UserAccountService>().CreateConnection())
    .LifestyleTransient()

So why avoid UsingFactory? Why is UsingFactory not in fact better recommended, since IKernel could be injected with UsingFactory too if you need it for some other reason?

1reaction
ghostcommented, Apr 16, 2018

@mario-d-s Windsor Factories* are on my hit list as soon as I am done with the ASP.NET stuff. They most definitely need a complete re-write as far as I am concerned. The lifestyle violations for factories alone are stacking up on this issue tracker. They are also incredibly hard to solve.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why should I use a factory class instead of direct object ...
Factory classes are often implemented because they allow the project to follow the SOLID principles more closely.
Read more >
Should I avoid downcasting by any means when using ...
We designed the main server with factory pattern. When a new frame is received, we first identify the associated device type of that...
Read more >
Improve Scalability Using Factory Method Design Pattern
Factory pattern removes the instantiation of actual implementation classes from client code. The code interacts solely with the resultant ...
Read more >
Back to Basics Using Factory Utilities - YouTube
After this webinar you should be able to : - Improve your efficiency in using Autodesk's Factory ... Back to Basics Using Factory...
Read more >
Using Factory Functions - Documentation - SAPUI5 SDK
The factory function is a more powerful approach for creating controls from model data. The factory function is called for each entry of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found