简体   繁体   中英

Spring .NET Inheritance implementation

I am getting exception Multiple objects found for type: A --> EQUITY_SWAP_EQUITY_RESET_VIEWMODEL,EQUITY_SWAP_INTEREST_RATE_RESET_VIEWMODEL. No objects will be resolved. Instance of derived object is created without any issues. The base object is throwing the above exception. I have tried removing the parent property from the derived object definition and I'm still getting same exception. How can I fix that?

public class A
{
   public A(string name, int age, string gridType)
   {
   }
}

public class B: A
{
   public B(string name, int age, string gridType) : base(name, age, gridType)
   {
   }
}

<object id="EQUITY_SWAP_EQUITY_RESET_VIEWMODEL" singleton="false" type="A">
  <constructor-arg index="0" value="David" />
  <constructor-arg index="1" value="10" />
  <constructor-arg index="2" value="modern" />
</object>
<object id="EQUITY_SWAP_INTEREST_RATE_RESET_VIEWMODEL" singleton="false" parent="EQUITY_SWAP_EQUITY_RESET_VIEWMODEL" type="B">
  <constructor-arg index="0" value="Suresh" />
  <constructor-arg index="1" value="12" />
  <constructor-arg index="2" value="old fashioned" />
</object>

On Spring.Net the object definition is not a class declaration, it's a way to send to Spring.Net informations about how they will instantiate and initialize dependencies of your classes.

First: the "parent" attribute defined on OBJECT is about inheritance of configurations. It's helpful to create a base configuration for the same injections, like logging, data access and many other ways to use that.

<object id="EQUITY_SWAP_EQUITY_RESET_VIEWMODEL" singleton="false" type="ConsoleApplication1.A, ConsoleApplication1">
    <constructor-arg index="0" value="David" />
    <constructor-arg index="1"  value="10" />
    <constructor-arg index="2" value="modern" />
</object>
<object id="EQUITY_SWAP_INTEREST_RATE_RESET_VIEWMODEL" singleton="false" type="ConsoleApplication1.B, ConsoleApplication1">
    <constructor-arg index="0" value="Suresh" />
    <constructor-arg index="1" value="12" />
    <constructor-arg index="2" value="old fashioned" />
</object>

On github I've created a branch to explain that with your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM