本文介绍了你能调用接口方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在构建一个对象模型层次结构来简化数据操作,我想知道是否有更好的方法来做到这一点。



这个例子是高度抽象的为了简洁起见。每个模型对象都已编码为执行自己的读/写操作,因此UI代码简洁明了。

So I am building an object model hierarchy to simplify data operations and I am wondering if there is a better way to do this.

The example is highly abstracted for brevity. Each model object has been coded to perform its own read/write operations so that the UI code is clean and simple.

public class DataObject
{
 // a container class that basically contains
 // information from a row in a table in the target database
}

// core interface that models implement
public interface IDataModel
{
 DataObject SourceObject { get; }
}

// interface that defines object creation function
public interface ICreatableDataModel : IDataModel
{
 // contains a single declaration with two variables, the database
 // to write to and where to get variables specific to it's environment (QA, PROD, etc...)
 DataObject CreateObject(DataAccess data, EnvironmentInfo environment);
}

// interface applied to an object that can be related to another object
public interface IPrimaryRelatable
{
 void RelateToSource(DataObject sourceObject, EnvironmentInfo environment);
}

// interface applied to objects that have secondary relationships
public interface ISecondaryRelatable
{
 void CreateSecondaryRelationships(DataAccess data, EnvironmentInfo environment);
}

// Example source data model class
public class SimpleModel : IDataModel, ICreatableDataModel
{
 public DataObject { get; set; }
 public DataObject CreateObject(DataAccess data, EnvironmentInfo environment)
 {
  // write this object to the database and return it's data representation.
 }
}

// Example related data model class
public class RelatedModel : IDataModel, ICreatableDataModel, IPrimaryRelatable, ISecondaryRelatable
{
 private List<icreatabledatamodel> _secondaryObjects;

 public DataObject { get; set; }
 public DataObject CreateObject(DataAccess data, EnvironmentInfo environment)
 {
  // write this object to the database and return it's data representation.
 }
 public void RelateToSource(DataObject source, EnvironmentInfo environment)
 {
  // this creates the relationship two objects
 }
 public void CreateSecondaryRelationships(Database database, EnvironmentInfo environment)
 {
  // this creates any secondary objects and then relates them to this object
 }
}

// In my UserControl, I wrap this up nice an neat
// There is one object that must be created on the form but there are
// several other points of data they may or may not be needed
public class MyControl : UserControl
{
 private ICreatable _primaryModel;
 private List<icreatabledatamodel> _otherModels;

 private void Create_Click(object sender, RoutedEventArgs e)
 {
  CreateObjects();
 }

 private void CreateObjects()
 {
  DataAccess data = app.Data;
  EnvironmentInfo env = app.EnvironmentInfo;

  // this creates the primary object 
  DataObject sourceObject = _primaryModel.CreateObject(data, env);

  // iterate through the other models that will be created from the form
  // relate them if necessary
  foreach (ICreatableDataModel model in _relatedModels)
  {
    DataObject obj = model.CreateObject(data, env);

    "The following code is what I am trying to simplify"
    // relate to source if needed
    IPrimaryRelatable pRel = model as IPrimaryRelatable;
    if (pRel != null)
    {
     pRel.RelateToSource(sourceObject, env);
    }
    // handle other relationships if needed
    ISecondaryRelatable sRel = model as ISecondaryRelatable;
    if (sRel != null)
    {
     sRel.CreateSecondaryRelationships(data, env);
    }
   }
  }
 }
}



实际的模型类实现了执行所需的接口对数据库的所有读/写操作。所有形式需要做的是投射和测试。



我想知道,有没有比接口的演员/测试/执行更好的方法?



我尝试了什么:



我寻找类似于对象的方式?.Invoke(方法) ;但我所能找到的只是一种使用Reflection的复杂方式。


The actual model classes implement the interfaces necessary to perform all of the read/write operations to the database. All the form needs to do is cast and test.

I am wondering, is there a better way than the cast/test/execute for interfaces?

What I have tried:

I looked for a way similar to object?.Invoke(method); but all I could find was a complicated way that used Reflection.

推荐答案

if (model is IPrimaryRelatable) {
    ((IPrimaryRelatable)model).RelateToSource(sourceObject, env);
}
if (model is ISecondaryRelatable) {
    ((ISecondaryRelatable)model).CreateSecondaryRelationships(data, env);
}


这篇关于你能调用接口方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:44