Quickstart:

To use the library:

  • You must use Entity Framework 4.1+
  • You must add the ADO.Net DbContext Generator code generation item to your edmx (right click in the body of the edmx and select "Add Code Generation Item..."
  • Add a reference to ettaingroup.data.ef.dll to your project (download or build from source)
  • Once you have a named connection string in your application configuration file, you must add this key to your appSettings: 

 

  <appSettings>
    <add key="EntityFrameworkGenericRepository_ConnectionString" value="testingContainer"/>
  </appSettings>

 

Congratulations!

A typical usage scenario for the repository would be something like:

 

Repository<Application> repo = new Repository<Application>();
            var result = repo.Find(a => a.agencyID == 3 && a.Person.lastName.StartsWith("A"), new Helpers.PagingCriteria() { PageNumber = 1, PageSize = 25, SortBy="Person.lastName", SortDirection="asc" }, "Person", "ApplicationPersons");

if(result.NoErrors && !result.ListIsNullOrEmpty)
{
    // iterate over the results...
    foreach(var item in results.Entities){
        //process the results...
    }
}
           

 

Here we are finding people who are with Agency 3 and who's last name starts with A.  We are sending in paging criteria.  The results object contains paging metadata that can be used in things like DataGrid or jqGrid.  

But wait!  There's more!  The repository Entities collection is still usable for further querying.  You can project the Entities collection into, say, a ViewModel:

 

Repository<UserData> repo = new Repository<UserData>();
            var results = repo.Find(u => u.groupId == this.GroupId);
            if (results.NoErrors)
            {
                var distinctUsers = results.Entities.GroupBy(a => 
                    new { 
                         email = a.email, 
                         firstName = a.firstName, 
                         lastName = a.lastName, 
                         id = a.id, 
                         salary = a.salary }).Select(a =>
                    new WebUserView() { 
                        id = a.Key.id,
                        email = a.Key.email,
                        firstName = a.Key.firstName,
                        lastName = a.Key.lastName,
                        salary = a.Key.points
                    }).ToList();
            }

 

"That's great!" you say.  "How do I UPDATE stuff in the database?  That's where UnitOfWork comes in.  It allows you to use a single context, with multiple entities for transactional data manipulation:

 

 

UnitOfWork uow = new UnitOfWork();
Repository<Person> repo = new Repository<Person>();
// by default, a new DbContext based on your configuration settings
//is created.  You can, however, pass a DbContext to the constructor.
var results = repo.Single(p => p.id == this.personId);
if(results.NoErrors && results.HasValue)
{
    var person = results.Entity;
    person.testData = Model.testData;
    UnitOfWork uow = new UnitOfWork();
    uow.AddAction(person, DataActions.Update);
    uow.Save();
}

 

"Awesome!"  you say.  "How do I get stuff INTO the database?"  UnitOfWork is your friend, once again:

 

 

Person p = new Person();
Address a = new Address();

//we set the properties here
// ...

p.Addresses.Add(a);

UnitOfWork uow = new UnitOfWork();

uow.AddAction(p, DataActions.Add);
uow.Save();

// the person and their address information is now stored.

 

 

It's a powerful generic framework for accessing your data.  We will be adding about the repository and UnitOfWork as the need arises, and frankly as we get time.  I hope this gives you an idea of how powerful and flexible the library is, and how easy your data access can be.

 

 

Last edited Mar 1, 2012 at 10:50 PM by CATucker, version 5

Comments

sudip_p Jun 5, 2015 at 10:43 PM 
Liked it..
Can i get most recent version of the code?

mridulgzp Jun 5, 2012 at 1:55 PM 
How to use this DLL without using edmx file. can I use this by DbContex or an entity class.