0

I am new to Entity framework and relatively new to C# as well. I am using Entity Framework with the Repository Pattern. I have a DAL project, a business layer project and a web project which has the aspx pages along with the ObjectDataSource. Right now I have been creating seperate Repositories for all my entities, I would like to use a Generic Repository to handle all the basic CRUD functions. I can create the Generic entity DAL class for all entities like below in the code sample and inherit that in a generic Repository but with an object data Source how do I

1) map the ObjectDataSource's TypeName to a generic Type? assign the TypeName and
ObjectDataTypeName ? The Business Layer generic class inherits the generic IDALEntity class.

 <asp:ObjectDataSource  ID="ODSCustomers" runat="server"
       TypeName="SampleProject.BLL.  " how do i access the Customer instance of BL  
       DataObjectTypeName="SampleProject.DAL. " how do i access the instance of 
                                                Customer entity from the generic DAL 
                                                class? 
       SelectMethod="GetCustomers" >
       <SelectParameters>
         <asp:SessionParameter Name="client_id" SessionField="ClientID" />
       </SelectParameters>

2) How are related entities or Navigation properties handled this way? If I want to display columns from multiple entities for e.g Customer entity and Customer.CustomerAddress Entity as well would I bind the grid columns like DataFied="Customer.CustomerAddress.City" ?

public class DALEntityRepository<T> : IDisposable, IDALEntityRepository<T> where T : class
{
    private PFOEntities _context;
    private ObjectSet<T> _objectSet;

    public DALEntityRepository()
    {
        _context = new Entities(ConnectionStringHelper.GetConnectionString());
        _objectSet = (ObjectSet<T>)GetObjectSet();
    }


    public void Insert(T entity)
    {
        _context.AddObject(_objectSet.EntitySet.Name, entity);
        _context.SaveChanges();
    }

    public void Update(T newVersion, T origVersion)
    {
        _objectSet.Attach(origVersion);
        _context.ApplyCurrentValues(_objectSet.EntitySet.Name, newVersion);
        _context.SaveChanges();
    }

    public void Delete(T entity)
    {
        _context.AttachTo(_objectSet.EntitySet.Name, entity);
        _objectSet.DeleteObject(entity);
        _context.SaveChanges();
    }

    public IQueryable<T> GetEntities()
    {
        return _objectSet;
    }

    public IQueryable<T> GetEntitiesByClientId(int clientId)
    {
        Expression<Func<T, bool>> predicate = (Expression<Func<T, bool>>)GetPredicate(clientId);
        return GetEntities().Where(predicate);
    }


  private object GetPredicate(int clientId)
    {
        object retVal = null;
        Type type = GetType();

        //Use similar if's to check for Different Entities
        if (type == typeof(DataEntityRepository<Customers>))
        {
            Expression<Func<Customers, bool>> predicate = (c) => c.client_id ==    
           clientId;
            retVal = predicate;
        }

                 return retVal;
    }

    private object GetObjectSet()
    {
        object retVal = null;
        Type type = GetType();

        if(type == typeof(DataEntityRepository<Customers>))
        {
            retVal = _context.Customers;
        }
              return retVal;
    }

Your helps appreciated please let me know if I havnt explained clearly or if you have any questions thanks.

Shokwave
  • 311
  • 3
  • 6
  • 22
  • with the repository pattern you usually do databinding in the codebehind and not in the view, remember the view should be dumb and only display data (simple logic is ok), the codebehind is where most people put their logic for the view like databinding and what not and the data access is used just for getting the data. – Joakim Oct 18 '11 at 02:48

1 Answers1

0

For your first question, please refer to: Using generic classes with ObjectDataSource or a more ASP.NET like solution: http://www.manuelabadia.com/blog/PermaLink,guid,91a1d00f-197e-4148-b4e1-cea324029dc6.aspx

As for your second question:

Yes, you can refer to related entities by following the navigation properties but there is a catch. If you dont include the navigation properties in the output (by either navigating over them, selecting them or using the Include statement) and lazy loading is disabled, you will get a NullReferenceException since the navigation properties are not loaded.

My recommendation is to forcefully Include the navigation properties in your query, see: http://msdn.microsoft.com/en-us/library/bb738708.aspx

Community
  • 1
  • 1
Polity
  • 14,734
  • 2
  • 40
  • 40