Timeline for Making UnitOfWork testable
Current License: CC BY-SA 3.0
17 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 16, 2020 at 10:01 | history | edited | CommunityBot |
Commonmark migration
|
|
Nov 9, 2017 at 8:42 | comment | added | Neil |
Bottom line is that if you're testing UnitOfWork , the behavior of every class it uses must be passed. If you don't want to pass SqlConnection , then pass something that can give you an instance of ICustomerRepository , or better still, pass ICustomerRepository directly (caller will worry about obtaining an instance of ICustomerRepository , not UnitOfWork .
|
|
Nov 9, 2017 at 8:31 | comment | added | FCin |
Well, what I cannot figure out is how to pass the SqlConnection , because I create UnitOfWork myself. I cannot use DI for instances of UnitOfWork , because I create and dispose it only in scope of method(example i my question). If UnitOfWork would require instance of RepositoryFactory , even with DI I would need to have many instances of this class, not only one. I just cannot get a grasp on how to tie all of this together. I understand ways of providing instances of classes with DI , but with this example where UnitOfWork must be created manually I don't see how to do it.
|
|
Nov 9, 2017 at 7:52 | comment | added | Neil |
Dependency injection is entirely configurable from that point of view. You can make RepositoryFactory a singleton and therefore the same instance gets used everywhere. Heck, you can pass the SqlConnection as a dependency to RepositoryFactory if you define how to create an instance (aka establish a connection).
|
|
Nov 9, 2017 at 7:10 | comment | added | FCin |
If I pass SqlConnection from outside of UnitOfWork with e.g. Di , then after first use of UnitOfWork my connection will get disposed, so I would need to create instance of RepositoryFactory for every UnitOfWork .
|
|
Nov 9, 2017 at 1:42 | comment | added | Stop harming Monica | @FCin Your problem is that your objects are hard to test because they are responsible of instantiating their dependencies. The solution is making somebody else responsible of that, a.k.a D.I. If you do not want to do it then your objects will still be hard to test. | |
Nov 8, 2017 at 15:52 | history | edited | Robert Harvey | CC BY-SA 3.0 |
"Edit" isn't a subtitle, and the purpose of an "edit" moniker is already well-served by the edit history.
|
Nov 8, 2017 at 15:28 | history | edited | Neil | CC BY-SA 3.0 |
correction to class
|
Nov 8, 2017 at 14:46 | comment | added | FCin |
@ViktorSeifert I'm sorry, I phrased it badly. It is true that I can use DI for WebForms, but I would like to avoid it for now. Anyway, I think it should be possible to solve my problem without the use of DI . I will introduce it in the later stage of refactoring the application for sure.
|
|
Nov 8, 2017 at 14:41 | comment | added | Viktor Seifert | @FCin Why do you say that you cannot use DI? Seems like Autofac supports WebForms docs.autofac.org/en/latest/integration/webforms.html | |
Nov 8, 2017 at 13:39 | comment | added | FCin |
Thank you for your answer. From what you wrote it looks like IRepositoryFactory has to be passed in UnitOfWork constructor. UnitOfWork is used inside service classes and the instance of RepositoryFactory requires SqlConnection which means that services would have to provide SqlConnection . I would like to keep creation of SqlConnection inside UnitOfWork to keep separation of concerns. Please note I cannot use Dependency Injection , because it is a WebForms project.
|
|
Nov 8, 2017 at 13:26 | comment | added | Neil | @FCin Answer is updated. | |
Nov 8, 2017 at 13:24 | history | edited | Neil | CC BY-SA 3.0 |
Added winded explanation of additional abstraction layer
|
Nov 8, 2017 at 12:16 | comment | added | FCin |
I'm not sure I follow. Where would I instantiate RepositoryFactory ? Inside a service and pass it to every UnitOfWork ? This would require me to expose SqlConnection to services which is now what I wanted to do. Could you write a small example of what you have in mind?
|
|
Nov 8, 2017 at 12:00 | comment | added | Neil |
@FCin So create a IRepositoryFactory interface which returns an instance of ICustomerRepository . It is then implemented by RepositoryFactory normally which takes a SqlConnection parameter to the constructor and passes it along to all instances of ICustomerRepository created. You then replace an instance of RepositoryFactory in your tests with a implementation that returns your mock version of ICustomerRepository .
|
|
Nov 8, 2017 at 11:29 | comment | added | FCin |
Let's say I use e.g. DbContext class that contains all repositories and I pass it to UnitOfWork . How would I pass sqlConnection to any of these classes? I would have to pass it via property which is not the best way of providing functionality.
|
|
Nov 8, 2017 at 11:24 | history | answered | Neil | CC BY-SA 3.0 |