هل هناك طريقة أفضل لاستخدام واجهة برمجة تطبيقات Castle Windsor للمصانع؟

StackOverflow https://stackoverflow.com/questions/2367016

سؤال

أنا منفتح على حاويات IOC الأخرى ، مثل Ninject و structuremap إذا كانت أكثر نظافة من هذا. أسمع أن هيكستوريماب قدمت للتو "حاويات" قد تبسط هذا ، ربما؟

كما يقول العنوان ، هل هناك طريقة أفضل؟ هذا يبدو وكأنه الكثير من التعليمات البرمجية ، فقط لتسجيل كائن يتطلب مصنع لإنشائه.

// The process to register an object, with a factory method
var cfg = new MutableConfiguration(p.Name);
cfg.Attributes["factoryId"] = p.TypeFactory.Name;
cfg.Attributes["factoryCreate"] = "Create";
var model = _container.Kernel.ComponentModelBuilder.BuildModel(
    p.Name, p.TypeService, p.Type, null);
model.LifestyleType = LifestyleType.Pooled;
model.Configuration = cfg;
_container.Kernel.AddCustomComponent(model);

فيرساس طريقة "غير الحارس" لإضافة مكون:

// registering a component with no factory method
_container.AddComponentLifeStyle(
    p.Name, p.TypeService, p.Type, LifestyleType.Singleton);

الأول يبدو معقدًا للغاية.

شكرا لك مقدما!

هل كانت مفيدة؟

المحلول

لست متأكدًا مما تحاول التسجيل (ماذا p في كتلة الكود الأول؟) ولكن مع UsingFactoryMethod, ، تسجيل المصنع هو نسيم. عينة من الرموز:

container.AddFacility<FactorySupportFacility>()
   .Register(
      Component.For<IMyService>()
         .UsingFactoryMethod(() => MyLegacyServiceFactory.CreateMyService())
         .LifeStyle.Pooled
   );
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top