AsSelf在autofac中做了什么?

时间:2017-01-09 06:21:58

标签: c# dependency-injection inversion-of-control autofac

autofac中的AsSelf()是什么? 我是autofac的新手,究竟是什么AsSelf以及下面两者之间有什么区别?

builder.RegisterType<SomeType>().AsSelf().As<IService>();
builder.RegisterType<SomeType>().As<IService>();

谢谢!

1 个答案:

答案 0 :(得分:28)

通常,您希望将接口而不是实现注入到类中。

但是,假设你有:

interface IFooService { }

class FooService { }

注册builder.RegisterType<FooService>()可以注入FooService,但即使IFooService实施FooService,您也无法注入builder.RegisterType<FooService>().AsSelf()。这相当于builder.RegisterType<FooService>().As<IFooService>()

注册IFooService可让您注入FooService,而不是.As<T> - 使用.AsSelf()&#34;覆盖&#34;默认注册&#34;按类型&#34;如上所示。

为了能够按类型和界面注入服务,您应该将builder.RegisterType<FooService>().As<IFooService>().AsSelf()添加到之前的注册:builder.RegisterType<SomeType>().AsImplementedInterfaces()

如果您的服务实现了许多接口并且您想要全部注册它们,则可以使用/** * This example illustrates how to add/update EXIF metadata in a JPEG file. * * @param jpegImageFile * A source image file. * @param dst * The output file. * @throws IOException * @throws ImageReadException * @throws ImageWriteException */ public void changeExifMetadata(final File jpegImageFile, final File dst) throws IOException, ImageReadException, ImageWriteException { try (FileOutputStream fos = new FileOutputStream(dst); OutputStream os = new BufferedOutputStream(fos);) { TiffOutputSet outputSet = null; // note that metadata might be null if no metadata is found. final ImageMetadata metadata = Imaging.getMetadata(jpegImageFile); final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; if (null != jpegMetadata) { // note that exif might be null if no Exif metadata is found. final TiffImageMetadata exif = jpegMetadata.getExif(); if (null != exif) { // TiffImageMetadata class is immutable (read-only). // TiffOutputSet class represents the Exif data to write. // // Usually, we want to update existing Exif metadata by // changing // the values of a few fields, or adding a field. // In these cases, it is easiest to use getOutputSet() to // start with a "copy" of the fields read from the image. outputSet = exif.getOutputSet(); } } // if file does not contain any exif metadata, we create an empty // set of exif metadata. Otherwise, we keep all of the other // existing tags. if (null == outputSet) { outputSet = new TiffOutputSet(); } { // Example of how to add a field/tag to the output set. // // Note that you should first remove the field/tag if it already // exists in this directory, or you may end up with duplicate // tags. See above. // // Certain fields/tags are expected in certain Exif directories; // Others can occur in more than one directory (and often have a // different meaning in different directories). // // TagInfo constants often contain a description of what // directories are associated with a given tag. // final TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory(); // make sure to remove old value if present (this method will // not fail if the tag does not exist). exifDirectory.removeField(ExifTagConstants.EXIF_TAG_APERTURE_VALUE); exifDirectory.add(ExifTagConstants.EXIF_TAG_APERTURE_VALUE, new RationalNumber(3, 10)); } { // Example of how to add/update GPS info to output set. // New York City final double longitude = -74.0; // 74 degrees W (in Degrees East) final double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees // North) outputSet.setGPSInDegrees(longitude, latitude); } // printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME); new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet); } } - 这允许您通过它实现的任何接口来解析您的服务。

您必须明确注册,因为Autofac不会自动执行此操作(因为在某些情况下您可能不想注册某些接口)。

here in Autofac documentation

中也对此进行了描述