I have a model in RavenDb that I am trying to assemble into an index that will make identifying Clips for Channels you follow. Here are the two objects (properties simplified for discussion) I'm attempting to connect:
public class Clip {
string ChannelId
DateTimeOffset CreatedAt
string Title
...
}
public class FollowerRecord {
string FollowerId
string[] ChannelIDs
}
Each user in the application has only 1 FollowerRecord.
I'd like to output an index that's shaped like this:
public class Index_FollowedClipsByDate {
string FollowerId
DateTimeOffset CreatedAt
string[] Clips
}
I'm a bit stumped on how to load the FollowerRecord, do the many-to-many join between ChannelIds in the FollowerRecord and Clips, and reduce to 1 record per FollowerId and CreatedAt
Related
We have quite a few lookup entities that all have the same structure - just ID and Name. For example, Gender, Ethnicity are just dropdown lists on a Patient view. Each lookup entity has views for viewing/adding/editing the values. Each entity has its own controller with nearly identical actions - Index (view list), Create, and Edit. The only thing different is the type.
Is there a way to create one controller and one set of views to manage all of these lookup entities using generics, a base entity, some other technique or a combination of these?
One simple thing you can do is create an Interface for ID and Name. Then inherit it in the models that you need to.
Example
public interface IEntity
{
int Id { get; set; }
string Name { get; set; }
}
Inherit this in your models
public class Gender : IEntity
{
}
In ASP.NET MVC application I have a model named CarSearchCriteria:
public class CarSearchCriteria{
public int CarMake {get;set;} // This is selected from a dropdownlist
public int YearOfFirstReg {get;set;}
public string ModelVariant {get;set}
}
I have two views - one for editing and the other one for viewing. In the editing view for the CarMake property I can do the following. I know I could have used DropDownListFor but didn't want to mess with SelectList for the time being:
<select name="CarMake">
<option value="1">BMW</option>
<option value="2">Mercedes</option>
<option value="3">Toyota</option>
</select>
So the model binding mechanism will easily bind the selected value to the appropriate model property. But what about the reading mode. I can't show 1s or 2s. I need to show BMW, Mercedes and so on. My question is what is the preferred way, do I have to have a property name that holds the actual textual information, something like CarMakeText?
You could have both the identifier (which you currently have) as well as the Make object itself. The latter would never need to be accessed when building the model, but can be accessed when reading the model. A lazy-loaded read-only property often works well for that. Something like this:
public int CarMakeID { get; set; }
public Make CarMake
{
get
{
if (CarMakeID == default(int))
return null;
// fetch the Make from data and return it
}
}
Naturally, this depends a lot on what a Make actually is and where you get it. If there's just some in-memory list somewhere then that should work fine. If fetching an instance of a Make is a little more of an operation (say, fetching from a database) then maybe some in-object caching would be in order in case you need to access it more than once:
public int CarMakeID { get; set; }
private Make _carMake;
public Make CarMake
{
get
{
if (CarMakeID == default(int))
return null;
if (_carMake == null)
// fetch the Make from data and save it to _carMake
return _carMake;
}
}
David's solution is just fine but for some reason I find my own solution to better fit my needs and besides that I find it more elegant. So basically what I do is I create a class that holds the textual descriptions of all the properties that keep just ID. For example, I have the following model:
public class EmployeeModel{
public int EmployeeID {get;set;}
public string FullName {get;set}
*public int DepartmentID {get;set}
*public int SpecialityID {get;set;}
public int Age {get;set;}
}
The properties marked with asterisk are the properties that keep ids of possible many predefined options and when showing we're supposed to show the actual descriptions, not the number representations. So for this purpose, we create a separate class:
public class EmployeeTextValues{
public string DepartmentName {get;set;}
public string SpecialityName {get;set;}
}
And then I just add this class as a property to my model:
public EmployeeTextValues TextValues {get;set;}
After that, it's quite easy to access it from anywhere, including Razor.
P.S. I'm sure that a lot of people will tend to do the following before initializing this property:
Employee emp=new Employee;
emp.Age=25;
emp.TextValues.DepartmentName="Engineering";// Don't do this
If you try to access or set Textvalues.Someproperty you'll get Object reference not set to an instance of an object. So do not forget to set TextValues first to some initialized object. Just a kind reminder, that's all.
Is there any way to address/refer to a SQL SERVER table, by its Unique Index rather its TableName ?
my Application is using multiple html tables, which are acutally within a form,
for CRUD opporations,(more R/U than Create or delete) of an Existing SQL Table.
now... that i need to mix two separated tables, in same update-form (that's my first 'mix')
as i am using Jquery to pass parameters to C# code behind, so it will take care of the
update opporations, and will be able to differentiate the source of the update post/request
and i do know, i could address the table by its TableName,
i just wanted to make sure, as i was searching for some information on the web,
i've encountred results with the Terms sql server 2005... table index or id,
though all results seem to have something to do with, what i can call, some kind of "manipulation", in oreder to create some indexing system /schema (i think it's like hdds has a FAT table)
and I Emphasize "Create",cause I was actualy looking for an existing /built in, numeric value for a "TableID", just the same as a Table has rows and each row has its row IDENTITY - Column.
so at this point i am turning back to the real question at the top .
so that's what i came up with aventually : a simple struct + a class to hold values for database
public class DBMetaDetails
{
public struct DbTable
{
public DbTable(string tableName, int tableId): this()
{
this.Name = tableName;
this.ID = tableId;
}
public string HtmlOutput;
public string Name { get; private set; }
public int ID { get; private set; }
}
}
public sealed class tblsIDs
{
public const int firstTbl= 1, SecondTbl = 2;
}
another class should be for table names
public sealed class tblsNames
{
public const string firstTbl= "firstTbl", SecondTbl = "SecondTbl";
}
another that will hold tables columns names ...etc
i have the following entities:
public class Worker
{
public int WorkerID {get;set;}
public string Name { get;set;}
public int version { get;set;}
}
public class TransferOrder
{
public int TransferOrderID { get;set;}
public Worker workerTobeTransfered{get;set;}
public int version { get;set;}
}
and i am using the Auto mapping, in fluent nhibernate.
when i try to save the TransferOrder like this:
TransferOrder order = new TransferOrder();
order.Worker = new Worker(){WorkerID = 1};
Session.Save(order);
but in database, the workerID in the TransferOrder table is NULL???
but when i give a version to the worker, it is saved as normal?
TransferOrder order = new TransferOrder();
order.Worker = new Worker(){WorkerID = 1,Version = 1};
Session.Save(order);
notice that it is not important what version number is given to the worker as long as it is not 0.
and i have a worker saved in the database with workerID = 1.
how can i handle this ? why should i give a version to the worker???is the nhibernate making sure that worker is saved?? and why it should do that ?
Maybe this is the scenerio:
Your Work.WorkerID is an identity column within your Work table and, you cannot insert a row into your table that consists of only an identity column entry.
But when you provide a value for Work.Version, as well, then you are creating a valid insert.
The "version" is probably going to be auto mapped to a NHibernate attribute for optimistic concurrency checking. I would use Fluent NHibernate to generate the mapping XML to see what it's using, as I can't find anything via Google about the default settings for auto mapped .
what is hydrate in nhibernate? I am not able to get my head around this term.
This was used, in a video, in context of hydrating the child table rows.
Please advise.
Thanks
AJ
You may wish to look at Build Your own DAL. It has a section on hydration that you probably will find interesting.
But hydration from a really simple view means means take from the raw persistent storage module and map into an object/a list of objects.
Update
Look at Understanding Lazy Loading Strategies or Lazy Loading - Eager Loading. I think this is what is happening for the hydration of the objects in the video you describe.
Example
This is a really simple hydration example (Not an NHibernate example)
Say we have run a query like: select name, breed from tblDogs and that has these results
K9, GermanShepard
IBeBlind, Labrador
SmallAnoyance, Shitsu
Trigger, GermanShepard
And say we have the following object:
public class Dog {
public string name { get; set; }
public string breed { get; set; }
}
Now we can write our own hydrator:
public List<Dog> Hydrate(results rs) {
List<Dog> dogs = new List<Dog>();
foreach(Record rec in rs) {
Dog d = new Dog();
d.name = rec["name"];
d.breed = rec["breed"];
dogs.Add(d);
}
return dogs;
}