# ServiceType and abstract? classes

**URL:** <https://forum.litium.com/t/servicetype-and-abstract-classes/1055>\
**Category:** Questions\
**Tags:** litium-7\
**Created:** [August 28, 2019, 11:31am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055 "2019-08-28T11:31:42Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [August 28, 2019, 11:31am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/1 "2019-08-28T11:31:42Z")

</div>

I’m registering some ServiceTypes and want to be able to have a default implementation of the service, with a possibility to override entire class or specific methods.

Tried an abstract class, but this cannot be registered.

Tried an ‘default’ implementation of the abstract class, but can only get that implementation, no other class that implements the same interface.

Tried an normal class with virtual methods, but can only get that implementation, no other class that implements the same interface.

Any ideas?

Litium version: 7.2.3

---

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [August 28, 2019, 11:32am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/2 "2019-08-28T11:32:41Z")

</div>

```
[Service(ServiceType = ...)]
public interface ....

public class ...Impl
```

---

<div class="post-metadata">

**Author:** ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)\
**Post date:** [August 28, 2019, 11:36am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/3 "2019-08-28T11:36:01Z")

</div>

Did you check this documentation?  
[https://docs.litium.com/documentation/architecture/dependency-injection](https://docs.litium.com/documentation/architecture/dependency-injection)

---

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [August 28, 2019, 11:41am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/4 "2019-08-28T11:41:01Z")

</div>

Yes! This doesn’t help. The service decorator is close to what we want but not completely.

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [August 28, 2019, 11:54am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/5 "2019-08-28T11:54:40Z")

</div>

The abstract class or the interface should be the service type

```auto
[Service(ServiceType = typeof(A))]
public abstract class A {}

```

Create an implementation that inherit the abstract class or interface. This class will automatic be found during application initialization.

```auto
public class AImpl : A {}

```

To let another implementation be used before the `AImpl` you need to add the service attribute on the default implementation and set the `FallbackService = true`, that will tell the registration process that the implementation should be used if no other implementations is found.

```auto
[Service(FallbackService = true)]
public class AImpl : A {}

public class ASuperImpl : A {}

```

If you build an extension and have a contract that need to be implemented in the project you can add the `RequireServiceImplementationAttribute` on the service registration and if no registered service is found the application will not start.

```auto
[Service(ServiceType = typeof(A))]
[RequireServiceImplementation]
public abstract class A {}

```

---

<div class="post-metadata">

**Author:** ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)\
**Post date:** [August 28, 2019, 12:16pm UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/6 "2019-08-28T12:16:29Z")

</div>

What happens if you have  
`[Service(FallbackService = true)]`  
on 2 or more classes?

---

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [August 28, 2019, 12:16pm UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/7 "2019-08-28T12:16:52Z")

</div>

Do this work between namespaces/assemblies as well?

I’ve done what you suggest. But I end up with the default implementation anyway.

---

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [August 28, 2019, 12:20pm UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/8 "2019-08-28T12:20:53Z")

</div>

```
namespace x.y.z (same assembly)
[Service(ServiceType = typeof(EntityTypeService), Lifetime = DependencyLifetime.Transient)]
public abstract class EntityTypeService
{
    public abstract EntityType Get(ImportEntity importEntity);
}

namespace x.y.x (same assembly)
[Service(FallbackService = true)]
internal class EntityTypeServiceImpl : EntityTypeService
{
    public override EntityType Get(ImportEntity importEntity)
    {
        throw new System.NotImplementedException();
    }
}

namespace a.b.c (other assembly)
public class CustomEntityService : EntityTypeService
{
    public override EntityType Get(ImportEntity importEntity)
    {
        throw new System.NotImplementedException();
    }
}
```

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [August 28, 2019, 12:31pm UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/9 "2019-08-28T12:31:11Z")

</div>

Yes, it works between assemblies and namespaces, that is used in the product.

---

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [August 28, 2019, 12:32pm UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/10 "2019-08-28T12:32:09Z")

</div>

Then why do I get the default implementation still?.. Tried both interface and abstract class.

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [August 28, 2019, 3:11pm UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/11 "2019-08-28T15:11:14Z")

</div>

You haven’t the assembly registered as [ignore for plugins](https://docs.litium.com/documentation/get-started/web_config#plugins)? This setting can also be set with the assembly attribute `AssemblyLoading`.

Another possibility is that you missed one assembly redirects in the configuration file, this is needed when different components using the same component with different version numbers.

---

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [August 30, 2019, 9:57am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/12 "2019-08-30T09:57:34Z")

</div>

No.

It’s a standard Accelerator with an additional project in the solution.

Assembly redirect should not be required?

---

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [September 2, 2019, 7:54am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/13 "2019-09-02T07:54:15Z")

</div>

@patric.forsgard do you have any other ideas?

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [September 2, 2019, 2:59pm UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/14 "2019-09-02T14:59:07Z")

</div>

No, I haven’t. Is it possible for you to create a minimal project that you can PM me a link to so I can check how everything is setup for you?

---

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [September 3, 2019, 8:51am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/15 "2019-09-03T08:51:53Z")

</div>

Skype? Teams? VS Live Share?

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [September 4, 2019, 10:58am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/16 "2019-09-04T10:58:39Z")

</div>

Include the link to the downloadable code in a direct message here in the forum and I will download and check that code.

---

<div class="post-metadata">

**Author:** ![carl.sjoholm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/carl.sjoholm/32/17_2.png) [@carl.sjoholm](https://forum.litium.com/u/carl.sjoholm)\
**Post date:** [September 11, 2019, 6:07am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/17 "2019-09-11T06:07:43Z")

</div>

The solution was to have an interface or abstract class as base for the service. Then use the

```
[Service(ServiceType = typeof(Interface/Abstract), Fallback = true, Lifetime = DependencyLifetime.Transient)]

```

On the implementing type and not the interface or the abstract. E.g.:

```
namespace x.y.z {
    public interface IServiceImplementation { .. methods }
}

namespace x.y.z {
      [Service(ServiceType = typeof(IServiceImplementation ), Fallback = true, Lifetime = DependencyLifetime.Transient)]
      public class ServiceImplementation : IServiceImplementation { ... methods }
}

```

Maybe I missread your directons somehow? anyway maybe write this down in the documentation.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [October 9, 2019, 6:07am UTC](https://forum.litium.com/t/servicetype-and-abstract-classes/1055/18 "2019-10-09T06:07:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
