ORM and layers

Sorry this moment is all over here ... but I feel like a dog chasing my tail, and I'm all embarrassed at this point.

I am trying to see the cleanest way to develop a three-tier solution (IL, BL, DL), where DL uses ORM for abstract access to the database.

Wherever I have seen, people use LinqToSQL or LLBLGen Pro to create objects that represent database tables, and belong to these classes in all three layers. It seems that 40-year-old coding patterns have been ignored - or a paradigm shift has occurred, and I skipped part of the explanation as to why this works great for this.

Nevertheless, it seems that there is still reason to believe that the data storage mechanism is agnostic - look what just happened with LinqToSQL: a lot of code was written with it - only for MS to refuse this ... Therefore, I would like to isolate ORM- part as best as possible, just don't know how to do it.

So, back to the absolute basics, here are the main parts that I want to assemble in a very simple way:

Collections that I start with: UL.dll BL.dll DL.dll

Main classes:

A message class that has a property that displays the collection (called MessageAddresses) of MessageAddress objects:

class Message 
{
    public MessageAddress From {get;}
    public MessageAddresses To {get;}
}

Functions per layer:

BL provides a UI method called GetMessage (Guid id) that returns an instance of Message.

BL, in turn, wraps DL.

DL ProviderFactory, . DL.ProviderFactory (... ) , GetMessage (Guid id) SaveMessage ( ) , Linq2SQL, LLBLGen Pro , ORM (, VistaDB).

: . , , . , , ORM, DL. , UL BL.

, , :

a) BL ) Db/Orm/Manual ( "DbMessageRecord", "MessageEntity" - , ORM ), DL. c) BL DL d) DL, BL BL, BL BL- (: DbMessageRecord)?

UL:

Main() 
{
    id = 1;
    Message m = BL.GetMessage(id);
    Console.Write (string.Format("{0} to {1} recipients...", m.From, m.To.Count));
}

BL:

static class MessageService 
{ 
    public static Message GetMessage(id)
    {
        DbMessageRecord message = DLManager.GetMessage(id);
        DbMessageAddressRecord[] messageAddresses = DLManager.GetMessageAddresses(id);

        return MapMessage(message, 
    }

    protected static Message MapMessage(DbMessageRecord dbMessage. DbMessageAddressRecord[] dbAddresses)
    {
        Message m = new Message(dbMessage.From);
        foreach(DbMessageAddressRecord dbAddressRecord in dbAddresses){
        m.To.Add(new MessageAddress (dbAddressRecord.Name, dbAddressRecord.Address);
    }
}

DL:

static class MessageManager 
{
    public static DbMessageRecord GetMessage(id);
    public static DbMessageAddressRecord  GetMessageAddresses(id);
}

: a) , . b) c) d) BL DL DL (, DbMessageRecord), , , ORM, ,... ... ORM BL. e) ... BL DL , BL DL.

, ... . .

+5
5

DDD. , , . , , , - . NHibernate FluentNHibernate , goosey.hbm config.

, - , , BL .

, , , , , .

.

. , , db. , , :

[Serializable]
public class AddMediaCategoryRequest : IRequest<AddMediaCategoryResponse>
{
    private readonly Guid _parentCategory;
    private readonly string _label;
    private readonly string _description;

    public AddMediaCategoryRequest(Guid parentCategory, string label, string description)
    {
        _parentCategory = parentCategory;
        _description = description;
        _label = label;
    }

    public string Description
    {
        get { return _description; }
    }

    public string Label
    {
        get { return _label; }
    }

    public Guid ParentCategory
    {
        get { return _parentCategory; }
    }
}

[Serializable]
public class AddMediaCategoryResponse : Response 
{
    public Guid ID;
}


public interface IRequest<T> : IRequest where T : Response, new() {}


[Serializable]
public class Response
{
    protected bool _success;
    private string _failureMessage = "This is the default error message.  If a failure has been reported, it should have overwritten this message.";
    private Exception _exception;

    public Response()
    {
        _success = false;
    }

    public Response(bool success)
    {
        _success = success;
    }

    public Response(string failureMessage)
    {
        _failureMessage = failureMessage;
    }

    public Response(string failureMessage, Exception exception)
    {
        _failureMessage = failureMessage;
        _exception = exception;
    }

    public bool Success
    {
        get { return _success; }
    }

    public string FailureMessage
    {
        get { return _failureMessage; }
    }

    public Exception Exception
    {
        get { return _exception; }
    }

    public void Failed(string failureMessage)
    {
        _success = false;
        _failureMessage = failureMessage;
    }

    public void Failed(string failureMessage, Exception exception)
    {
        _success = false;
        _failureMessage = failureMessage;
        _exception = exception;
    }
}


public class AddMediaCategoryRequestHandler : IRequestHandler<AddMediaCategoryRequest,AddMediaCategoryResponse>
{
    private readonly IMediaCategoryRepository _mediaCategoryRepository;
    public AddMediaCategoryRequestHandler(IMediaCategoryRepository mediaCategoryRepository)
    {
        _mediaCategoryRepository = mediaCategoryRepository;
    }

    public AddMediaCategoryResponse HandleRequest(AddMediaCategoryRequest request)
    {
        MediaCategory parentCategory = null;
        MediaCategory mediaCategory = new MediaCategory(request.Description, request.Label,false);
        Guid id = _mediaCategoryRepository.Save(mediaCategory);
        if(request.ParentCategory!=Guid.Empty)
        {
            parentCategory = _mediaCategoryRepository.Get(request.ParentCategory);
            parentCategory.AddCategoryTo(mediaCategory);
        }
        AddMediaCategoryResponse response = new AddMediaCategoryResponse();
        response.ID = id;
        return response;
    }
}

, ,

, , ,

+2

, , , , - IoC/DI (.. /). , . DL , - , .

, ( ), ( , ). , , POCOs ( , ), DTO ( ). DTOs BL, , - ( " " ), BL ( " Microsoft" ).

Llblgen, NHibernate + IoC, SpringFramework.NET Windsor, , .

+2

, , , Java ( . ). , . , .

, iBatis SQL mapper . ( SQL SQL, ORM, , ). SQL mapper , . ( , , ) DTO. BL SQL Mapper ( , ..) DTO. DTO (UI), , XHTML, XML JSON. , DL-, , DTO, , .

, , . , .

: @Ciel. , DTO - POCO (, , Java POJO). DTO first_name "Jim" .. DTO , . , DL . . 401 ( , ).

ORM, . mapper SQL, SQL. SQL- ( , ), DTO, iBatis, , "select * from Person, personid = # personid #" Java DTO. ORM (Hibernate, , Java), , .

ORM, , , . #, POCO get set, API-, DL, , ORM :

interface Person ...

class ORMPerson : Person ...

, ORM, POCO:

class NewORMPerson : Person ...

DL, BL UI Person.

@Zvolkov () " , " , , Person, dependency injection ORMPersons NewORMPersons , ORM

+1

. , - , , . .

, , .

0

, YMMV.

, , . ( .)

  • , .

  • .

, , , , LINQ ORM.

DL BL . DL . . ( SQL , , , SQL, ). LINQ ORM , , , , ; .

0