Generic interface implementation - c#

Let's say we have GeneralRequest class with no properties. Every specific request class extends this class. For example:
public class GeneralRequest
{
}
And:
public class UserInsertRequest : GeneralRequest
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class UserUpdateRequest : GeneralRequest
{
public string FirstName { get; set; }
}
Also there is interface IGeneralService like this:
public interface IGeneralService
{
public Task Insert(GeneralRequest request);
public Task Update(GeneralRequest request);
}
With specific user service interface that implements it:
public interface IUsersInterface : GeneralInterface
{
public Task FindByFirstName(String FirstName);
}
And specific service class that implements it:
public class UserService: IUsersInterface
{
public async Task Insert(UserInsertRequest request)
{
//implementation
}
public async Task Update(UserUpdateRequest request)
{
//implementation
}
public Task FindByFirstName(String FirstName)
{
//implementation
}
}
The problem is that UserService's Insert and Update is not IGeneralService's Insert and Update method implementation (different method signature). But, is there a way to make this logic work?
I tried something like this
public interface IGeneralService<T> where T:GeneralRequest
{
public Task Insert(T request);
public Task Update(T request);
}
But in that case I need to specify the type parameter when implementing IGeneralService, and there are multiple. I'm so confused !
My goal is to have general service interface that every service class implements it, and specific interface for specific service class.

Related

Pull up Generic Type to interface

I have an IModule interface defined as below:
public interface IModule
{
//Properties
string StoreName { get; set; }
void Execute();
T ApiClient; // <-- How do I do this?
}
I also have an abstract class defined as:
public abstract class ModuleBase<T> : IModule where T : IAPIClient
{
public T ApiClient { get; private set; }
public abstract Task Execute();
}
I then inherit ModuleBase in a class such as:
public class DemoModule : ModuleBase<DemoApiClient>
{
public override string StoreName => "Demo"
public override async Task Execute(){
ApiClient.DoStuff();
//... some long running stuff here
}
}
DemoApiClient is defined as:
public class DemoApiClient : IAPIClient
{
public IProxyManger ProxyManager {get; set;}
public IHttpClient HttpClient {get; set;}
public void DoStuff() {
Console.Log("DemoApiClient - DoStuff");
}
}
IAPIClient is an interface with the following declarations:
public interface IAPIClient
{
IProxyManager ProxyManager { get; }
IHTTPClient HttpClient { get; }
}
How can I pull up the generic ApiClient on ModuleBase up to IModule? I want to ensure that every implementation of IModule can implement the implementation of IAPIClient?
The reason I need this is because of the following example:
IModule module = (IModule)Activator.CreateInstance(typeof(DemoModule));
module.ApiClient.DoStuff()
Note that DoStuff() is specific to DemoApiClient and other implementations of IAPIClient will contain methods specific to them. For this reason, I cannot pull up DoStuff() to IAPIClient.
You need to make IModule generic
public interface IModule<T> where T : IApiClient
{
Task Execute();
T ApiClient {get;}
}
Then:
public abstract class ModuleBase<T> : IModule<T> where T : IAPIClient
{
public T ApiClient { get; private set; }
public abstract Task Execute();
}
You'll need to know the type:
DemoModule module = (DemoModule)Activator.CreateInstance(typeof(DemoModule));
module.ApiClient.DoStuff()
But lets take a step back here; why do you think IModule needs to know about the APIClient at all, when you never use it outside of the base class?
You original code should work just fine as you only call ApiClient.DoStuff(); from Execute
Add IAPIClient property to IModule:
public interface IModule<T> where T : IAPIClient
{
//Properties
string StoreName { get; }
T ApiClient { get; }
}
And DoStuff to IAPIClient:
public interface IAPIClient
{
IProxyManager ProxyManager { get; }
IHttpClient HttpClient { get; }
void DoStuff();
}
And you can do this:
IModule module = (IModule)Activator.CreateInstance(typeof(DemoModule));
module.ApiClient.DoStuff();

Passing any model class to a common method C#

So, I want to create an interface which has a method that can take in any model class.
For example
I have these three property class
class A
{
public long id { get; set; }
public string description { get; set; }
public string code { get; set; }
}
class B
{
public long someID { get; set; }
}
class C
{
public long anydesign { get; set; }
}
class D
{
public long Router { get; set; }
}
I have an interface
public interface IModel
{
void Dosomething(A model); // Now in this example it takes the A model,But I want it to be set, so that that class that implements the interface can put any model as required
}
Now, I have a class that implements the mode
Since the interface only takes the A model, I can pass in the A model in the class during implementation
public class ImplemenationA: IModel
{
public void Dosomething(A model)
{
Console.WriteLine(model.description);
}
}
Say i have another implemenation Class
Now, I am guessing the below one wouldnt work, as the interface signature enforces only to take a Model A and not any other model
public class ImplementationB:IModel
{
public void Dosomething(B model)
{
Console.WriteLine(model.someID);
}
}
I want to the interface method to be invoked by any implementation class and use any model
While it is unjustified what you're attempting to do... to answer the actual question it is possible using generics...
Take the following for guidance (and example)...
class Test : IJobTask
{
public void Start(string val = "")
{
throw new NotImplementedException();
}
}
public interface ITest
{
void MyMethod<T>(T model) where T : IJobTask;
}
public class ConcreteTest : ITest
{
public void MyMethod<T>(T model) where T : IJobTask
{
}
}
public class Main
{
public Main()
{
var ct = new ConcreteTest();
ct.MyMethod(new Test());
}
}
Your MyMethod will need to perform checks and casts now which kind of defeats the purpose, but this does answer the question of "a method accepting any model"
Use interface for your property classes.
public interface IProperty
{
...
}
class A: IProperty
{
...
}
class B: IProperty
{
...
}
class C: IProperty
{
...
}
class D: IProperty
{
...
}
And then you can pass interface as a parameter:
public interface IModel
{
int getModel(IProperty model);
}

Interface method that takes any property class/model C#

So, I want to create an interface which has a method that can take in any model class. For example
I have these three property class
class A
{
public long id { get; set; }
public string description { get; set; }
public string code { get; set; }
}
class B
{
public long someID { get; set; }
}
class C
{
public long anydesign { get; set; }
}
class D
{
public long Router { get; set; }
}
I have an interface
public interface IModel
{
void Dosomething(A model); // Now in this example it takes the A model,But I want it to be set, so that that class that implements the interface can put any model as required
}
Now, I have a class that implements the mode Since the interface only takes the A model, I can pass in the A model in the class during implementation
public class ImplemenationA: IModel
{
public void Dosomething(A model)
{
Console.WriteLine(model.description);
}
}
Say i have another implemenation Class Now, I am guessing the below one wouldnt work, as the interface signature enforces only to take a Model A and not any other model
public class ImplementationB:IModel
{
public void Dosomething(B model)
{
Console.WriteLine(model.someID);
}
}
I want to the interface method to be invoked by any implementation class and use any model
Based on your description you may want to use Generics. Since you're creating separate implementations you can apply the interface below to achieve a similar result.
public interface IModel<T>
{
void Dosomething(T model);
}
public class ImplementationB : IModel<B>
{
public void Dosomething(B model)
{
Console.WriteLine(model.someID);
}
}
Some sort of Decorator pattern can solve it, postpone your actual implementation to inner classes and insist on separation of concern, please leave a comment if I miss understood your question:
//added to support inner implementation
interface IModelImpl {
void Do();
}
class A: IModelImpl
{
public long id { get; set; }
public string description { get; set; }
public string code { get; set; }
public void Do(){
console.WriteLine(this.description);
}
}
class B: IModelImpl
{
public long someID { get; set; }
public void Do(){
console.WriteLine(this.someID);
}
}
class C: IModelImpl
{
public long anydesign { get; set; }
public void Do(){
...
}
}
Here is your IModel, pretty much the same, considered like an outer implementation:
public interface IModel
{
void Dosomething(IModelImpl model); //put any model as long it implements IModelImpl
}
Your class implementation should now changed to:
public class ImplemenationA: IModel
{
public void Dosomething(IModelImpl model)
{
//Do more specific work to ImplementationA
model.Do();
}
}
Another class implementation:
public class ImplementationB:IModel
{
public void Dosomething(IModelImpl model)
{
//Do more specific work to ImplementationB like validation
model.Do();
}
}

List of generic interface with generic interface parameter

I know similar questions have been asked, but I didn't find any which was similar enough to what I did.
Let's say I have this:
public interface IData
{
string Data { get; set; }
}
public interface IJob<out T> where T: IData
{
T JobData { get; } // works because no setter
void Run();
}
public class JobAData : IData
{
public string Data { get; set; }
}
public class JobA : IJob<JobAData>
{
public JobAData JobData { get; private set; } // implements IJob's get plus a set
public JobA(JobAData data)
{
JobData = data;
}
public void Run()
{
//can use JobData nicely here
}
}
And, because of the out parameter, this also works:
List<IJob<IData>> jobs = new List<IJob<IData>>();
jobs.Add(new JobA(new JobAData()));
//in another class, extremely simplified (actually running with Quartz)
foreach (var job in jobs)
{
job.Run();
}
While this works fine, it feels like a hack since I have to remember that JobA needs a setter that is not enforced by the interface.
I originally was using a double IJob interface (an IJob and an IJob<T>) but that meant I had to cast from IJob<T> to IJob and I didn't like that.
Is there any cleaner way to do this?
UPDATE
My original suggestion was to create an abstract class that sets the Data in the constructor,
public abstract class JobBase<T> : IJob<T> where T : IData {
public JobBase(T data) {
JobData = data;
}
public T JobData { get; private set; }
public abstract void Run();
}
forcing derived classes to set the JobData property.
public class JobA : JobBase<JobAData> {
public JobA(JobAData data) : base(data) { }
public void Run() {
//can use JobData nicely here
}
}
ORIGINAL ANSWER
Following the abstract base class idea consider a abstract factory method that would force any derived class to provide data, either in the property itself
public abstract class JobBase<T> : IJob<T> where T : IData {
public T JobData { get { return GetData(); } }
public abstract void Run();
public abstract T GetData();
}
or having a private setter and setting it one time in the constructor
public abstract class JobBase<T> : IJob<T> where T : IData {
public JobBase() {
JobData = GetData();
}
public T JobData { get; private set; }
public abstract void Run();
public abstract T GetData();
}
Any derived implementations would be forced to implement the GetData method.
From what I understand, you want to enforce setter definition on inheritance which would have accessibility restriction as well! If you define a setter method, you would still end up making it publicly accessible. And, "double IJob interface (an IJob and an IJob<T>) but that meant I had to cast from IJob<T> to IJob" doesn't sound good to you.
There are not much solutions to this situation but one work around can be restriction using Abstract Classes. What I am suggesting here is something like this:
public interface IData
{
string Data { get; set; }
}
public interface IJob<out T> where T : IData
{
T JobData { get; }
void Run();
}
public class JobAData : IData
{
public string Data { get; set; }
}
public abstract class Abs_JobA : IJob<JobAData>
{
public abstract JobAData JobData { get; protected set; }
public abstract void Run();
}
public class JobA : Abs_JobA
{
public override JobAData JobData
{
get;
protected set;
}
public JobA(JobAData data)
{
this.JobData = data;
}
public override void Run()
{
//can use JobData nicely here
}
}
So now, you do not implement IJob to subsequent classes but, rather you extend Abs_JobA abstract class.

Allow delegate to work with inherited type

I'm not sure if I'm approaching this correctly. I did some searching, but couldn't find a good example.
I would like to have some objects inherit from a base type that includes a common method. I would like to pass in functions as delegates that would be called at the same time as the common method DoWork. But this delegate functions would internally work on properties only available in the child/inherited object.
Something like this:
public abstract class BaseModel
{
public bool IsValid {get; set;}
public delegate Task DelegateWork<in T>(T myObject)
public virtual async Task DoWork<T>(DelegateWork<T> myDelegate)
{
// run some code
await myDelegate(this);
}
}
So, I would have an inherited class:
public interface IChild
{
string ChildName {get; set;}
}
public class MyChild : BaseModel
{
public string ChildName {get; set;}
}
Later in a service, I would add a function;
public class MyService
{
private async Task CheckName<T>(<T> child) where T : BaseModel, IChild
{
if (child.ChildName == "BadData")
child.IsValid = false;
}
public async Task<T> SpecialFunction<T>(T mySpecial) where T : BaseModel, IChild
{
// Do somestuff
await mySpecial.DoWork(CheckName);
}
}
This all looks good, except for BaseModel.DoWork, it doesn't like this.
How should I code it?
Maybe you can educate me... what is the point of using generics in this scenario? The delegate will always require an argument that derives from BaseModel, so you can do it like this:
public abstract class BaseModel
{
public bool IsValid { get; set; }
public delegate Task DelegateWork(BaseModel myObject);
public virtual async Task DoWork(DelegateWork myDelegate)
{
// run some code
await myDelegate(this);
}
}
public interface IChild
{
string ChildName { get; set; }
}
public class MyChild : BaseModel, IChild
{
public string ChildName { get; set; }
}
public class MyService
{
private async Task CheckName(BaseModel model)
{
var child = model as IChild;
if (child != null && child.ChildName == "BadData")
model.IsValid = false;
}
public async Task SpecialFunction(BaseModel mySpecial)
{
// Do somestuff
await mySpecial.DoWork(CheckName);
}
}
Hey I think maybe I got it. Just need to call it with this as T.
public abstract class BaseModel
{
public bool IsValid { get; set; }
public delegate Task DelegateWork<T>(T myObject) where T: BaseModel, IChild;
public async Task DoWork<T>(DelegateWork<T> myDelegate) where T : BaseModel, IChild
{
// run some code
await myDelegate(this as T);
}
}
public interface IChild
{
string ChildName { get; set; }
}
public class MyChild : BaseModel, IChild
{
public string ChildName { get; set; }
}
public class MyService
{
private async Task CheckName<T>(T model) where T: BaseModel, IChild
{
if (model.ChildName == "BadData")
model.IsValid = false;
}
public async Task SpecialFunction<T>(T mySpecial) where T: BaseModel, IChild
{
// Do somestuff
await mySpecial.DoWork<T>(CheckName);
}
}

Categories