简体   繁体   中英

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 here enter image description here . Here are some implementation details: enter image description here enter 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 controller enter 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM