Why doesn't DoIt() method call get intercepted? Should I use something other than InterfaceInterceptor to intercept the DoIt() method? How would you do it?
using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
namespace UnityTest
{
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<ILogger, Logger>();
container.Configure<Interception>().SetInterceptorFor<ILogger>(new InterfaceInterceptor());
var logger = container.Resolve<ILogger>();
logger.Write("World.");
Console.ReadKey();
}
}
public interface ILogger
{
void Write(string message);
[Test]
void DoIt(string message);
}
public class Logger : ILogger
{
public void Write(string message)
{
DoIt(message);
}
public void DoIt(string message)
{
Console.Write(message);
}
}
public class TestAttribute : HandlerAttribute
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new TestHandler();
}
}
public class TestHandler : ICallHandler
{
public int Order { get; set; }
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.Write("Hello, ");
return getNext()(input, getNext);
}
}
}
You need to apply the [Test] attribute to ILogger.Write instead of DoIt. The way interception works is to create a transparent proxy which then passes control to any handlers before the target method. The problem with your current setup is that DoIt is called by the logger instance itself, so there is no way for the proxy to intercept the call. In other words, you can only intercept methods called directly on an interface when using interception.
Related
I Know
IoC is a design principle which recommends the inversion of different kinds of controls in object-oriented design to achieve loose coupling between application classes.
But I have confilct with the following code:
static void Main(string[] args)
{
ProductService ProductService = new ProductService(new LogInDB());
ProductService.Log();
Console.ReadKey();
}
public class ProductService
{
private readonly Ilog log;
public ProductService(Ilog _log)
{
log = _log;
}
public void Log()
{
log.Log();
}
}
public interface Ilog
{
void Log();
}
public class LogInFile : Ilog
{
public void Log()
{
Console.WriteLine("Log Into File");
}
}
public class LogInDB : Ilog
{
public void Log()
{
Console.WriteLine("Log Into Data Base");
}
}
What is difference between previous and next code
In the first code I used IOC (and added product service) but next I'm using just late binding
but i see IOC not added any value
static void Main(string[] args)
{
Ilog logObj = new new LogInDB();
logObj.Log();
//I still able to using LogInDB
//Ilog logObj = new new LogInDB();
//logObj.Log();
Console.ReadKey();
}
public interface Ilog
{
void Log();
}
public class LogInFile : Ilog
{
public void Log()
{
Console.WriteLine("Log Into File");
}
}
public class LogInDB : Ilog
{
public void Log()
{
Console.WriteLine("Log Into Data Base");
}
}
This depends on your defintion of value. One advantage of IoC would be a better testability of your code, which many would argue adds a lot of value. You can easily inject mocked classes into your test code and only test the class you want to test.
By the way your example is not compileable because of the line Ilog logObj = new new LogInDB();
Let's say, I have a list of strategies which are presented as interfaces and a class (Container) which implements these strategies explicitly. Now I want a method to perform one of these strategies as it's specified in the generic type parameter. The question is in the code.
interface IStrategy
{
void PerformAction();
}
interface IChasingStrategy : IStrategy
{
new void PerformAction();
}
interface ITestStrategy : IStrategy
{
new void PerformAction();
}
class Container : IChasingStrategy, ITestStrategy
{
void IChasingStrategy.PerformAction()
{
Console.WriteLine("ChasingStrategy");
}
void IStrategy.PerformAction()
{
Console.WriteLine("Strategy");
}
void ITestStrategy.PerformAction()
{
Console.WriteLine("TestStrategy");
}
}
class Program
{
static void PerformStrategy<TStrategy>(Container container) where TStrategy : IStrategy
{
TStrategy strategy = container; //why can't I use here implicit casting?
//However this is valid:
//IStrategy strat = container;
//IChasingStrategy ch_strat = container;
//ITestStrategy ts_strat = container;
strategy.PerformAction();
}
static void Main(string[] args)
{
var container = new Container();
PerformStrategy<IChasingStrategy>(container);
}
}
You can let it decide at runtume, which Strategy should it run by the following code.
class Container<T> Where T : IStrategy
{
public void ActionFactory()
{
T.PerformAction();
}
.
.
.
}
And in the Program class:
static void PerformStrategy<TStrategy>(Container<TStrategy> container) where TStrategy : IStrategy
{
container.ActionFactory(); //to perform the desired action for your strategy
}
When you calling the PerformStrategy method, you can decide what container should it get by:
PerformStrategy(new Container<IChasingStrategy>);
You can take off the class binding by making the ActionFactory method generic.
class Container
{
public void ActionFactory<T>() where T: IStrategy
{
T.PerformAction();
}
.
.
.
}
class Program
{
static void PerformStrategy<TStrategy>(Container container) where T: IStrategy
{
container.ActionFactory<TStrategy>(); //to perform the desired action for your strategy
}
}
PerformStrategy<IChasingStrategy>(new Container());
I have a class which is decorated with [TestFixture] attribute and this class contains methods that are decorated with [Test] attribute, each method signature is
public void MethodName([ValueSource("TestConfigurations")] TestConfiguration tConf)
also there are set up and tear down methods
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
}
[SetUp]
public void TestSetUp() { }
[TearDown]
public void TestTearDown()
{
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
}
how can I run this unit test class via reflection in c#?
Thank you in advanced
Something like:
public static class RunUnitTestsClass<TUnitTests> where TUnitTests : new()
{
private static IEnumerable<MethodInfo> WithAttribute<TAttribute>()
{
return typeof(TUnitTests).GetMethods().Where(method => method.GetCustomAttributes(typeof(TAttribute), true).Any());
}
private static void RunWithAttribute<TAttribute>()
{
var unitTests = new TUnitTests();
foreach (var method in WithAttribute<TAttribute>())
method.Invoke(unitTests, new object[0]);
}
public static void RunTestFixtureSetup()
{
RunWithAttribute<TestFixtureSetUp>();
}
// same for the rest of them
public static void RunTests(TestConfiguration tConf)
{
var unitTests = new TUnitTests();
foreach (var method in WithAttribute<Test>())
method.Invoke(unitTests, new []{tConf});
}
}
I have an interface IInterface and it looks something like below -
public interface IInterface
{
void SomeMethod1();
void SomeMethod2();
void SomeMethod3();
.
.
.
}
One of the implementations is something like -
public class Implementation : IInterface
{
private Object obj;
public Implementation(Object obj)
{
this.obj = obj;
// Do Something
}
public void SomeMethod1()
{
lock(obj)
{
// Do Something
}
}
public void SomeMethod2()
{
// Do Something
}
public void SomeMethod3()
{
lock(obj)
{
// Do Something
}
}
.
.
.
}
How to pass a static readonly instance of type Object while registering Implementation class with type IInterface via unity configuration?
My preferred approach is probably to create a factory for creating IInterfaces
public interface IInterface
{
void SomeMethod1();
}
public interface IInterfaceFactory
{
IInterface CreateInterface();
}
public class StandardInterfaceFactory : IInterfaceFactory
{
// Define your static lock object here. Other customers
// can define their own IInterfaceFactory to use a
// different lock object.
private static readonly object lockObject = new object();
public IInterface CreateInterface()
{
return new StandardInterface(lockObject);
}
}
public class StandardInterface : IInterface
{
private readonly object lockObject;
public StandardInterface(object lockObject)
{
this.lockObject = lockObject;
}
public void SomeMethod1()
{
lock (this.lockObject)
{
Console.WriteLine("I've locked on " + lockObject);
}
}
}
Your unity configuration and client code will then look like this.
void Main()
{
IUnityContainer container = new UnityContainer();
// This mapping can be done trivially in XML configuration.
// Left as an exercise for the reader :)
container.RegisterType<IInterfaceFactory, StandardInterfaceFactory>();
IInterfaceFactory factory = container.Resolve<IInterfaceFactory>();
IInterface myInterface = factory.CreateInterface();
myInterface.SomeMethod1();
}
We are using factory to create an instance of Subscribers. Each subscriber can have its own dependency.
Each subscriber will use constructor injection.
Should I pass dependency into subscribers through Subscriber Factory? Every time adding new dependency in any subscriber will change Subscriber factory?
public interface IMessageSubscriber
{
bool Process(string message)
}
public class MessageSubscriber1 : IMessageSubscriber
{
public bool Process(string message)
{
//Some custom logic
}
}
public class MessageSubscriber2 : IMessageSubscriber
{
public bool Process(string message)
{
//Some custom logic
}
}
public class MessageSubscriberFactory
{
//SubscriberType is enum
public IMessageSubscriber Get(SubscriberType type)
{
if(type == 1)
{
return new MessageSubscriber1();
}
else if(type == 2)
{
return new MessageSubscriber2();
}
}
}
//Main class
public class Process
{
public static void Main(string[] args)
{
MessageSubscriberFactory fac = new MessageSubscriberFactory();
foreach SubscriberType
{
string = "Message";
IMessageSubscriber subscriber = fac.Get(type);
subscriber.Process(message)
}
}
}
One approach would be to use named registrations with a DI/IOC container. This would involve using the container in a service locator fashion (which some people oppose), but I think it could make sense in this case. The example below is pretty crude, but it does give you an approach to handle subscribers with different dependencies without passing them into the factory. I used Unity here and you'd want to wrap the container reference rather than referencing directly, but this gets the point across.
public interface ILowerCaseWriter
{
void Write(string message);
}
public class LowerCaseWriter : ILowerCaseWriter
{
public void Write(string message)
{
Console.WriteLine(message.ToLower());
}
}
public interface IUpperCaseWriter
{
void Write(string message, int number);
}
public class UpperCaseWriter : IUpperCaseWriter
{
public void Write(string message, int number)
{
Console.WriteLine("{0}:{1}", number, message.ToUpper());
}
}
public interface ISubscriber
{
void Write();
}
public class Subscriber1 : ISubscriber
{
private ILowerCaseWriter _writer;
public Subscriber1(ILowerCaseWriter writer)
{
_writer = writer;
}
public void Write()
{
_writer.Write("Using subscriber 1");
}
}
public class Subscriber2 : ISubscriber
{
private IUpperCaseWriter _writer;
public Subscriber2(IUpperCaseWriter writer)
{
_writer = writer;
}
public void Write()
{
_writer.Write("Using subscriber 2", 2);
}
}
public class SubscriberFactory
{
private UnityContainer _container;
public SubscriberFactory()
{
_container = new UnityContainer();
_container.RegisterType<ILowerCaseWriter, LowerCaseWriter>();
_container.RegisterType<IUpperCaseWriter, UpperCaseWriter>();
_container.RegisterType<ISubscriber, Subscriber1>("Subscriber1");
_container.RegisterType<ISubscriber, Subscriber2>("Subscriber2");
}
public ISubscriber GetSubscriber(int type)
{
switch (type)
{
case 1:
return _container.Resolve<ISubscriber>("Subscriber1");
case 2:
return _container.Resolve<ISubscriber>("Subscriber2");
default:
throw new Exception();
}
}
}
class Program
{
private static void Main(string[] args)
{
var factory = new SubscriberFactory();
var subscriber = factory.GetSubscriber(1);
subscriber.Write();
Console.ReadLine();
}
}