Istalling Examples

To install the examples released is very easy. Just have to uncompress the files, create a database using the script on the web app example and indicate the created database on the .config files.

Model
To initialize and keep the model the only needed action is to create a class where the model of the software would be set.

    /// <summary>
    /// This class represents the helper class of the Model.
    /// <remarks>
    /// No implementation is needed on this class, unless some changes must be done on
    /// the base implementations.
    /// </remarks>
    /// </summary>
    public class TestModelHelper : EntityFramework.Helper.Model.ModelHelper<TestModelEntities>
    {
    }


Creating Persister and Searcher classes
The following examples are to illustrate how to create the classes for each entity.

Persister
This kind of class would be used to persist changes in the database.

    /// <summary>
    /// This persister is responsible for the Product entity.
    /// <remarks>
    /// This persister would be used to persist any change made in exist objects or to 
    /// create new ones.
    /// </remarks>
    /// </summary>
    public class ProductsPersister : EntityFramework.Helper.Persist.Persister<Products>
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ProductsPersister()
            : base(Model.TestModelHelper.Context)
        {
        }
    }


Searcher
This kind of classes would be use to retrieve data from da database.

    /// <summary>
    /// This class woudl be the searcher of product entity objects.
    /// <remarks>
    /// This class would be used to search objects of the product entity.
    /// </remarks>
    /// </summary>
    [System.ComponentModel.DataObject]
    public class ProductsSearcher : EntityFramework.Helper.Search.Searcher<Products>
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ProductsSearcher()
            : base(TestModelHelper.Context)
        {
        }
    }


Find/Retrieving Examples

FindAll
This example illustrate how easy it is, after configured, to retrieve data using the EFH.

// instanciate the searcher of the entity.
ProductsSearcher searcher = new ProductsSearcher();

// retrieve all the data from the specified type.
var result = searcher.FindAll();


FindFirst
This example illustrate how to retrieve the first record of the collection.

// instanciate the searcher of the entity.
ProductsSearcher searcher = new ProductsSearcher();

// retrieve the first record of the Products collection .
var result = searcher.FindFirst();


FindAllDefaultOrderedBy
This functionality, if configured in the searcher, will retrieve all the data of the table ordered by one of the type properties.

// instanciate the searcher of the entity.
ProductsSearcher searcher = new ProductsSearcher();

// retrieve all the data from the specified type ordered.
var result = searcher.FindAllDefaultOrderedBy(Helper.Search.OrderByDirection.Ascending);

Could be also called with the Descending order.

// retrieve all the data from the specified type ordered.
var result = searcher.FindAllDefaultOrderedBy(Helper.Search.OrderByDirection.Descending);

It is possible to call without any parameter, the result would be the same if called with the Ascending as the parameter

// retrieve all the data from the specified type ordered.
var result = searcher.FindAllDefaultOrderedBy();

FindOne
The last Find example is thhe FindOne. This method, as the name said, will retrive one object. In order to choose this object one criteria should be provided to the method, so it could decide which object matches the given criteria. One code example of this method would be provided further on in this page.

Criterias
In this section we will show one of the most interesting feature of this helper framework, the way we build search criterias.
There are two kinds of criterias to be used.

Simple Criteria
This criteria would be built using any kind of property and type.
It would be used to build a criteria with not typed values associated with any kind of property.

One SimpleCriteria has 4 base methods, but the structure leave the possibility to create more methods.
The base methods are:

/// <summary>
/// Creates a Equal condition to be validated when matching the criteria.
/// </summary>
/// <param name="propertyName">The compared property name.</param>
/// <param name="value">The value to be compared.</param>
void AddEqual(string propertyName, object value);

/// <summary>
/// Creates a NotEqual condition to be validated when matching the criteria.
/// </summary>
/// <param name="propertyName">The compared property name.</param>
/// <param name="value">The value to be compared.</param>
void AddNotEqual(string propertyName, object value);


/// <summary>
/// Creates a Like condition to be validated when matching the criteria.
/// </summary>
/// <param name="propertyName">The compared property name.</param>
/// <param name="value">The value to be compared.</param>
/// <param name="type">Indicates the type of like condition that would be aplied.</param>
void AddLike(string propertyName, object value, Like type = Like.Full)

/// <summary>
/// Creates a Equal condition to be validated when matching the criteria.
/// </summary>
/// <param name="propertyName">The compared property name.</param>
/// <param name="firstValue">The first value to be used as based on the comparison.</param>
/// <param name="secondValue">The secon value tu be used as top limit on the comparison.</param>
void AddBetween(string propertyName, object firstValue, object secondValue);

Below you can find a example using the SimpleCriteria. The example below uses the FindOne method as well.

// creates a Product searcher
ProductsSearcher searcher = new ProductsSearcher();

// creates a Product SimpleCriteria instance
SimpleCriteria<Products> crit = new SimpleCriteria<Products>();
crit.AddNotEqual("ProductName", "test");

// retrieves all the objects that matches the informed criteria.
var result = searcher.FindOne(crit);


The criterias internally work using Conditions. For one object match a criteria all the conditions has to be fulfilled.
There are 4 default conditions that are used on the 4 base SimpleCriteria methods. Each condition is responable to validate itself in one object.
In order to create a new method for a criteria one Condition should be created. The process is quite simples, there is a base class that should be used and then one constructor and the validate method should be implemented.

Below you can find the class declaration of one of the default conditions.

 /// <summary>
/// Condition of equality.
/// </summary>
/// <typeparam name="E">The type of the entity class that would be validated.</typeparam>
internal class EqualCondition<E> : Condition<E>
                                                   where E : System.Data.Objects.DataClasses.EntityObject
{
     // Constructor

    // Validate Method Implementation
}


Object Criterias
This second type of criterias are based on one type.
All the conditions of a search, would be created on object of the criteria type.

However for one object be retrieved on the Match method it must have all the indicated properties identical to the build ObjectCriteria.

// creates one ObjectCriteria for the Products Type.
ObjectCriteria<Products> eqCrit = new ObjectCriteria<Products>();

// set the properties and values that should be checked on the objects.
eqCrit.CurrentEntityObject.UnitPrice = 21;

// retrieves the objects that matches the criteria.
var result = searcher.FindAll(eqCrit);

Last edited Oct 20, 2010 at 4:14 PM by andkler, version 7

Comments

priyankbhat Mar 4, 2014 at 8:32 AM 
This page was last updated in Oct 2010. Post that looks that there is no further update on this fine wrapper. I hope some body puts more supportive samples.

andkler Sep 30, 2010 at 7:30 AM 
Ok, I will put some examples here ASAP.

I know the Repository Pattern but I've never used it. Seem to me that in a superficial way, all of the persistence frameworks kind of use this pattern, because all of them encapsulate an data structure and expose it in a OO way. In my opinion EFH is a specific implementation for Entity Framework and a good Repository implementation wouldn't be to an specific kind of data persister, would be generic and could be used with differents persistence frameworks.

Please correct me if I'm wrong.

mertner Sep 30, 2010 at 12:18 AM 
It would be useful with some source examples here to illustrate the main selling points of using the library.

Also, have you looked at the Repository pattern? This looks to me as if it is mostly trying to do the same thing, but then I only spent a minute browsing the sources and could be wrong.