开发者

ORM with entity classes and domain interfaces/classes

开发者 https://www.devze.com 2023-03-07 17:23 出处:网络
I\'m trying to decide how to handle domain-level classes and interfaces with respect to objects populated via ORM (NHibernate in this case). I\'ve mocked up a very simple scenario that helps to illust

I'm trying to decide how to handle domain-level classes and interfaces with respect to objects populated via ORM (NHibernate in this case). I've mocked up a very simple scenario that helps to illustrate my current situation.

public interface ICar
{
    public bool PassesEmisionStandards(string state);
    public int Horsepower { get; set; }
    public string Model { get; set; }
}

public class CarFromDB
{
    public int Horsepower { get; set; }
    public string Model { get; set; }
}

public class ModelT : CarFromDB, ICar
{
    public bool PassesEmissionStandards(string state)
    {
        return false;
    }

    public override string ToString()
    {
        return Model + " with " + Horsepower + " ponies";
    }
}

In this case, CarFromDB is the class that's got the mapping via NHibernate to my database. ICar is the interface that my UI/Controller code is handling. ModelT is one of the classes that have instances passed to the UI.

In my actual domain, the PassesEmissionStandards is a complicated method that differs significantly among the different derived classes, and the CarFromDB class has a dozen simple properties along with references to other classes, both singly and in lists. This information is all used in the PassesEmissionStandards equivalent.

I'm confused about the best way to e开发者_Python百科nd up with my derived classes decorated with the interface when I start with a populated base class from the ORM. The ideas I've come up with to try to handle this are:

  1. Decorate CarFromDB with ICar and try to come up with a clean way to implement the extensive PassesEmissionStandards method within it or by calling out to other classes for help
  2. Use AutoMapper or the equivalent + a factory to transform my base class objects into derived class objects
  3. Since the derived class type can be identified from a property in the base class, mapp my derived classes for NHibernate and find some way to hook into NHibernate to instruct it which mapped derived class to use.

I feel like this must be a very common issue, but I searched around SO and other places without finding any solid guidelines. Please note: I'm relatively new to ORM and domain modelling and very new to NHibernate. Any help is appreciated.


I don't think that I understand your problem, why can´t you use:

public interface ICar
{
    public bool PassesEmisionStandards(string state);
    public int Horsepower { get; set; }
    public string Model { get; set; }
}

public abstract class CarBase : ICar
{
    public int Horsepower { get; set; }
    public string Model { get; set; }
    public abstract bool PassesEmisionStandards(string state);
}

Or if CarBase is used for all derived classes too, you might want to use strategy pattern

public interface IEmissionCalculator
{
    void Calculate(IEmissionCalculatorContext context);
}

public CarBase : ICar
{
    internal void Assign(IEmissionCalculator calculator){}

    public bool PassesEmisionStandards(string state)
    {
        //assign all info needed for calculations
        var ctx = new IEmissionCalculatorContext {  };
        return _calculator.Check(ctx);
    }
}

You can use the same DB-class, but assign different emission caluclations depending of the type of car.

If that doesn't work either, I would use automapper.

0

精彩评论

暂无评论...
验证码 换一张
取 消