This question is locked. New answers and comments are not allowed.
Let me see if I can explain this:
First I have a simple EntitiesModel created via the designer which currently have one table for now but will include more later (This unfortunately is on a database this has had some abuse of the years and relationships are "loosely" enforced, but I digress)
Now I have an MVC application that is using Data Access via a service layer which both projects share a viewmodel project.
So if I have for Example
1 project with Open Access Entity (Accessed only by the service layer)
1 Project with View Models (Accessed by service layer and Application)
1 Project with Service Layer (Accessed by Application)
1 Project MVC Application
So for example I may have a class like this in my Open Access Entity Class:
Then have a view on this entity in the model project like this:
Which as you can see is pretty much a copy of the open access model.
So if I was using this model in a Kendo Grid and had filtering, paging, sorting etc. being used I want to be able to pass these options back through the chain to the service layer and then some how translate the various options that have been created via the DataSourceRequest object associated with the grid and perform the filtering, sorting, grouping etc on the server before bringing the results back so that I can then cast them from the Open Access entity to the view model entity.
so for example I have something like this in the service layer:
I need some mechanism to be able to try and cast/convert/reflect the viewmodel back to the open access entity so that I can apply the filters, sorting etc to the right properties in the sql query.
Ideally I would like a generic solution in which I can just pass in my viewmodel T and output it to OpenAccess Entity U so that the dynamic linq created can apply the filters correctly.
Hopefully this explains what it is I am after.
What I want to avoid is having to repeat a lot of code for something that I know I will do a lot for this project and future projects. It also means if I change the model then I don't have to change the business logic too much to account for a filter being added/removed.
Thanks in advance for any assistance with this.
First I have a simple EntitiesModel created via the designer which currently have one table for now but will include more later (This unfortunately is on a database this has had some abuse of the years and relationships are "loosely" enforced, but I digress)
Now I have an MVC application that is using Data Access via a service layer which both projects share a viewmodel project.
So if I have for Example
1 project with Open Access Entity (Accessed only by the service layer)
1 Project with View Models (Accessed by service layer and Application)
1 Project with Service Layer (Accessed by Application)
1 Project MVC Application
So for example I may have a class like this in my Open Access Entity Class:
01.
public
partial
class
Student
02.
{
03.
private
Guid _iD;
04.
public
virtual
Guid ID
05.
{
06.
get
07.
{
08.
return
this
._iD;
09.
}
10.
set
11.
{
12.
this
._iD = value;
13.
}
14.
}
15.
16.
private
Guid _schoolsID;
17.
public
virtual
Guid SchoolsID
18.
{
19.
get
20.
{
21.
return
this
._schoolsID;
22.
}
23.
set
24.
{
25.
this
._schoolsID = value;
26.
}
27.
}
28.
29.
private
Guid _gendersID;
30.
public
virtual
Guid GendersID
31.
{
32.
get
33.
{
34.
return
this
._gendersID;
35.
}
36.
set
37.
{
38.
this
._gendersID = value;
39.
}
40.
}
41.
42.
private
string
_firstName;
43.
public
virtual
string
FirstName
44.
{
45.
get
46.
{
47.
return
this
._firstName;
48.
}
49.
set
50.
{
51.
this
._firstName = value;
52.
}
53.
}
54.
55.
private
string
_lastName;
56.
public
virtual
string
LastName
57.
{
58.
get
59.
{
60.
return
this
._lastName;
61.
}
62.
set
63.
{
64.
this
._lastName = value;
65.
}
66.
}
67.
68.
}
Then have a view on this entity in the model project like this:
01.
public
class
StudentModel
02.
{
03.
04.
public
Guid ID {
get
;
set
; }
05.
06.
[Required]
07.
[Display(Name =
"Gender"
)]
08.
public
string
GenderID {
get
;
set
; }
09.
10.
[Required]
11.
[Display(Name =
"First Name"
)]
12.
public
string
FirstName {
get
;
set
; }
13.
14.
[Required]
15.
[Display(Name =
"Last Name"
)]
16.
public
string
LastName {
get
;
set
; }
17.
18.
[Required]
19.
[Display(Name =
"Date of Birth"
)]
20.
public
DateTime? DateOfBirth {
get
;
set
; }
21.
22.
[Required(AllowEmptyStrings =
true
)]
23.
[Display(Name =
"UPN"
)]
24.
public
string
UpnNumber {
get
;
set
; }
25.
26.
[Required(AllowEmptyStrings =
true
)]
27.
[Display(Name =
"Adno"
)]
28.
public
string
Adno {
get
;
set
; }
29.
30.
[Required]
31.
[Display(Name =
"Left School"
)]
32.
public
bool
HasLeft {
get
;
set
; }
33.
34.
}
Which as you can see is pretty much a copy of the open access model.
So if I was using this model in a Kendo Grid and had filtering, paging, sorting etc. being used I want to be able to pass these options back through the chain to the service layer and then some how translate the various options that have been created via the DataSourceRequest object associated with the grid and perform the filtering, sorting, grouping etc on the server before bringing the results back so that I can then cast them from the Open Access entity to the view model entity.
so for example I have something like this in the service layer:
01.
List<StudentViewModel> model = OaContext.Students.OrderBy(
"my sorting options"
).Where("my group of filters").Select(s =>
new
StudentViewModel()
02.
{
03.
ID = s.ID,
04.
FirstName = s.FirstName,
05.
LastName = s.LastName,
06.
UPN = s.UPN,
07.
DateOfBirth = s.DateOfBirth,
08.
YearGroup = s.Adno
09.
}
10.
).ToList();
I need some mechanism to be able to try and cast/convert/reflect the viewmodel back to the open access entity so that I can apply the filters, sorting etc to the right properties in the sql query.
Ideally I would like a generic solution in which I can just pass in my viewmodel T and output it to OpenAccess Entity U so that the dynamic linq created can apply the filters correctly.
Hopefully this explains what it is I am after.
What I want to avoid is having to repeat a lot of code for something that I know I will do a lot for this project and future projects. It also means if I change the model then I don't have to change the business logic too much to account for a filter being added/removed.
Thanks in advance for any assistance with this.