Business logic layer, multiple tables, relationship - asp.net

In the business logic we map, Tables to Objects and fields of this table to properties.
What to do in one-to many relationship? Just an example: I have Table, Products and Categories.
I need to drag all products and instead of Category_ID (Products table) need to display actual Category name wich is stored in Category table.
What is propal way of doing it? In similar situations?

You can have a property on the Products Entity that is of type Category Entity. Check this link out from the EntitySpaces (an ORM for .Net) documentation for ideas:
link text
Also, you may want to look into using an ORM.

Either use an ORM like LINQ to SQL or ADO.NET Entity Framework or strongly-typed datasets. But if you want to custom code your business layer, your Category class could have a Products collection property which contains those products, loaded from the DB, which you could infer the name.
HTH.

Related

Symfony2 - extending existing non-abstract entity?

Let's say I have a Setting entity with some fields like IntValue, dateValue, stringValue and some linked entities, like countries (ManyToMany to entity Country), languages (ManyToMany to Language) etc.
Settings are created by users and assigned to specific objects (not important here, but I wanted to clarify).
Now I suddenly need to have UserDefaultSetting, which will be the same, but with additional user field (ManyToOne to User entity).
I tried to extend existing Setting entity class with one more field added. The problem is, as I looked at the schema update SQL, it created new table for the new entity, but without all the tables needed to ORM connections (mostly ManyToMany). Just one table with "scalar" fields.
So previously I've had setting table with int_value, date_value etc. but also setting_country and setting_language tables, linking ManyToMany relations. After creating child entity, Doctrine created only user_default_setting table with int_value, date_value etc. and additionally user_id column, but I can't see any relation/link tables.
I know I should've been do it with abstract base entity class, but at the time I started, I didn't know that and now part of the project is on production (don't look at me like that, I blame the client) and I don't want to change that "base" class now. Can I inherit everything from non-abstract entity class in a way it will work?
UPDATE: everything explained. See Cerad's comment. Thanks!

Comment entity related to Article or Product

I have entities "Article" and "Product". Now I want to add comments to these 2 entities. Should I create 2 different entities "ArticleComment" and "ProductComment" with the same properties, and build a ManyToOne relation to their respective entity, or create a single "Comment" entity and find a way to build a relation to both "Article" and "Product" entities. Considering solution #2, how could I do that ?
Considering solution #2, how could I do that ?
One way would be to use Single Table Inheritance
Single Table Inheritance is an inheritance mapping strategy where all classes of a hierarchy are mapped to a single database table. In order to distinguish which row represents which type in the hierarchy a so-called discriminator column is used.
This means that you can easily create two separate entities ArticleComment and ProductComment both extending Comment. Then you use the advantages DiscriminatorMap column provides.
Your Comment entity could hold a relation called parent for instance that would refer to either your Article or Product entities. By creating new instance of ArticleComment or ProductComment your discriminator map field would be automatically populated depending on which type you're using.
This would also give you advantages with using DQL to query related comments by their type. Example from documentation:
$query = $em->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee');
And more. You can read that chapter here. Of course this is just a sample and you can use completely different approach.

Symfony 2: Howto Map 2 way multiple Entities with one FormType

Suppose i have an Bundle to manage anything related to advertisements.
This bundle contains an Entity Advertisement. this has an field for relation purposes: lets say relation field
Suppose i have an Entity Company and an Entity Events in different Bundles
(In companies there are companies stored and in events there are events stored.)
Case:
The entities have a relation to multiple Advertisements.
A single Advertisement has a relation to only one of the entities.
From the perspective advertisement:
I want to be able to select one of the entities (entity.id) to view or update the reference (like dropdown)
From the perspective of an event or a company:
I want to be able to select/add/delete multiple advertisements (like the symfony collection form type)
all this preferred without the use of foreign-keys.
the entities are like "modules" so there can be more than just these entities.
I think you just have to use OneToOne (For Advertisement) and OneToMany (For Company and Events) relations.
It's easy to use, read this doc : http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html

configure entitydatasource with entity set that includes multiple tables

I have an entity data model with two entities. One entity called Posts contains blog posts and another entity called Comment contains comments. I set up the association as a one to many relationship and the primary keys are both postid in both Posts and Comment.
However, when I try to bind this to an entitydatasource control it only lets me select either the Posts or the Comment entity set not both. What I want to do is to be able to bind the post with its comments to the data source, but I can't seem to figure out a way to do this.
Any help would be appreciated!

Need Help on entity framework

I have 3 tables(Roles,Actions and RoleActionLinks). Roles table has few columns(RoleID,RoleName,Desc). Actions table has few colums(ActionID,ActionName,Desc). In RoleActionLink is created for store the association between Roles and Actions and this table has the columns such as RoleID,ActionID
When I created the data model(edmx). it shows only Role and Action as entity. i did not find RoleActionLink table. but even there is no direct relation between Roles and Actions table, both tables are automatically related using RoleActionLink table.
When i create the new Action, a action record should be populated in Action table(this is works fine). At the same time, i need to populate record in RoleActionLinks table. But i dont have the entity to populate.
Please tell me how to accomplish my needs.
This should work:
newAction.Roles.Add(role1);
newAction.Roles.Add(role2);
Look at navigation properties in your model. There should be EntityCollection called Roles (name may differ).
Entity framework automatically handles n-n tables and creates collections on both sides.

Resources