开发者

Database, DataAccess And Business Layers Design When I Have a Common Entity

开发者 https://www.devze.com 2023-04-05 07:02 出处:网络
I have a website that will contain a lot of types of businesses, each business has it\'s own layers, but now I have a common entity that\'s called Brand.

I have a website that will contain a lot of types of businesses, each business has it's own layers, but now I have a common entity that's called Brand.

Should I have table for each business brand info, like:

DB Table
Business1_BrandsInfo
....................

DB Table
Business2_BrandsInfo
开发者_如何学Python...etc ?

and of course I'll have a seperate business entity for each Brand info for each business.

Or should I have one common table and add an id for each business to define to which business does this brand belongs.

The several business is not related but I have the same typical info for each brand regardless the type of the business, and I have like 5 different types of business on my site.


simple : if it has the same properties for each table so you can put it in 1 table and recognize them by TypeId column.

if you have more than 1 type then you'll need also more than 1 entities to content these tables.

the type for recognition will be also in the class.

please be aware that if tomorrow you will add a specific column to type "1" then you will have a problem...


I assume by common entity you mean an object that is shared amongst all businesses in your website. If this is the case your database schema should reflect a single table for all Brands, but it should have a BusinessID column with a foreign key to its associated business.

[dbo].[Brands]
ID
BusinessID → [dbo].[Businesses].[ID]
...

[dbo].[Businesses]
ID ← [dbo].[Brands].[BusinessID]
...

You could add as many businesses and brands as you want, but later on though if you want custom properties that differ from business to business or brand to brand, then that schema would have to change, or you would have to devise some other method for those custom fields.


I would split these out. Here are two options below. You don't need to repeat tables that will have the same type of data.

Option 1

Brands
{
  ID,
  BusinessID,   (FK to Business.ID)
  Info1,
  Info2
}

Business
{
  ID,
  BusinessName,
  BusinessDesc,
  OtherStuff1,
  OtherStuff2
}

Option 2

Brands
{
  ID,
  Info1,
  Info2
}

Business
{
  ID,
  BusinessName,
  BusinessDesc,
  OtherStuff1,
  OtherStuff2
}

BusinessToBrands
{
  ID,
  BrandsID(FK to Brands),
  BusinessID(FK to Business)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号