.NET Core Include Method in Dependency Injection

52 views Asked by At

I'm not sure if I am asking this question correctly, so please bear with me as this is all very new to me.

I am trying to fully grasp dependency injection in .NET Core while also trying to understand proper design patterns. I found a great tutorial (the teacher is excellent and explains things well), but the problem is that the sample application in the tutorial is being built using Visual Studio 2010 using Unity for Dependency Injection.

I'm trying to convert the DI into my app in VS 2022, but I am not understanding how to include the injection of the methods in the services section of .NET Core.

The example using Unity looks like this:

public static ICustomer Create(string TypeCust)
{
     if (custs == null)
     {
         custs = new UnityContainer();
         custs.RegisterType<ICustomer,Customer>("Customer", new InjectionConstructor(new CustomerValidationAll()));
         custs.RegisterType<ICustomer, Lead.("Lead", new InjectionConstructor(new LeadValidation()));
    }

    return custs.Resolve<ICustomer>(TypeCust);
}

I know how to include the interfaces and the concrete classes in .NET Core:

services.AddScoped<ICustomer, Customer>();
services.AddScoped<ICustomer, Lead>();

As requested, here is the rest of the code:

public interface ICustomer
{
    string CustomerName { get; set; }
    string PhoneNumber { get; set; }
    decimal BillAmount { get; set; }
    public DateTime BillDate { get; set; }
    string Address { get; set; }

    void Validation();
}

public interface IValidation<Any>
{
    void Validate(Any obj);
}

public class CustomerValidationAll : IValidation<ICustomer>
{
    public void Validate(ICustomer obj) 
    {
        if (obj.CustomerName?.Length == 0)
        {
            throw new Exception("Customer Name is required");
        }

        if (obj.PhoneNumber?.Length == 0)
        {
            throw new Exception("Phone Number is required");
        }

        if (obj.BillAmount == 0)
        {
            throw new Exception("Bill Amount is required");
        }

        if (obj.BillDate >= DateTime.Now)
        {
            throw new Exception("Bill date is incorrect");
        }

        if (obj.Address?.Length == 0)
        {
            throw new Exception("Address is required");
        }
    }
}

public class LeadValidation : IValidation<ICustomer>
{
    public void Validate(ICustomer obj)
    {
        if (obj.CustomerName?.Length == 0)
        {
            throw new Exception("Customer Name is required");
        }

        if (obj.PhoneNumber?.Length == 0)
        {
            throw new Exception("Phone Number is required");
        }
    }
}

What I can't figure out is how to include the validation methods CustomerValidationAll() and LeadValidation() that are in the InjectionContstructor in the above sample.

Can someone please help me?

1

There are 1 answers

0
Khanh Nguyen On

Here are some ways you can achieve this, basically, you can create a ServiceResolver or a factory to retrieve your object with a key, or use anti pattern.

You can see more info here: https://andrewlock.net/exploring-the-dotnet-8-preview-keyed-services-dependency-injection-support/