PetaPoco fetch only retrieves totalCount but not items - petapoco

Using PetaPoco, I am running a fetch similar to this:
var result=db.Page<article>(1, 20, // <-- page number and items per page
"SELECT * FROM articles WHERE category=#0 ORDER BY date_posted DESC", "coolstuff");
where article is defined as
public class article
{
public long article_id { get; set; }
public string title { get; set; }
public DateTime date_created { get; set; }
public bool draft { get; set; }
public string content { get; set; }
}
But while result.TotalItems shows the correct number, result.Items does not contain anyting. I have also tried to decorate the definition of class article with
[PetaPoco.TableName("articles")]
[PetaPoco.PrimaryKey("article_id")]
and explicit column mapping
[PetaPoco.Column]
but result has always been the same. Is there a bug or what am I doing wrong?

Make sure that the page number starts from 1 and not 0. Its possible that in your code that you did not show here has that bug.

Related

What's the best way to go about implementing hashtag functionality in EF Core/Razor Pages?

I'm trying to implement hasthags into an application. I've configured the database already and even have a layout for how I'd like the form to look, however I'm not sure if it's possible to do what I want to do with Razor pages. I have the below classes:
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName{ get; set; }
public IList<PersonTag> PersonTags { get; set; }
}
public class Tag
{
public int TagId { get; set; }
public string TagName { get; set; }
public IList<PersonTag> PersonTags { get; set; }
}
public class PersonTag
{
public int PersonTagId { get; set; }
public int PersonId { get; set; }
public Person Person { get; set; }
public int TagId { get; set; }
public Tag Tag { get; set; }
}
And would like the form to look like this, where you see this input while editing a Person. You'd add a new hashtag by typing in the input box--hitting enter there (or pressing a button--not pictured) would create the tag in the Tag table and also create a new PersonTag relationship with the Person being edited, which would be saved on form submission. I guess the Tag could also get created on form submission if that's simpler. Clicking the "x" on one of the tags should also remove the PersonTag relationship
Is this something that's possible with razor pages? To this point I've only ever done simpler inputs (either single values or select lists), but in this case I need the user to have the option to enter free text multiple times. What kind of input would I need for these? Or would I need to do entirely special handling to pull the data on form submit, and just populate the tags with Javascript when the user adds them?

DDD shared entity between two aggregate roots

I'm working with two different aggregate roots: Post and Question. Both of them have a Category.
So far I have implemented it as a shared entity (which I'm not sure if is a correct design in DDD).
public class Post
{
public Guid Id { get; private set; }
public Category Category { get; private set; }
public string Title { get; private set; }
public string Body { get; private set; }
}
public class Question
{
public Guid Id { get; private set; }
public Category Category { get; private set; }
public string Title { get; private set; }
public string Body { get; private set; }
}
public class Category
{
public int Id { get; private set; }
public string Name { get; private set; }
public string Key { get; private set; }
}
Note: I'm aware I'm falling into primitive obsession anti-pattern, and I have plans on refactor the primitives into ValueObjects.
After read this post DDD: Share entity with multiple aggregate roots I'm thinking that maybe I should convert the Category in a ValueObject (with multiple fields).
In theory Category could be an Entity with its own lifecycle, but reality is that I don't really add/remove/update categories.
Is it possible to use a shared Entity on DDD? Or I better rather use a ValueObject?
Lets deal with one aggregate first: Post
Now to answer your question:
Is it possible to use a shared Entity on DDD? Or I better rather use a ValueObject?
It depends on what you will do with Category.
Scenario 1:
You have a feature(or page) in your application to show all posts of a category. I would go with the following design:
public class Category
{
public int Id { get; set; }
//this is my in-memory database. Use repository and service to adjust yours
public static List<Post> Posts;
public Category()
{
Posts = new List<Post>();
}
public void AddPost(Guid id, string title, string body)
{
var post = new Post(id, title, body, this.Id);
//saving the post into in-memory. Perhaps you can check some business logic inside Post entity
Posts.Add(post);
}
// You can retrieve all posts of a single category
public IEnumerable<Post> GetAllPosts()
{
return Posts.Where(x => x.CategoryId == this.Id);
}
}
public class Post
{
public Guid Id { get; private set; }
public string Title { get; private set; }
public string Body { get; private set; }
public int CategoryId { get; private set; }
public Post(Guid id)
{
Id = id;
}
public Post(Guid id, string title, string body, int categoryId)
{
//I prefer to pass guid into domain from external services.
//Using this way, your service will have the id to return to upper layers.
//Alternatively you can create new guid here on your own
Id = id;
Title = title;
Body = body;
CategoryId = categoryId;
}
// you can retrieve a post detail
public Post GetPost()
{
return Category.Posts.FirstOrDefault(x => x.Id == this.Id);
}
}
I can see only one aggregate root in this scenario: Category.
Scenario 2:
You have posts page, from there users can view detail post. Additionally, every post has a category which will be shown somewhere on that detailed page. You can have following simple design:
public class Post
{
public Guid Id { get; private set; }
public string Title { get; private set; }
public string Body { get; private set; }
public string CatKey { get; private set; }
public Post(Guid id)
{
Id = id;
}
public Post(Guid id, string title, string body, string catKey)
{
//I prefer to pass guid into domain from external services.
//Using this way, your service will have the id to return to upper layers.
//Alternatively you can create new guid here on your own
Id = id;
Title = title;
Body = body;
//I don't even bother with category id. This is a simple value object, you can store all of your categories
//into a hashtable of key-value
CatKey = catKey;
}
// you can retrieve a post detail
public Post GetPost()
{
//get your post detail from repo
}
}
Hope you can make your decision now.
The main question of Entity vs ValueObject is would two instances of the Category with the same values need to be tracked differently? The classic example is a dollar bill - in most instances, the serial number (ID) doesn't matter, and one dollar is the same as another (ValueObject). If your domain is collecting rare bills, though, that would change.
I'd suspect not in your case, since it appears Category is really just comprised of the name and key. If the Category of a Post changes, do you need to track what the Category previous was?

Is there a method to choose only some specific fields of a table using Automapper or EntityFramework?

I have a table in SqlServerDatabase. Table name is User(Id,Name,Paswd) and Im using automapper in Mvc4. Now i want only specific fields or 2 fields from the table instead of whole table, using automapper.how to do??
basically if the 2 objects have the same fields as in the little example
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Paswd { get; set; }
}
public class UserDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Paswd { get; set; }
}
You just have to ignore the field
Mapper.CreateMap<User, UserDto>()
.ForMember(o => o.Paswd, m => m.Ignore());
You can find a lot of usefull example and features here
Automapepr Wiki

Linq - How to to create a list of categories which are included in another table

I am trying to select from a list of categories where it matches the category type of a list of items using linq. IE, from a list of all the FIGstationeryCategories, select only the ones where the FiGStationeryType has a matching category from an already filtered list. The models are listed below.
public class FIGstationeryType
{
public int Id { get; set; }
public virtual FIGstationeryCategory Category { get; set; }
public virtual FIGcompany Company { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
public virtual FIGstationeryType Template { get; set; }
public bool DoubleSided { get; set; }
}
public class FIGstationeryCategory
{
public int Id { get; set; }
public string Name { get; set; }
public decimal MaxZoom { get; set; }
public ICollection<FIGstationeryType> StationeryItems { get; set; }
}
I have been going around in circles with this, and any help will be much appreciated. I haven't got very far! The first line of code works fine, it is the second one I am struggling with.
var listOfItems = db.StationeryTypes
.Where(C => C.Company.Users.Any(u => u.UserId == WebSecurity.CurrentUserId))
.ToList();
var categoryList = db.StationeryCategories
.Where(listOfItems
Any help would be much appreciated.
var listOfCategories =
(from o in listOfItems select o.Category.Name).Distinct().ToList();
When I thought about it (After watching 3 hours of linq videos last night), I realised that the listOfItems already held all the categories which where in use, so I didn't need to query and compare the two tables, just draw the relevant values from the list I already had.
I am not entirely sure how you want to select your categories but this probably goes a little way:
var categoryList = db.StationeryCategories
.*Select*(x => listOfItems.Where(y => y.Category == x)
.FirstOrDefault());
Can you clarify if this is the criteria you are after?

Using TransformResults to select hierarical data from RavenDB?

I have a simple class hierarchy looking like this:
public class Top
{
public string Id { get; set; }
public string Description { get; set; }
public List<Middle> Middles { get; set; }
}
public class Middle
{
public string Id { get; set; }
public string Description { get; set; }
public List<Bottom> Bottoms { get; set; }
}
public class Bottom
{
public string Id { get; set; }
public string Description { get; set; }
}
The whole thing is saved as entity of type 'Top'. Document is designed to preserve and reflect relationships/hierarchy but half but at time I will, for example, care only about an 'Id' and 'Description' of a given relationship. So, the types of queries I'd want to run are
select all Top,
select all Middle,
select Middle where Top.Id=somevalue
select Bottom where Top.Id=somevalue and Middle.Id=somevalue
I would like the results to be transformed and returned to me like this:
public class Result
{
public int Id { get; set; }
public string Description { get; set; }
}
How can I implement TransformResults (I presume that that's the feature that can be used) to achieve this? I've read quite a few examples but all of the sudden I see parameters/values, which were not declared anywhere and as a result I don't understand what's happening.
TransformResults doesn't have access to the outside world, you can't execute logic based on the query that you run.
You can flatten this structure, sure, but unless you will create multiple indexes with different TransformResults, you can't do this.
Note that this is a strange thing to do in the first place, because it doesn't matches the standard modeling of documents as a transaction boundary.