Property Injection of Autofac failed when I called the generic property injected in base class from the subclass

I do property injection all in StartUp file in Asp.Net project by using the Autofac function called RegisterAssemblyTypes. Here is the image shows the details :enter image description here

My program logic:
I set some base classes include BaseServiceImpl ( which process the infomation received from controller ) and BaseManagerImpl ( which mainly used to retrieve data from database by using Ef Core ). The generic type T represents the Model I have previsously set. By using these classes, I can first get the request from controller and process them in service whilc retrieving data from database.

By setting these base classes, I’m able to put some common functions togethere without rewrite them again and again. Here are the common functions in BaseManagerImpl and BaseService: enter image description hereenter image description here. Here are some implementation details: enter image description hereenter image description here. As you can see the implementation is farily simple, the manager retrieved the data and get called by service. But when I inherit the BaseServiceImpl to generate the UserServiceImpl class. Problem comes. Here is how I write the UserServiceImpl class:enter image description here. As you can see, the class are farily simple, most of the functions are implementation of interface IUserService. And the rest override or reuse the functions exist in BaseServiceImple.

Problems Introduction:

I called the UpdateUser function from controllerenter image description here, and then the process flow comes to the UserServiceImpl class, but as you saw above in the picture, UserServiceImpl does not have the corresponding function, so the flow arrives at BaseServiceImpl. Here the Generice Type T becomes User ( which is one of my basic Model to retrieve data from EF Core ).

The problem goes here: enter image description here. The _baseManager property I have comments out is where the problems happened. Autofac didn’t inject correctly, so the value of the proeprty remains Null, which make the following execution of function UpdateEntity throws the NullException: (Code in BaseServiceImpl.UpdateEntity()) return _baseManager.AddNewEntity(entity);.
Note that the _basemanager is generic typed.
Any help is appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I tried again with the problem property’s modifier changed from private to protected and then everything is fine.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x