Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository pattern and Navigation properties

I just want know best practice about writing repository methods. Problem is deciding to write repository in which context has no lazy loading. How do you naming your method if it is GetById but it is not clear that which the navigations included in entity.

So I am thinking to write method names like GetUserByIdIncludedPosts Or it is better to use lazy loading activated context ?

If I write included properties in method names so will be really annoying long method names for few navigation property.

like image 875
Freshblood Avatar asked Mar 13 '11 23:03

Freshblood


People also ask

What are navigational properties?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name.

What is the Repository pattern?

The Repository pattern. Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.

What is Repository pattern in MVC?

The main purpose of the repository pattern is to isolate the data access layer and business logic.In Asp.Net MVC model is used to interact with the Data Access layer and Controller for performing Data access operation. The controller is responsible for passing data to the view.

What is the use of Unitofwork?

The unit of work class serves one purpose: to make sure that when you use multiple repositories, they share a single database context. That way, when a unit of work is complete you can call the SaveChanges method on that instance of the context and be assured that all related changes will be coordinated.


1 Answers

I am using the following in my repository base class to allow retrieval of entities along with a user-specified list of dependencies/relations:

protected DbSet<T> Objects { get; private set; }
protected YourDatabaseContext Context { get; private set; }

public virtual T GetByID( int id, params string[] children )
{
    if( children == null || children.Length == 0 )
    {
        return Objects.SingleOrDefault( e => e.ID == id );
    }
    DbQuery<T> query = children.Aggregate<string, DbQuery<T>>( Objects, ( current, child ) => current.Include( child ) );
    return query.SingleOrDefault( e => e.ID == id );
}

The code uses EF4/CTP5 and therefore uses Db* classes, but is trivial to convert back to the normal EF4 classes (e.g. ObjectSet instead of DbSet).

This would be used like so:

var product = productsRepository.GetByID( 42, "Category", "Orders.OrderLines" );

which would fetch you a product with Category and Orders populated as well as all orders having their OrderLines eagerly loaded.

like image 185
Morten Mertner Avatar answered Sep 27 '22 22:09

Morten Mertner