Should ORM objects be expanded or encapsulated?

I am having trouble understanding how to use the generated ORM objects. We use LLBLGen to map our database model to objects. We encapsulate these objects in another layer, which represents our business model (I think).

Perhaps this bit of code will explain this better.

public class Book { // The class as used in our application
    private BookEntity book;      // LLBLGen entity
    private BookType bookType;    // BookType is another class that wraps an entity

    public Book(int Id) {
        book = new BookEntity(Id);
    }

    public BookType BookType {
        get { return this.bookType; }
        set { 
            this.bookType = value; 
            this.book.BookType = new BookTypeEntity(value.ID);
            this.book.Save();
        }
    }

    public int CountPages() { }  // Example business method
}

Identifying object fields, such as properties, seems inconvenient as I am matching again. With list types, this is even much worse, since I have to write the Add and Remove methods plus the property that the list provides.

BookType BookTypeEntity, , , BookType. .

, BookEntity -? , , ?

LLGLGen , . , - (, CountPages) .

+3
3

, LLGLGen, , ORM, - , IBook. getter wrapping. , , IBook , , - .

, , 3 "" ORM- :

  • , . , .
  • , , ORM-
  • ORM-

# 1, 2 . DRY, KISS YAGNI .

# 3, - , ORM.

.. , № 2, 3 ;)

[] :)

ORM :

public class Book : IBook
{
   public string ISBN {get; private set;}
}

IBook - :

public interface IBook
{
    string ISBN {get;}
}

public class BookWrapper   //Or whatever you want to call it :)
{
    //Create a new book in the constructor
    public BookWrapper()
    {
        BookData = new Data.MyORM.CreateNewBook();
    }

    //Expose the IBook, so we dont have to cascade the ISBN calls to it
    public IBook BookData {get; protected set;}
    //Also add whichever business logic operation we need here
    public Author LookUpAuther()
    {
       if (IBook == null)
          throw new SystemException("Noes, this bookwrapper doesn't have an IBook :(")
       //Contact some webservice to find the author of the book, based on the ISBN
    }
}

, , , , :)

+3

LLBLGen , ORM, , . /​​, ( , ).

, . ORM, Facade, Data Object -... , , , .

UPDATE

, . , .

MSDN:

partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { }

class Earth : Planet, IRotate, IRevolve { }
+5

.

, , . , . . , . . , , .

, , , , . , , . , , , . .

( ) , . - NHibernate. , , .

, , , , : , - .

+1

Source: https://habr.com/ru/post/1711171/


All Articles