ObjectFactory class

Handles retrieval and persistance of data object classes to and from databases.

Namespace: ReFlex.Data namespace
Assembly: ReFlex.Data.dll

Syntax

public class ObjectFactory<ObjectType> : IDisposable

Constructors

ObjectFactory();
ObjectFactory(string connectionStringName);

Remarks

ObjectFactory provides object-relational mapping functionality for use in the automated retrieval and persistance of your data object classes.

The premise is you create a class that represents each of your database tables, define through attributes on your classes and their properties any additional information that ObjectFactory needs to know, then start retrieving, changing and persisting your objects.

Examples

[RelationalExplicitMapping("CurrentCompany", typeof(Company), "CurrentCompanyID")]
[RelationalExplicitMapping("PreviousCompany", typeof(Company), "PreviousCompanyID")]
public class Contact
{
    public int ContactID;

    [RelationalMemberInfo(Length = 50)]
    public string FirstName;

    [RelationalMemberInfo(Length = 50)]
    public string LastName;

    [RelationalMemberInfo(IsIntZeroConvertedToNull = true)]
    public int CurrentCompanyID;

    [RelationalMemberInfo(IsIntZeroConvertedToNull = true)]
    public int PreviousCompanyID;
}

public class Company
{
    public int CompanyID;

    [RelationalMemberInfo(Length = 50)]
    public string CompanyName;
}

public class ContactManager : ReFlex.Data.ManagerBase<Contact>
{
    /// <summary>
    /// Gets a list of Contact object that have a FirstName like John, and
    /// a company name that equals the companyName input
    /// </summary>
    /// <param name="companyName"></param>
    /// <returns></returns>
    public List<Contact> GetJohns(string companyName)
    {
        if (string.IsNullOrEmpty(companyName))
            throw new ArgumentNullException("companyName");

        // specify a relationship that will be included in the Criterion
        // with strong parent (inner join) relationship type
        Relationship currentCompanyRelationship = new Relationship(typeof(Company), typeof(Contact), "CurrentCompany", RelationshipTypeEnum.StrongParent);
       
        // create query checking Contact's with FirstName like John
        // and CurrentCompany.CompanyName equals companyName
        Criterion<Contact> criterion = new Criterion<Contact>(CriteriaOperatorEnum.And)
            .Add("FirstName", "John", ComparisonOperatorEnum.Like, CriteriaOperatorEnum.And)
            .Add(new Field<Company>("CompanyName", "CurrentCompany"), companyName, ComparisonOperatorEnum.Equals, CriteriaOperatorEnum.And);

        // specify how the results should be ordered using the Ordering object
        Ordering ordering = new Ordering()
            .Add<Contact>("LastName", Direction.Ascending)
            .Add<Contact>("FirstName", Direction.Ascending);

        // instantiate ObjectFactory and run the query, catching the
        // resulting list of matching Contact objects
        List<Contact> contacts;
        using (ObjectFactory<Contact> mapper = new ObjectFactory<Contact>())
            contacts = mapper.GetDataObjects(criterion, ordering, currentCompanyRelationship);

        return contacts;
    }
}