Using a generic Repository/DataContext

Aug 13, 2009 at 2:26 PM
Edited Aug 13, 2009 at 2:32 PM

Hello everyone!

I just wanted to ask if there was any specific reason that IQToolkit does not support retrieving a table by just its entity type alone (out-of-the-box) but requires an explicit tableId?

And by that I mean:

        public IUpdatable<User> Users
        {
            get { return _provider.GetTable<User>("USERS"); }
        }

You already specify the table name with the mapping (so why the need to specify it again?):

[Table(Name = "USERS")]

I added an extra method to IEntityProvider like so:

    public interface IEntityProvider : IQueryProvider
    {
        IEntityTable<T> GetTable<T>();
        // Etc...
    }

Then extended DbEntityProviderBase.cs as follows:

        public IEntityTable<T> GetTable<T>()
        {
            return (IEntityTable<T>)GetTable(Mapping.GetEntity(typeof(T)));
        }

Now you can access any mapped entity and not have to worry about the table name and this opens up the ability to request a table based on entity type without knowing the type before-hand:

        public IQueryable<TEntity> Query<TEntity>() where TEntity : class, IEntity
        {
            return _provider.GetTable<TEntity>();
        }

Some other methods that are now possible and are quite easy to use:
public IQueryable<TEntity> Find<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class, IEntity { return _provider.GetTable<TEntity>().Where(predicate); } public TEntity GetById<TEntity>(int key) where TEntity : class, IEntity { return _provider.Find<TEntity>(e => e.Id == key).FirstOrDefault(); }

Now I can use my Repository class again and it's methods to retrieve my records:

var q = uow.Find<User>(u => u.Deleted).Take(10).ToList();

Of course I realise this is my own personal way of doing things when using IQueryable and that many people might not prefer to code like this but I was surprised that Linq-to-Sql has a nice GetTable<TEntity>() method but IQToolkit did not, so I just wanted to let people know howto add one to the toolkit if they also use a generic repository/datacontext to do the heavy work :-).

As always any comments/hints/tips/suggestions are more then welcome, thank you.

 

Coordinator
Aug 13, 2009 at 10:53 PM

The string in the GetTable call does not correspond to the database table name, it corresponds to the 'id' inside the mapping that has all the mapping info.  If you are using mapping attributes, the name is the name of the property with the mapping attributes declared on it (which is the name of the property the call to GetTable is inside of.) 

You can simply leave this argument null and get the behavior you want.  If you leave it null, the mapping by attributes system will search the class for a property that returns a collection or query of that type and find its attributes there..

 

 

Aug 14, 2009 at 7:40 AM
I realize that (now) but I just want to have a clean call in the code so I can omit the parameter.
Extremely happy that the code allows for this level of flexibility :-) The way the mapping works is quite awesome.

We are not using the built-in properties, since they are published through the repository in a generic matter and not directly accessible from our pages, or rather for now we do not allow direct access to them. They are however usable from within the services in order to write filters and/or extension methods on IQueryable<TEntity>.

Not sure if we want to publish the IQueryable<TEntity> all the way to the page level, but that's something entirely different!

Thanks for correcting me on that misunderstanding and keep up the amazing work Matt!

On Thu, Aug 13, 2009 at 11:53 PM, mattwar <notifications@codeplex.com> wrote:

From: mattwar

The string in the GetTable call does not correspond to the database table name, it corresponds to the 'id' inside the mapping that has all the mapping info.  If you are using mapping attributes, the name is the name of the property with the mapping attributes declared on it (which is the name of the property the call to GetTable is inside of.) 

You can simply leave this argument null and get the behavior you want.  If you leave it null, the mapping by attributes system will search the class for a property that returns a collection or query of that type and find its attributes there..

 

 

Read the full discussion online.

To add a post to this discussion, reply to this email (IQToolkit@discussions.codeplex.com)

To start a new discussion for this project, email IQToolkit@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--

(\__/)
(='.'=)
(")_(")