开发者

Singleton Pattern vs. ContainerControlled LifeTImeManager in Unity

Changes in database

1- Removed column dimesion from the iC_ProductFeature table

2 - Added new table iC_ProductDimensions to hold the different dimensional data of the product. below is the detailed description of different columns used in the iC_ProductDimensions

1- DimensionId - This is the primary key in the table and of bigint datatype.

2- UOMID -  (FK) Unit of Measurement ID , this is refrenced from the table iC_ProductUnitOfMeasure.

3- ProductFeatureId : This is referenced column from the iC_ProductFeature table.

4- NumericValue  : This column stores the dimensional value (e.g. if UOM is pound and this column stores 10 than we can say that Weight of product is 10 pound.

3- One column 'MeasurementType' is added in the iC_ProductUnitOfMeasure.

4- Changed the datatype of ProductFeatureId to Bigint from varchar(100)

Data Flow

'MeasurementType' in the iC_ProductUnitOfMeasure will store the measurement for which unit of mesaure is created.

e.g. let's take a snapshot view of  rows in iC_ProductUnitOfMeasure 


    UOMID  Abbrivation         MeasurementType         Description 

    1              lb          Weight           This UOM is used for measuring weight of the product 
    2              inch.       Width            This UOM is used for measuring width of the product 
    3              meter       Length           This UOM is used for measuring length of the product 
    4              Kg          Weight           This UOM is used for measuring weight  of product  in Kg.

in the above example, we have tagged each UOM with its associative MeasurementType.

UOM 1 can be used measure weight of products in pound similarly UOM 4 can be used to measure the product weight in kilogrames.

Adding a product which have product length is 0.5 meter , product width is 50 inch and product weight as 100 pound.

1 - Create a record in the iC_Product table to store the common attributes of the product. (Suppopse ProductId = ICPR0001)

2- Create a record in iC_ProductFeature and store the common product feature like color , size , s/w , h/w features if any. (Suppose ProductFeatureId = 1)

3- Create 3 records in iC_ProductDimesion table as shown below.

DimensionID UOMID ProductFeatureId NumericValue 
1           1     1                100 
2           2     1                50 
3           3     1                0.5 

4- Add a record in the iC_ProductFeatureApplicability with (ProductId = ICPR0001 and ProductFeatureId = 1)

Quering data to get the different dimensional values of the product ICPR001

select 

          product .* , 
          productFeature.* ,
          ( CAST (dimension.NumericValue  as Varchar(100) )+" " + UOM.Abbrivation) as Dimension ,
          dimension.MeasurementType

         from 
                iC_Product product , 
                iC_ProductFeature productFeature , 
                iC_ProductFeatureApplicabi开发者_运维知识库lity , 
                iC_ProductDimesions dimension , 
                iC_ProductUnitOfMeasure UOM

where

 iC_ProductFeatureApplicability.ProductId = product.ProductId  

and 

 iC_ProductFeatureApplicability.ProductFeatureId = productFeature.ProductFeatureId 

and 

dimension.ProductFeatureId = productFeature.ProductFeatureId 

and 

dimension.UOMID= UOM.UOMID

and 

 product.ProductId = 'ICPR001'        


A true singleton can only ever be instantiated once (per thread, in some implementations).

The Unity lifetime management is just an option to have Unity always return the same instance; that instance is unlikely to actually be designed as a singleton, so you could still, in principle, create other instances manually.

See my answer to this question for more about the distinction.


The singleton pattern is the pattern itself - Unity simply implements that pattern in several places (including most of the lifetime managers).

Singletons by definition cannot be mocked - they are designed to have only one instance. If your code that uses a singleton does by way of an interface then it is possible to mock a type to implement that interface but you cannot mock the singleton itself.

As for whether or not the singleton pattern is an anti-pattern I don't believe it is. I think that it is often overused and tends to be implemented in place of better solutions. I don't think that is a mark against the pattern itself, though.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜