简体   繁体   English

取实现接口的类

[英]Take class which implements interface

I have an abstract base model class which contains some shared properties.我有一个包含一些共享属性的抽象基模型类。 On top of that I have a number of minor interfaces, which defines what processes a model will be available for.最重要的是,我有许多次要接口,它们定义了模型可用于哪些进程。

Here is an example of the base class, an interface and a derived class:下面是基类、接口和派生类的示例:

public abstract class ModelBase
{
    public ModelBase(string name) 
    {
        Name = name;
    }

    public int Id { get; private set; }

    public int Name { get; private set; }
}


public interface ISupportProcessA
{
    public decimal Amount { get; }
}


public class ModelDerived : ModelBase
{
    public ModelDerived(string name) : base(name) { }

    public decimal Amount { get; private set; }
}

To perform some work with the base model I also have an abstract base processor class, with some shared functionaliy and some derived processor classes, one for each process.为了使用基本模型执行一些工作,我还有一个抽象的基本处理器类,具有一些共享功能和一些派生处理器类,每个进程一个。 It could look like this:它可能看起来像这样:

public abstract class ProcessorBase
{
    private readonly ModelBase model;

    public ProcessorBase(ModelBase model)
    {
        this.model = model;
    }

    // ...some shared methods
 }


 public class ProcessorA : ProcessorBase 
 {
     private readonly ISupportProcessA model;

     public ProcessorA(ISupportProcessA model) : base(model)
     {
         this.model = model;
     }

     // ...some specific methods
 }

Now here lies the problem.现在问题就在这里。 Because ISupportProcessA is not (to the compilers knowledge) related to ModelBase and can therefore not be given as input in base() .因为ISupportProcessA (编译器的知识)与ModelBase无关,因此不能作为base()输入。 That makes sense.那讲得通。

So what I tried to do was to create an interface IModelBase which I use instead of ModelBase .因此,我试图做的是创建一个接口IModelBase我用,而不是ModelBase However, this gives some issues once I connect to Entity Framework, which doesn't work well with interfaces (or maybe it's just me?).但是,一旦我连接到 Entity Framework,这会带来一些问题,它不适用于接口(或者可能只是我?)。

So here is my question:所以这是我的问题:

Is there anyway to request a class which both derives from ModelBase but also implements ISupportProcessA ?无论如何要请求一个既从ModelBase派生实现ISupportProcessA

You can get some of the way there with generics:您可以通过泛型获得一些方法:

public class ProcessorA<TModel> : ProcessorBase where TModel : ModelBase, ISupportProcessA
{
    private readonly ISupportProcessA model;

    public ProcessorA(TModel model) : base(model)
    {
        this.model = model;
    }

    // ...some specific methods
}

This is a bit ugly however, as you need to create a new ProcessorA<ModelDerived> , rather than just a ProcessorA .然而,这有点难看,因为您需要创建一个新的ProcessorA<ModelDerived> ,而不仅仅是一个ProcessorA


You can add more boilerplate to make things a bit nicer:您可以添加更多样板以使事情变得更好:

public abstract class ProcessorBase
{
    protected abstract ModelBase ModelForBase { get; }


    // ...some shared methods
}

public abstract class ProcessorA : ProcessorBase
{
    public static ProcessorA Create<TModel>(TModel model) where TModel : ModelBase, ISupportProcessA
    {
        return new ProcessorA<TModel>(model);
    }

    // Abstract specific methods
    public abstract void SomeSpecificMethod();
}


public class ProcessorA<TModel> : ProcessorA where TModel : ModelBase, ISupportProcessA
{
    protected override ModelBase ModelForBase => model;

    private readonly TModel model;

    public ProcessorA(TModel model)
    {
        this.model = model;
    }

    // Specific method overrides
    public override void SomeSpecificMethod()
    {
    }
}

This means you can do ProcessorA processor = ProcessorA.Create(new Model()) , but at the cost of a lot more boilerplate.这意味着您可以执行ProcessorA processor = ProcessorA.Create(new Model()) ,但代价是更多样板。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从它实现的接口创建 class 的实例 - Create Instance of a class from interface which it implements 接口方法的签名包括一个class,它实现了接口本身 - Signature of interface's method includes a class, which implements the interface itself 从抽象类继承并实现接口的模拟类 - Mock class which inherit from abstract class and implements interface 如何处理实现接口并具有基类的类? - How to approach a class which implements an interface and has a base class? 这种设计模式是否有名称,其中具体类实现了一个特定接口,该接口实现了 CRUD 操作的基本接口? - Is there a name for this design pattern where a concrete class implements a specific interface which implements a base interface for CRUD operations? Moq具有内部属性并实现接口的类 - Moq a class that has an internal property and which implements an interface 具有通用类型的基类,该基类实现具有通用类型的接口 - Base class with a generic type which implements an interface with a generic type 如何从实现接口的类中调用新方法 - How to call new methods from a class which implements interface 接口中的函数,该函数返回实现该类的对象的类型 - Function in an Interface which returns an object of the type of whatever class implements it 对实现通用接口的抽象类执行“保护的抽象替代” - Perform a 'protected abstract override' for an abstract class which implements a generic interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM