Pan Zdrowie
Pan Zdrowie

Reputation: 347

What kind of object I should pass from DAL to BLL if I need to create custom type?

I don't know what kind of object I should pass from DAL to BLL if I need to create custom type.
In DAL I have an entity:

public class Note
{
    [Key]
    public int NoteId { get; set; }

    [Required]
    [Column(TypeName = "varchar(MAX)")]
    public string Content { get; set; }

    public DateTime CreatedDate { get; set; }

    public DateTime ModifiedDate { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
}

But I need to return to BLL only: NoteId, Content and number of comments which aren't spam so in DAL I have a query:

public IEnumerable<WhatIsIt> GetNotesWithNoSpamCommentsCount()
{
    var whatIsIt = context.Notes.Select(x =>
        new WhatIsIt
        {
            NoteId = x.NoteId,
            NoteSummary = x.Content,
            NoteCommentsCount = x.Comments.Where(y => y.IsSpam == false).Count() 
        }).ToList();

    return whatIsIt;
}

What kind of object is it which I return - is it Data Access Object or Model or Data Transfer Object or Business Object or something else?

Upvotes: 1

Views: 506

Answers (3)

Divyang Desai
Divyang Desai

Reputation: 7866

As per the code you've written, it could be your DTO(Date transfer object). I assume here that Note is your entity class. So, it wouldn't best practices to use entity class to transfer the data between layers.

You can use DTO object to transfer data with required properties only. But again keep in mind, your viewpage need a model class to bind the data. So you've to map your DTO to your entity class with using AutoMapper or other library.

Hope this helps!

Upvotes: 1

Nikhil Vartak
Nikhil Vartak

Reputation: 5117

I see you are only concerned about WhatIsIt that is striped down version of Note and will be the result of your DAL query. I have different opinion on this i.e. WhatIsIt should not be part of and thus returned from DAL to BLL. I will come back to this in a moment. Before that let's revise few terms.

Domain / Business object - They are usually same and include business logic.

Domain object / DTO - If business logic is performed outside of domain object then that object can be considered to be DTO that has nothing but properties and is used for data transfer across layers.

Entity vs DTO - Entity is purely part of data model and are used by ORMs or data access layer in general to work with data stores.

Now back to the point I made earlier - "WhatIsIt should not be part of and thus returned from DAL to BLL". To me WhatIsIt is a DTO that will be created by BLL once it gets the Entity object back from DAL (you already have referenced DAL in BLL so this should not be a problem). And then BLL will pass this DTO up to the presentation layer, ViewModel to be specific, if applicable.

Upvotes: 0

Biswabid
Biswabid

Reputation: 1411

Model and DTO can be different. Both are used for different purpose. Here in your context I think you should create a new DTO (model)object which will contain only the required fields that you need to send from DAL to BAL. It will be more secure and faster as it will carry less data.

Upvotes: 0

Related Questions