This question is locked. New answers and comments are not allowed.
Hello
I am having problem with Include() and FetchStrategies.
What did i do :
- Following database first approach, Created a Domain Model using Telerik Data Access. This resulted in 'rlinq' file.
- Since I was going to implement repository pattern and the quickest way I found with Telerik Data Access was that, I right click the rlinq file, and select "Create Telerik Data Access Service...'
-
Now I have e.g. an autogenerated entity by the rlinq file
01.
public
partial
class
Approval
02.
{
03.
private
long
_approvalId;
04.
public
virtual
long
ApprovalId
05.
{
06.
get
07.
{
08.
return
this
._approvalId;
09.
}
10.
set
11.
{
12.
this
._approvalId = value;
13.
}
14.
}
15.
16.
private
long
? _clientId;
17.
public
virtual
long
? ClientId
18.
{
19.
get
20.
{
21.
return
this
._clientId;
22.
}
23.
set
24.
{
25.
this
._clientId = value;
26.
}
27.
}
28.
29.
private
string
_subscriptionId;
30.
public
virtual
string
SubscriptionId
31.
{
32.
get
33.
{
34.
return
this
._subscriptionId;
35.
}
36.
set
37.
{
38.
this
._subscriptionId = value;
39.
}
40.
}
41.
42.
private
Company _company;
43.
public
virtual
Company Company
44.
{
45.
get
46.
{
47.
return
this
._company;
48.
}
49.
set
50.
{
51.
this
._company = value;
52.
}
53.
}
54.
55.
private
IList<Calling> _callings =
new
List<Calling>();
56.
public
virtual
IList<Calling> Callings
57.
{
58.
get
59.
{
60.
return
this
._callings;
61.
}
62.
}
I the base repository interface (note this is also auto generated)
1.
public
interface
IOpenAccessBaseRepository<TEntity, TContext> where TContext : OpenAccessContext,
new
()
2.
{
3.
IQueryable<TEntity> GetAll();
4.
TEntity GetBy(Expression<Func<TEntity,
bool
>> filter);
5.
TEntity AddNew(TEntity entity);
6.
TEntity Update(TEntity entity);
7.
void
Delete(TEntity entity);
8.
}
Concrete implementation of above base repository, all other repositories are derived from this base repository (from the base repository, i am pasting implementation of only two GET functions for brevity)
01.
public
abstract
partial
class
OpenAccessBaseRepository<TEntity, TContext> : IOpenAccessBaseRepository<TEntity, TContext> where TContext : OpenAccessContext,
new
()
02.
{
03.
protected
TContext dataContext =
new
TContext();
04.
protected
FetchStrategy fetchStrategy =
new
FetchStrategy();
05.
06.
public
IQueryable<TEntity> GetAll()
07.
{
08.
List<TEntity> allEntities = dataContext.GetAll<TEntity>().ToList();
09.
10.
List<TEntity> detachedEntities = dataContext.CreateDetachedCopy<List<TEntity>>(allEntities, fetchStrategy);
11.
12.
return
detachedEntities.AsQueryable();
13.
}
14.
15.
public
TEntity GetBy(Expression<Func<TEntity,
bool
>> filter)
16.
{
17.
if
(filter ==
null
)
18.
throw
new
ArgumentNullException(
"filter"
);
19.
20.
TEntity entity = dataContext.GetAll<TEntity>().SingleOrDefault(filter);
21.
if
(entity ==
null
)
22.
return
default
(TEntity);
23.
24.
TEntity detachedEntity = dataContext.CreateDetachedCopy(entity, fetchStrategy);
25.
26.
return
detachedEntity;
27.
}
28.
}
A Derived Repository from the above Base Repository
01.
public
partial
class
ApprovalRepository : OpenAccessBaseRepository<Approval, VizAppModel>
02.
{
03.
public
new
IQueryable<Approval> GetAll()
04.
{
05.
this
.fetchStrategy.LoadWith<Approval>(a => a.Company);
06.
List<Approval> allEntities = dataContext.GetAll<Approval>().ToList();
07.
08.
List<Approval> detachedEntities = dataContext.CreateDetachedCopy<List<Approval>>(allEntities, fetchStrategy);
09.
10.
return
detachedEntities.AsQueryable();
11.
}
12.
13.
}
PROBLEM NOTE : the above repository is never actually used, this new GetAll() function is never called, instead GetAll() function from the base repository is called by the program. I am using a Service Layer, this is simple class calling from repositories as follows:
The Service Classes derive from an Interface as follows:
01.
public
interface
IVizAppService<TEntity, TContext> where TContext : OpenAccessContext,
new
()
02.
{
03.
IQueryable<TEntity> GetAll(
string
sid);
04.
TEntity GetById(
object
id,
string
sid);
05.
TEntity AddNew(TEntity entity,
string
sid);
06.
TEntity Update(TEntity entity);
07.
void
Delete(TEntity entity);
08.
void
HardDelete(TEntity entity);
09.
}
Concrete implementation of this IVizAppService .... the methods in this class are called by controllers directly
01.
public
class
ApprovalService : IVizAppService<Approval, VizAppModel>
02.
{
03.
protected
IOpenAccessBaseRepository<Approval, VizAppModel> service;
04.
05.
public
ApprovalService()
06.
{
07.
this
.service =
new
ApprovalRepository();
08.
}
09.
public
ApprovalService(IOpenAccessBaseRepository<Approval, VizAppModel> service)
10.
{
11.
this
.service = service;
12.
}
13.
14.
15.
public
IQueryable<Approval> GetAll(
string
sid)
16.
{
17.
return
service.GetAll()
18.
.Where(i => i.SubscriptionId == sid && i.IsActive ==
true
);
19.
}
20.
21.
public
Approval GetById(
object
id,
string
sid)
22.
{
23.
if
(id ==
null
)
24.
{
25.
throw
new
ArgumentNullException(
"id"
);
26.
}
27.
return
service.GetAll().Include(i => i.Company)
28.
.SingleOrDefault(i => i.ApprovalId == (
long
)id);
29.
}
PROBLEM THAT I AM FACING AND CANNOT SOLVE = FETCHSTRATEGY OR 'INCLUDE' NOT WORKING
my GetAll methods in above service class, despite using 'Include' still cant get related entity 'Company' in the calls. I have tried a lot using fetch strategy but I know there is something wrong with the architecture or i just cant get my head around it. Most of the code is boilercode which is added automatically.
PLEASE HELP ..... If i could drop the service layer and use Enity Repository directly, i would gladly do so, but since i cant get 'repository class' to reference in my controllers, i used this above service layer.
Thanks for your help
Ali