Proxy exposing multiple interfaces with Ninject.Extensions.Interception.Linfu - c#

I'm using Ninject.Extensions.Interception (more specifically, InterceptAttribute) and Ninject.Extensions.Interception.Linfu proxying to implement a logging mechanism in my C# app, but I am facing some problems when a proxied class implements several interfaces.
I've a class which implements an interface and inherits from an abstract class.
public class MyClass : AbstractClass, IMyClass {
public string SomeProperty { get; set; }
}
public class LoggableAttribute : InterceptAttribute { ... }
public interface IMyClass {
public string SomeProperty { get; set; }
}
public abstract class AbstractClass {
[Loggable]
public virtual void SomeMethod(){ ... }
}
When I try to get an instance of MyClass from ServiceLocator, the Loggable attribute causes it to return a proxy.
var proxy = _serviceLocator.GetInstance<IMyClass>();
The problem is the proxy returned only recognizes the AbstractClass interface, exposing SomeMethod(). Consequentially, I receive an ArgumentException when I try to access the inexistent SomeProperty.
//ArgumentException
proxy.SomeProperty = "Hi";
In this case, is there a way of using mixin or some other technique to create a proxy exposing multiple interfaces?
Thanks
Paulo

I ran in a similar problem and i did not found a elegant solution with only ninject means. So i tackled the problem with a more basic pattern from OOP: composition.
Applied to your problem something like this would be my suggestion:
public interface IInterceptedMethods
{
void MethodA();
}
public interface IMyClass
{
void MethodA();
void MethodB();
}
public class MyInterceptedMethods : IInterceptedMethods
{
[Loggable]
public virtual void MethodA()
{
//Do stuff
}
}
public class MyClass : IMyClass
{
private IInterceptedMethods _IInterceptedMethods;
public MyClass(IInterceptedMethods InterceptedMethods)
{
this._IInterceptedMethods = InterceptedMethods;
}
public MethodA()
{
this._IInterceptedMethods.MethodA();
}
public Method()
{
//Do stuff, but don't get intercepted
}
}

Related

Add common method to classes inheriting from a C# Interface?

I have an interface such as this one:
public interface ITestInterface
{
int a { get; set; }
void DoSomething();
}
Some of my classes are deriving from this interface:
public class OneClass : ITestInterface
{
public int a { get; set; }
public void DoSomething()
{
Console.WriteLine(this.a.ToString());
}
}
public class AnotherClass : ITestInterface
{
public int a { get; set; }
public void DoSomething()
{
Console.WriteLine((this.a * 2).ToString());
}
}
Since I now need a (large) common method on all classes derived from my interface, I was trying to provide an additional base class for that:
public class MyBaseClass
{
public void LargeCommonMethod()
{
Console.WriteLine((this.a * 3).ToString()); // no 'a' on base class
}
}
This clearly doesn't work because the base class would also need to implement my interface in order to know about that a field.
I am now asking myself what the best approach would be here:
make MyBaseClass inherit from ITestInterface?
set LargeCommonMethod() to protected and provide all internal data it uses via arguments? (There's actually a lot of these..)
skip the interface all along and replace it with an abstract base class?
...?
C# 8 provides a feature precisely for this scenario.
Your classes all implement an interface
You want to add a method to the interface
You don't want a breaking change to all of the existing classes. If you add a method to the interface all of the classes will break unless you find some way to add the method to all of them. (That includes modifying them all to inherit from a new base class.)
That feature is default interface methods.
You can add your method and a default implementation to the interface:
public interface ITestInterface
{
int a { get; set; }
void DoSomething();
void LargeCommonMethod()
{
Console.WriteLine((this.a * 3).ToString());
}
}
Your existing classes that implement the interface will not break. When cast as the interface, you'll be able to call the method which is defined in the interface. You can still modify any class to provide its own implementation, overriding the interface's default implementation.
For the method to be available the object must be cast as the interface - ITestInterface.
A lot of developers - including myself - found this to be an odd feature. But this is the scenario it's for.
Some documentation
The most common scenario is to safely add members to an interface already released and used by innumerable clients.
If you require a base implementation for a method then an interface is clearly not the way to go.
I would choose an abstract class instead and get rid of the interface. There is no need to complicate the design basically.
The Adapter pattern could fit your Use case, when you want to keep the ITestInterface consistent:
public interface ITestInterface
{
int a { get; set; }
void DoSomething();
}
public class TestInterfaceAdapter : ITestInterface
{
private readonly ITestInterface _testInterface;
public int a {
get => _testInterface.a;
set => _testInterface.a = value;
}
public TestInterfaceAdapter(ITestInterface testInterface)
{
_testInterface = testInterface;
}
public void DoSomething()
{
_testInterface.DoSomething();
}
public void LargeCommonMethod()
{
Console.WriteLine((this.a * 3).ToString());
}
}
public class OneClass : ITestInterface
{
public int a { get; set; }
public void DoSomething()
{
Console.WriteLine(this.a.ToString());
}
}
public class AnotherClass : ITestInterface
{
public int a { get; set; }
public void DoSomething()
{
Console.WriteLine((this.a * 2).ToString());
}
}

C# Multiple Generic Parameters into single concrete type

I am little stuck with achieving something with multiple generic parameters. I have a service interface which will take a messagecontext interface as input to method.
public interface IService<T,U> where T : IMessageContext<U>
{
void ExecuteJob(T data);
bool CancelJob();
}
Then an abtract class which implements the interface.
public abstract class AbstractService<T,U> : IService<T,U> where T: IMessageContext<U>
{
public virtual bool CancelJob()
{
return true;
}
public abstract void ExecuteJob(T data);
}
The IMessage interface and default implementation is as shown below
public interface IMessageContext<U>
{
U Data { get; set; }
}
public class DefaultMessageContext : IMessageContext<string>
{
public string Data { get ; set; }
}
In this case, if we inherit the AbstractService class, it would be like below with the Generic parameters for T as DefaultMessageContext & U as string
public class ConcereteService : AbstractService<DefaultMessageContext,string>
{
public override void ExecuteJob(DefaultMessageContext data)
{
//Execute the job
}
}
Is there a way where I can just only mention the type for T as DefaultMessageContext as the type of U is already set in the class while implementing IMessageContext
Something like this
public class ConcereteService : AbstractNewService<DefaultMessageContext>
{
public override void ExecuteJob(DefaultMessageContext data)
{
//Execute the job
}
}
I could introduce another layer of abstraction where the type of U is set as string and only keep T open for implementation like AbstractStringService<T> : AbstractService<T,string>. But I don't want to do that.
Is there any other way to achieve it.

Interface property inheritance

I may not be explaining this well, as I am not familiar with the vocabulary, but I have a base level interface, IMyStuff, that is inherited in a chain multiple times.
interface IOnceRemoved
{
IMyStuff MyStuff{get;set;}
}
interface ITwiceRemoved : IOnceRemoved
{
...
}
interface ITarget : ITwiceRemoved
{
...
}
public void MyMethod(ITarget target)
{
...
}
I have an object of the IMyStuff type. Is there a way to wrap this interface so MyMethod will accept it as an ITarget?
As Camilo already suggested, you need a wrapper like
class MyStuffAsTargetWrapper : ITarget
{
public MyStuffAsTargetWrapper(IMyStuff myStuff)
{
MyStuff = myStuff
}
public IMyStuff MyStuff{ get; set; }
}
and then call MyMethod(MyStuffAsTargetWrapper(myStuff)).

How can I declare two classes so they can be passed to the same function?

Given the following two classes
public class LocalFoo {
}
public class RemoteFoo {
}
Both of these classes SHOULD have similar properties :
public string strBar{ get; set; }
public int intBar{ get; set; }
The difference being how those properties are set and read - in the former case the properties are handled locally - by the program. In the latter, the properties are read and written directly from and two a DataTable read from a Database.
I know that Inheritance is going to have to happen here but that's where I'm getting stuck.
For example, if I declare a method TakesFoo :
public void takesFoo( Foo bar ){
/*Magic Happens Here*/
}
I want that to be able to call that method with an object of either type LocalFoo or RemoteFoo.
I know I could write an abstract class Foo :
public abstract class Foo{
}
and have both LocalFoo and RemoteFoo inherit from it but there's a caveat - I have another abstract class which I need RemoteFoo to inherit that is used making it easier to read/write to/from DataTables -
public abstract class DRHelper : IEquatable<DRHelper>, IDisposable {
}
My initial instinct is to make Foo an interface but I don't know if that will work and I was hoping someone could give me some direction on how I should handle this problem.
Create an IFoo interface that has:
string strBar{ get; set; }
int intBar{ get; set; }
class LocalFoo and class RemoteFoo would both need to implement the IFoo interface.
then change your method as:
public void takesFoo( IFoo bar ){
/*Magic Happens Here*/
}
I think, you should use Visitor Pattern
It allows you to avoid if(bar is LocalFoo) code.
public abstract class Foo
{
public abstract void Accept(IVisitor visitor);
}
public class LocalFoo : Foo
{
public override void Accept(IVisitor visitor)
{
visitor.Visit(this);
}
}
public class RemoteFoo : Foo
{
public override void Accept(IVisitor visitor)
{
visitor.Visit(this);
}
}
public interface IVisitor
{
void Visit(LocalFoo foo);
void Visit(RemoteFoo foo);
}
public class Visitor : IVisitor
{
public void Visit(RemoteFoo foo)
{
/*Magic Happens Here*/
}
public void Visit(LocalFoo foo)
{
/*Magic Happens Here*/
}
}

Accessing a method which is not on the base Interface

I have a design issue and am looking for the best design solution. I have added an example of the issue below.
public interface IVehicle<T>
{
int GetEngineSize();
}
public class Car : IVehicle<Car>
{
public int GetEngineSize()
{
throw new NotImplementedException();
}
public bool HasSpolier()
{
return true;
}
}
public class Bus : IVehicle<Bus>
{
public int GetEngineSize()
{
throw new NotImplementedException();
}
}
public abstract class BaseController<T>
{
public IVehicle<T> Repository { get; set; }
}
public abstract class CarController : BaseController<Car>
{
public CarController()
{
// How can I access the HasSpolier method from the IVehicle<T> without having to cast the Interface to concrete class Car
bool result = Repository.HasSpolier();
}
}
I'm not sure your generics are doing what you want here.
If instead of
IVehicle<T> Repository {get; set;}
You did
T Repository {get; set;}
You could make
public abstract class BaseController<T> where T : IVehicle
To ensure that they're of the IVehicle Interface
Then you'd have a typed repository and get access to your spoiler method.
You're doing IVehicle<Bus> but at least in the sample code, the T is never used in the interface. At this point the T is worthless.
Unless you implement the method in the interface, you can't access it without casting it to another class.
You'd have to cast your Repository to Car.
It would make using your interface pointless as the dependency on the implementation which you're trying to remove is re-introduced.
Also the type parameter on your interface isn't required, you don't use it anywhere else in the interface...
public interface IVehicle
{
int GetEngineSize();
}

Categories

Resources