In my ASP.NET MVC Application, I have many actions that return JSONs.
In these JSONs, some sub-structures are repeated.
For example
ajax: "/Bands/Members" is supposed to return the members of a band, which are all musicians
ajax: "/Musicians/All" is supposed to return all the musicians on the system
ajax: "/Musicians/Search" is supposed to return all the musicians that match something
etc...
Here I show (1):
public JsonResult Members(long musicBandId)
{
MusicBand b = db.MusicBands.SingleOrDefault(b => b.MusicBandId == musicBandId);
if (b == null)
return null;
return Json(new
{
error = false,
message = "",
persons = from m in b.Members select new
{
musicianId = p.MusicianId,
name = m.Name,
instrument = new
{
instrumentId = m.instrument.InstrumentId,
model = m.instrument.Model
}
}
}, JsonRequestBehavior.AllowGet);
}
And here I show (2):
public JsonResult All(int page, int pageSize)
{
var musicians = db.Musicians;
var pageOfMusicians = musicians.Skip((page-1) * pageSize).Take(pageSize);
return Json(new
{
error = false,
message = "",
musiciansCount = musicians.Count(),
page = page,
pageSize = pageSize
musicians = from m in pageOfMusicians select new
{
musicianId = m.MusicianId,
name = m.Name,
instrument = new
{
instrumentId = m.instrument.InstrumentId,
model = m.instrument.Model
}
}
}, JsonRequestBehavior.AllowGet);
}
This has several problems:
If I want to change the JSON format, I have to change it in every single action!
example: If I want to change "name" to "fullname", I have to change it in Members() and All()
Lot of "copy pasting": If I'm creating a new action that somewhere in the structure returns a musician, I need to copy and paste that piece of code that represents the musician
{
musicianId = p.MusicianId,
name = p.Name,
instrument = new
{
instrumentId = instrument.InstrumentId,
model = instrument.Model
}
}
Code is too large
What solution exists to this problem?
If you propose a framework, please show me how would the previous queries look with that framework
Notice I'm always using Linq-to-entities in my examples, and I would like to keep it like that because of performance issues. I know that with Linq-to-objects I could to something like:
from m in pageOfMusicians.ToList() select m.GetDTO()
Being GetDTO() some method that can be run with Linq-to-Objects.
But then, again, I want to stick to Linq-to-Entities
Alternative 1
If you don't worry about using dynamics mixed with regular typed C# code you could make a utility method like...
public static dynamic PrepareForMusiciansView(IQuerable<Musician> musicians)
{
return musicians.Select(m => new
{
musicianId = m.MusicianId,
name = m.Name,
instrument = new
{
instrumentId = m.instrument.InstrumentId,
model = m.instrument.Model
}
}
}
...and then...
return Json(new
{
error = false,
message = "",
musiciansCount = musicians.Count(),
page = page,
pageSize = pageSize
musicians = Util.PrepareForMusiciansView(pageOfMusicians)
}, JsonRequestBehavior.AllowGet);
The method name should clearly reflect its purpose in terms of your application. Maybe you want to focus more on the serialization function and use a name like PrepareForJson. Also, it should be consistent with your coding standards. I would avoid it if nowhere else dynamics is used.
Alternative 2
Use AutoMapper (available via NuGet).
With AutoMapper you'd typically have DTO classes for Musician and Instrument, having the properties you want to expose in the view. If these properties have the same names as those in the source classes, AutoMapper will match them by name convention.
Using AutoMapper always involves defining the mappings and executing mappings. Defining the mappings should be done once at application startup. It looks like...
Mapper.CreateMap<Musician, MusicianDto>();
Mapper.CreateMap<Instrument, InstrumentDto>();
There are different ways to execute the mappings, but when working with IQueryable the preferred way is
musicians = pageOfMusicians.Project().To<MusicianDto>()
This projects IQueryable<Musician> to IQueryable<MusicianDto>, including the nested Instrument (if the DTO has a property of that name).
These are two viable alternatives I can think of to reduce the awkward new {} statements to reusable one-liners. It really depends on your application and coding style which alternative you prefer.
Related
Here are two examples of methods, the first one uses a foreach statement to populate a data model
public List<TheOrderSummary> GetTheOrderSummaryByID(int id)
{
ExoEntities = new ExoEntities();
List<TheOrderSummary> lst = new List<TheOrderSummary>();
foreach(var a in query)
{
lst.Add(new TheOrderSummary
{
OrderDetailID = a.OrderDetailID,
OrderHeaderID = a.OrderHeaderID,
ItemSeq = a.ItemSeq,
MaterialID = a.MaterialID,
Description = a.Description,
DWidth = a.DWidth,
DLength = a.DLength,
MaterialArea = a.MaterialArea.ToString(),
MaterialDetail = a.MaterialDetail,
DHeight = a.DHeight,
PurchUnitOfMeasure = a.PurchUnitOfMeasure,
SellUnitOfMeasure = a.SellUnitOfMeasure,
MaterialCategory = a.MaterialCategory,
MaterialType = a.MaterialType,
MaterialSubType = a.MaterialSubType,
ColorID = (int)a.ColorID,
Color = a.Color,
MaterialPrice = a.MaterialPrice,
MaterialCost = a.MaterialCost,
MaterialLocationID = (int)a.MaterialLocationID,
MaterialLocation = a.MaterialLocation,
LaborPrice = a.LaborPrice,
LaborCost = a.LaborCost,
VendorID = (int)a.VendorID,
VendorName = a.VendorName,
Size = a.Size,
Height = (decimal)a.Height,
Length = (decimal)a.Length,
Width = (decimal)a.Width,
PurchaseQuantity = (decimal)a.PurchaseQuantity,
SellQuantity = (decimal)a.SellQuantity,
TotalFootage = (decimal)a.TotalFootage,
GeneratedItemInd = (int)a.GeneratedItemInd,
ExtendedMaterialPrice = (decimal)a.ExtendedMaterialPrice,
ExtendedLaborCost = (decimal)a.ExtendedLaborCost,
ExtendedMaterialCost = (decimal)a.ExtendedMaterialCost
});
}
return lst;
}
and this one uses a stored procedure to return an object
public List<usp_GetTheOrderDetails_Result> GetTheOrderSummaryByID(int id)
{
ExoEntities = new ExoEntities();
var query = ExoEntities.usp_GetTheOrderDetails(id);
return query.ToList();
}
Both of these are in a DAL and the method that could call either one of these is a JSONResult, both of these can be used to populate a grid. What ramifications would using the second type be down the road as opposed to the first one? They both return the exact same thing, from the looks of it on a performance level, without doing the numbers, the second one would be faster
The pain that is introduced by returning the result directly is that you get a "hard-wired" dependency on the information contract between the consumer and the underlying data provider. If you make a change to the underlying data structure, you must update the client at the same time (which may come with various degrees of pain).
If you instead go with your first option, you encapsulate the knowledge of the underlying information structure into this layer, and may use that to map between the old and new contracts, thereby decoupling the direct dependency between the client and the data provider.
So yes, the second option might bit a tad faster (even though that really should be marginal), but the first one is likely to give you much less pain when it comes to maintaining code and deployments in the longer run.
I have the following query:
var userQuizzes = from quiz in Context.Quizzes
select new DashboardQuiz
{
QuizId = quiz.Id,
Questions = quiz.Questions
QuestionExcerpt = quiz.QuizVersion.Questions.FirstOrDefault().QuestionText
// etc...
}
That's all well and good, but is it possible to call my own methods that perform logic on the data as the models are instantiated?
I have a method on my Quiz POCO class IsQuizActive() that determines from the values in Quiz if the quiz is active.
So for example:
var userQuizzes = from quiz in Context.Quizzes
select new DashboardQuiz
{
QuizId = quiz.Id,
Questions = quiz.Questions
QuestionExcerpt = quiz.QuizVersion.Questions.FirstOrDefault().QuestionText
// Custom method IsQuizActive() called here
ActiveQuiz = quiz.IsQuizActive()
}
I get an error saying that it can't be converted to LINQ, which is understandable, but I created DashboardQuiz to use as my View Model.
As it currently stands, I have to loop through my objects and create another model DashboardQuizViewModel that can be used as a view model for each item:
var userQuizzes = from quiz in Context.Quizzes
select new DashboardQuiz
{
QuizId = quiz.Id,
Questions = quiz.Questions
QuestionExcerpt = quiz.QuizVersion.Questions.FirstOrDefault().QuestionText
// I'd like to call IsQuizActive() here
}
List<DashboardQuizViewModel> responseModel = new List<DashboardQuizViewModel();
foreach (var dashboardQuiz in userQuizzes)
{
DashboardQuizViewModel viewModel = new DashboardQuizViewModel();
viewModel.QuizId = dashboardQuiz .id;
viewModel.Questions = dashboardQuiz.Questions;
viewModel.QuestionExcerpt = dashboardQuiz.QuestionExcerpt;
// Call it here instead
viewModel.ActiveQuiz = dashboardQuiz.IsQuizActive();
responseModel.Add(viewModel);
}
return responseModel;
The only other 'tidy' way that I can think of, is to have IsQuizActive as a getter on my View Model and call that from within the view. But I really want the values to be evaluated before passing it to my view model
This cannot be done without creating a model defined function for sql as the query is converted to SQL and SQL does not recognise IsQuizActive as a function by default. You could possibly put the logic in the query but it is very untidy and inefficient. You could shorten the way you are looping through though. I think this should work (calling toList() executes the query and therefore you do not need to convert it to a different view model if you don't want to):
var userQuizzes = from quiz in Context.Quizzes
select new DashboardQuiz
{
QuizId = quiz.Id,
Questions = quiz.Questions
QuestionExcerpt = quiz.QuizVersion.Questions.FirstOrDefault().QuestionText
// I'd like to call IsQuizActive() here
}
var newUserQuizzes = userQuizzes.ToList().Select(x => {
x.ActiveQuiz = x.IsQuizActive();
return x;
});
I'm trying to assemble ad-hoc queries to Breeze.
I have a physician.contact.addresses relationship.
When I try:
myPred = new pred('contact.addresses.street1', op.StartsWith, "a");
And execute it I get:
"The parent value for a property access of a property 'Addresses' is not a single value. Property access can only be applied to a single value."
To try a work-around I've tried parsing out those many-relationships and am passing it to the breeze controller in .withParameters like this:
var criteriaStr = toManyArray.length ? ko.utils.stringifyJson(toManyArray) : "";
query = query.withParameters({ searchParms: criteriaStr });
where toManyArray is an array of fieldName:value pairs.
on the controller side:
[HttpGet]
public IQueryable<Physician> Physician(string searchParms = null)
{
if (searchParms != null)
{
var ser = new JavaScriptSerializer();
var searchCritAry = ser.Deserialize<String[]>(searchParms);
foreach (var aryItem in searchCritAry)
{
// aryItem looks like this:
// f_str_street_from_addresses:a
var predEnt = aryItem.Split(':')[0].Split('_')[4];
var predField = aryItem.Split(':')[0].Split('_')[2];
var predVal = aryItem.Split(':')[1];
switch (predEnt)
{
case "addresses":
switch (predField)
{
case "street":
//physPool =
_contextProvider.Context.Physicians
.Where(p => p.Contact.Addresses.Any(a => a.Street1.StartsWith(predVal)));
break;
case "street2":
//physPool =
_contextProvider.Context.Physicians
.Where(p => p.Contact.Addresses.Any(a => a.Street2.StartsWith(predVal)));
break;
}
break;
}
}
// here I want to combine the .Where clauses from above with the breeze preds
return _contextProvider.Context.Physicians;
}
return _contextProvider.Context.Physicians;
}
It's not working and only returning a selection using the predicates that are passed in the normal way through Breeze's query. I don't see how to pass the filtered IQueryable to Breeze's _contextProvider.
Thanks for any suggestions.
Take a look at the new "any/all" support in Breeze ( as of version 1.4.7). Some examples may be found here: http://www.breezejs.com/documentation/query-examples
You can construct your query/predicate like this:
var pred = new Breeze.Predicate('contact.addresses', "any", 'street1', op.StartsWith, "a");
var query = EntityQuery.from("Physicians").where(pred);
or simply
var query = EntityQuery.from("Physicians")
.where("contact.addresses", "any", "street1", "startsWith", 'a')
You may want to add an expand if you want the contact and address information sent down as well.
query = query.expand("contact.addresses");
If you have a need for nested any/all expressions you may need to add the following configuration to your BreezeController to determine just how deep you want to allow the expressions to go (2 in the example below):
[BreezeController(MaxAnyAllExpressionDepth = 2)]
I have method that looks like this:
private static IEnumerable<OrganizationViewModel> GetOrganizations()
{
var db = new GroveDbContext();
var results = db.Organizations.Select(org => new OrganizationViewModel
{
Id = org.OrgID,
Name = org.OrgName,
SiteCount = org.Sites.Count(),
DbSecureFileCount = 0,
DbFileCount = 0
});
return results;
}
This is returns results pretty promptly.
However, you'll notice the OrganizationViewModel has to properties which are getting set with "0". There are properties in the Organization model which I added via a partial class and decorated with [NotMapped]: UnsecureFileCount and SecureFileCount.
If I change those 0s to something useful...
DbSecureFileCount = org.SecureFileCount,
DbFileCount = org.UnsecureFileCount
... I get the "Only initializers, entity members, and entity navigation properties are supported" exception. I find this a little confusing because I don't feel I'm asking the database about them, I'm only setting properties of the view model.
However, since EF isn't listening to my argument I tried a different approach:
private static IEnumerable<OrganizationViewModel> GetOrganizations()
{
var db = new GroveDbContext();
var results = new List<OrganizationViewModel>();
foreach (var org in db.Organizations)
{
results.Add(new OrganizationViewModel
{
Id = org.OrgID,
Name = org.OrgName,
DbSecureFileCount = org.SecureFileCount,
DbFileCount = org.UnsecureFileCount,
SiteCount = org.Sites.Count()
});
}
return results;
}
Technically this gives me the correct results without an exception but it takes forever. (By "forever" I mean more than 60 seconds whereas the first version delivers results in under a second.)
Is there a way to optimize the second approach? Or is there a way to get the first approach to work?
Another option would be to load the values back as an anonymous type and the loop through those to load your viewmodel (n+1 is most likely the reason for the slowness).
For example:
var results = db.Organizations.Select(org => new
{
Id = org.OrgID,
Name = org.OrgName,
DbSecureFileCount = org.SecureFileCount,
DbFileCount = org.UnsecureFileCount,
SiteCount = org.Sites.Count()
}).ToList();
var viewmodels = results.Select( x=> new OrganizationViewModel
{
Id = x.Id,
Name = x.Name,
DbSecureFileCount = x.DbSecureFileCount,
DbFileCount = x.DbFileCount,
SiteCount = x.SiteCount
});
Sorry about the formatting; I'm typing on a phone.
You are basically lazy loading each object at each iteration of the loop, causing n+1 queries.
What you should do is bring in the entire collection into memory, and use it from there.
Sample code:
var organizationList = db.Organizations.Load();
foreach (var org in organizationList.Local)
{
//Here you are free to do whatever you want
}
I am trying to get information from a database, convert it to a list and return the CustomerList. This first bit of code works fine. The second example is where I’m trying to accomplish the same thing except the fields are coming from my database. What’s wrong with what I’m doing and how can I make this work? The second piece of code works elsewhere in my project but not here.
private SchoolIn.Models.CustomerList CreateCustomerList()
{
return new SchoolIn.Models.CustomerList()
{
new SchoolIn.Models.Customer { Id = 1, Name = "Patrick", Address = "Geuzenstraat 29", Place = "Amsterdam" },
new SchoolIn.Models.Customer{ Id = 2, Name = "Fred", Address = "Flink 9a", Place = "Rotterdam" },
new SchoolIn.Models.Customer { Id = 3, Name = "Sjonnie", Address = "Paternatenplaats 44", Place = "Enkhuizen" },
new SchoolIn.Models.Customer { Id = 4, Name = "Henk", Address = "Wakerdijk 74", Place = "Utrecht" },
new SchoolIn.Models.Customer { Id = 5, Name = "Klaas", Address = "Paternatenplaats 44", Place = "Plaantan" }
};
}
private SchoolIn.Models.CustomerList CreateCustomerList()
{
return new SchoolIn.Models.CustomerList()
{
SchoolInDB db = new SchoolIn.Models.SchoolInDB();
var courseprogresses = db.CourseProgresses.Include(c => c.Course).Include(c => c.Teacher);
return View(courseprogresses.ToList());
};
}
First things first the second code is invalid C#. So I suppose it doesn't event compile. You cannot use such expressions in an object initialization syntax. Please learn C# before getting into ASP.NET MVC.
The other problem is that your method is private and you are attempting to return View which is something that you do in a controller action. The view method returns an ActionResult whereas your method return type is SchoolIn.Models.CustomerList which once again is wrong.
So move this into some controller action where you would instantiate your database access context and then perform the query and return the model to the corresponding view for display:
public class HomeController: Controller
{
...
public ActionResult CreateCustomerList()
{
SchoolInDB db = new SchoolIn.Models.SchoolInDB();
var courseprogresses = db
.CourseProgresses
.Include(c => c.Course)
.Include(c => c.Teacher)
.ToList();
return View(courseprogresses);
}
}
and if you wanted to keep this into a separate method:
private List<SchoolIn.Models.CourseProgress> CreateCustomerList()
{
SchoolInDB db = new SchoolIn.Models.SchoolInDB();
return db
.CourseProgresses
.Include(c => c.Course)
.Include(c => c.Teacher)
.ToList();
}
In your first function you are returning actual list while in second function you are not returning list, instead you have returned the View with model as a list. So for it you need to return View Result viz. ActionResult.
Hope it helps