【问题标题】:How to use a model / entity class to create an threaded instance class?如何使用模型/实体类创建线程实例类?
【发布时间】:2014-02-17 15:17:10
【问题描述】:

从例如传递模型类是否被认为是一种好习惯?将 DAL 转换为线程服务类作为“信息模型”?

例子:

public class CarModel {

    public string Name                 { get; set; }
    public int    AmountOfWheels       { get; set; }
    public Engine EngineDescription    { get; set; }

}

public class Car {

    public CarModel CarModel        { get; set; }

    public Car(CarModel model) {
        this.CarModel = model;

        Thread.Start(BrummBrumm);
    }

    private void BrummBrumm() {
        // start the car
    }

}

这个例子是在假设 CarModel 是一个实体(例如与实体框架或任何其他存储库/DAL 一起使用)或模型类与 UI、WebApi、WCF.. 一起使用的假设下进行的,而 Car 是一个驻留的类作为例如实施Windows 服务。

编辑更多代码

public class CarManager {

    public List<Car> Cars = new List<Car>();

    public void Add(CarModel model) {
        this.Cars.Add(new Car(model));
    }

    public void Remove(int id) {
        ...
    }

}

...那么上面的例子是什么?如果我不仅有汽车,还有摩托车怎么办?上面的例子不会创建很多样板代码吗?

【问题讨论】:

标签: c# oop architecture


【解决方案1】:

最佳做法是在从/向服务传输数据时使用 DTO。还有一些工具可以减少从/到业务对象映射 DTO 所需的代码。我个人使用 Automapper。

有一篇很好的文章,以防你想了解 DTO 的内容 - Pros and Cons of Data Transfer Objects

但是,如果您的解决方案很小并且不应该增长并且您的服务没有公开公开,那么您最好只使用您的业务对象以避免过度设计。

希望对你有帮助!

【讨论】:

  • @Acrotygma 您好,您没有对我的回答做出任何贡献,只是想知道它是否对您有帮助?如果没有,我能帮您解决问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 2021-10-10
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多