How to associate a Interface, with classes from different solutions
I have a interface called iChannels, which has only one method.
using CHNL;
namespace iInterface
{
public interface iChannels
{
string getData(string docXML);
}
}
Then in another project I have a class called Channel1, wich is defined like this:
using iInterface;
namespace CHNL
{
public class Channel1:iChannels
{
string getData(string str)
{
return str;
}
}
}
I had to make a cross-reference, for the interface and class know each other. After that I have a webform, and I just want to use using iInterface;
, but if I only do that, I can't create Channel1 objects.
My intent is to create Channel1 objects, just using iInterface
Library.
I think I can see your issue now.
When you say 'My intent is to create Channel1 objects, just using iInterface Library'...this is not possible in C#, or at least not possible directly creating Channel1 objects without Channel1 being declared in the same library as iChannels.
You can achieve this though through dependency injection. Broadly speaking your webform will be dependent on iChannels and a dependency container will provide it will an implementation of iChannels, in your case Channel1.
Have a look at this article;
Why does one use dependency injection?
You cannot inherit from iInterface as iInterface is the name of a namespace and not the interface, so I think you need the following code;
using iInterface;
namespace CHNL
{
public class Channel1 : iChannels
{
string getData(string str)
{
return str;
}
}
}
Your webform does not need to know about the Channel1 class but the project your webform is in will need to reference the project Channel1 is in.
You can create an instance of iChannels using something like;
iChannels myIChannels = new Channel1();
Your webform can then reference iChannels without being aware of the actual concrete implementation of iChannels, in this case Channel1.
It is better practice though to use either a factory or dependency injection to create actual implementations of iChannels and avoid using 'new Channel1();' altogether.
链接地址: http://www.djcxy.com/p/82260.html上一篇: 这是实施工厂模式的正确方法吗?
下一篇: 如何将接口与不同解决方案中的类关联起来