C# Unity中的Singleton模式与ContainerControl LifeTImeManager
数据库中的更改 1-从iC_ProductFeature表中删除了列维度 2-添加新表iC_ProductDimensions以保存产品的不同维度数据。以下是iC_ProductDimensions中使用的不同列的详细说明C# Unity中的Singleton模式与ContainerControl LifeTImeManager,c#,.net,sql,dependency-injection,unity-container,C#,.net,Sql,Dependency Injection,Unity Container,数据库中的更改 1-从iC_ProductFeature表中删除了列维度 2-添加新表iC_ProductDimensions以保存产品的不同维度数据。以下是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
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-在iC_ProductUnitOfMeasure中添加一列“MeasurementType”
4-将ProductFeatureId的数据类型从varchar(100)更改为Bigint
数据流
iC_ProductUnitOfMeasure中的“MeasurementType”将存储为其创建的测量单位
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.
在上面的示例中,我们用其关联度量类型标记了每个计量单位
计量单位1可用于以磅为单位测量产品重量,类似地,计量单位4可用于以千克为单位测量产品重量
添加产品长度为0.5米、产品宽度为50英寸、产品重量为100磅的产品
1-在iC_产品表中创建一条记录,以存储产品的公共属性。(Suppopse ProductId=ICPR0001)
2-在iC_ProductFeature中创建一个记录,并存储常见的产品特征,如颜色、尺寸、s/w、h/w特征(如果有)。(假设ProductFeatureId=1)
3-在iC_ProductDimension表中创建3条记录,如下所示
DimensionID UOMID ProductFeatureId NumericValue
1 1 1 100
2 2 1 50
3 3 1 0.5
4-使用(ProductId=ICPR0001和ProductFeatureId=1)在iC_ProductFeature中添加记录
查询数据以获得产品ICPR001的不同尺寸值
select
product .* ,
productFeature.* ,
( CAST (dimension.NumericValue as Varchar(100) )+" " + UOM.Abbrivation) as Dimension ,
dimension.MeasurementType
from
iC_Product product ,
iC_ProductFeature productFeature ,
iC_ProductFeatureApplicability ,
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'
单例模式就是模式本身——Unity只是在几个地方(包括大多数生命周期管理器)实现了该模式 根据定义,单例不能被模拟——它们被设计为只有一个实例。如果使用单例的代码是通过接口实现的,那么可以模拟类型来实现该接口,但不能模拟单例本身
至于单身模式是否是反模式,我不认为是。我认为它经常被过度使用,往往被用来代替更好的解决方案。不过,我不认为这是对模式本身的一个标记。真正的单例只能实例化一次(在某些实现中,每个线程实例化一次) Unity生命周期管理只是让Unity始终返回相同实例的一个选项;该实例实际上不太可能被设计为单实例,因此原则上,您仍然可以手动创建其他实例
有关区别的更多信息,请参见我对的回答。“Unity只是在几个地方(包括大多数终身管理者)实现了这种模式。”我不同意这种说法。由于可以有多个注入器,因此可以在应用程序中获得一个类的多个实例。