Reputation: 339
I am converting my Xamarin Forms Application to .NET MAUI.
In Xamarin I used Unity Container for dependency injection, but since dependency injection was built in MAUI, I would like to use it. My question is how can I add [Dependency] Attribute in abstract class
I have a interface:
public interface IServiceBase
{
IAppSettings AppSettings { get; set; }
IRestClient RestClient { get; set; }
ICacheService<AppState> AppCache { get; set; }
}
In the below Abstract class how can I include [Dependency] attribute in Maui App
public abstract class ServiceBase : IServiceBase
{
//[Dependency]
public IAppSettings AppSettings { get; set; }
//[Dependency]
public IRestClient RestClient { get; set; }
//[Dependency]
public ICacheService<AppState> AppCache { get; set; }
}
This abstract class is extended by other Service Classes like below
public class ProfileService : ServiceBase, IProfileService
{
}
I have registered the Service Class in MauiProgram.cs
like below
builder.Services.AddSingleton<IProfileService, ProfileService>();
When trying to register ServiceBase
class getting below exception
builder.Services.AddSingleton<IServiceBase, ServiceBase>();
Cannot instantiate implementation type '....ServiceBase' for service type '....IServiceBase'.
Also how can I add the [Dependency] attribute in ServiceBase
abstract class
Upvotes: 0
Views: 826
Reputation: 2943
As others mentioned in the comment "it can not be instantiated"
Here's an example of how to define your Abstract base and define your dependencies.
First of all, for your base abstract class, define the ctor:
public abstract class ServiceBase : IServiceBase
{
public ServiceBase(IAppSettings _appSettings, IRestClient _restClient, ..., ...)
{
AppSettings = _appSettings;
RestClient = _restClient;
//....
}
public IAppSettings AppSettings { get; private set; }
public IRestClient RestClient { get; private set; }
public ICacheService<AppState> AppCache { get; private set; }
//...
}
This way, you defined what your base needs when your consumer of this class needs to implement it. Now inherit and provide those dependency from your consumer class like:
public class ProfileService : ServiceBase, IProfileService
{
public ProfileService(IAppSettings _appSettings, IRestClient _restClient, WhatEverElseClass _yourProfileServiceNeeds)
: base(_appSettings, _restClient)
{
//...
}
}
When you register dependency in your IOC Container, you just need to register IProfileService
, IAppSettings
, and IRestClient
. You don't need to register your Abstract base! Remember you created this as an Abstract so you should not be able to create an instance of it! That's what Abstract Class is about, to define what your contract will be for ALL your consumer classes!
PS: This is just to explain how to properly use your base class. It may or may not resolve your issue. It's hard to explain these in comments.
Tip Looking at the code you shared, I can see your interface exposing the and Setter. Even your Abstract class exposes those setters. Since you are trying to get those dependencies and assigned them. Those "can" be defined as Private setters! it's not necessary! but it's how we should be "using".
Upvotes: 1