How to access class properties through an Interface instance using Unity.WebApi - c#

Is it possible to expose class public properties in different class through IOC. I am creating an instance of Interface but i am not able to access public properties of class. I am using Unity.WebApi for resolving dependencies.
TransactionService Class
public class TransactionService : ITransactionService
{
private readonly IMRepository _mRepository;
private readonly IFService _fGateway;
public TransactionService(IMbaRepository mbaRepository, IFpnService fpnService)
{
_mRepository = mRepository;
_fGateway = fService;
}
private List<Transaction> SearchTransacionsByUser(FUser objFUser)
{
foreach (var item in something)
{
//can't use _fGateway to set properties because Interface
// don't implement them
_fGateway.OID = objFUser.OID.ToString();
_fGateway.Amount = objFUser.Amount;
_fGateway.Search(criteria);
}
}
}
FService class
public class FService : IFpService
{
public string _OID { get; set; }
public decimal _Amount{ get; set; }
public TransactionResponse Search(string criteria)
{
TransactionOperationInput _input;
_input = new TransactionOperationInput()
{
Criteria = _criteria,
OID = _OID,
Amount = _Amount
};
// search transactions
}
}

If you are in control of the services then refactor the interfaces to expose the desired members
public interface IFService {
TransactionResponse Search(TransactionOperationInput input);
}
Make sure the derived implementation has those members
public class FService : IFpService {
public TransactionResponse Search(TransactionOperationInput input) {
// search transactions
}
}
And that the dependent class uses the correct abstraction
public class TransactionService : ITransactionService {
private readonly IMRepository _mRepository;
private readonly IFService fGateway;
public TransactionService(IMbaRepository mbaRepository, IFService fService) {
_mRepository = mRepository;
fGateway = fService;
}
private List<Transaction> SearchTransacionsByUser(FUser objFUser) {
foreach (var item in something) {
TransactionOperationInput input = new TransactionOperationInput() {
Criteria = _criteria,
OID = objFUser.OID.ToString(),
Amount = objFUser.Amount,
};
fGateway.Search(input);
//...
}
//...
}
}
Finally make sure the register the appropriate abstractions and implementations with the IoC/DI container.

Related

.NET - It is possible to use DI with the State Pattern?

I'm studying design patterns in .NET and currently i'm trying to implement the State Pattern. But today i got to a problem and i can't figure out how to fix this situation.
I have some state classes, all of them implementing the state interface. One of the last states should connect to a data base, through a service injected by the .NET API Startup class, to persist the data and finish up the process.
The problem is... because of the dependency injection that i need to be in the final state, i can't instanciate this state object to progress to this point. I don't know how to continue from there. I don't know if i'm using the pattern wrong or if the use of dependency injection in this pattern is the problem. I can't give all the details of the problem because my studie's project is a little big mess at this moment, so i made a quick mimic of the structure i'm trying to build in my application.
States interface and the OperatingClass who will execute the state behaviour:
public interface IOperationState
{
public int ExecuteOperation(OperatingClass operatingClass);
}
public class OperatingClass
{
public IOperationState OperationState { get; set; }
public int id { get; set; }
public double value { get; set; }
public OperatingClass(int id) //constructor
{
this.id = id;
value = 0;
OperationState = new StartingState();
}
public int Execute()
{
return OperationState.ExecuteOperation(this);
}
}
Main Service: is the service who my controller calls after receive the API Post Method:
public class MainService
{
public int ExecuteFullOperation(int id)
{
//Receives an id and execute the state transition till the end;
var operatingClass = new OperatingClass(id);
return operatingClass.Execute();
}
}
The classes who represents the states and do the respective actions:
public class StartingState : IOperationState
{
public int ExecuteOperation(OperatingClass operatingClass)
{
// Do something...
operatingClass.OperationState = new MiddleState();
return operatingClass.Execute();
}
}
public class MiddleState : IOperationState
{
public int ExecuteOperation(OperatingClass operatingClass)
{
//Do something with the value... let's supose the result is 123, but it does not matter rn;
operatingClass.value = 123;
//Here is the problem: FinalState needs the PersistenceService, who
//receives a injected class to acess the database;
operatingClass.OperationState = new FinalState();
//I want to execute it and return the sucess or failure of the persistence.
return operatingClass.Execute();
}
}
public class FinalState : IOperationState
{
private readonly IPersistenceService PersistenceService;
public FinalState(IPersistenceService persistenceService)
{
PersistenceService = persistenceService;
}
public int ExecuteOperation(OperatingClass operatingClass)
{
return PersistenceService.PersistData(operatingClass.id, operatingClass.value) ? 200 : 503;
}
}
Additional info: i made the PersistenceService be injected in the Startup.cs as a Transient (i dont know how to make it in another way at this moment).
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IPersistenceService, PersistenceService>();
// Irrelevant configurations for the question.
services.AddControllers();
}
Please, help me if you can. I'm having a hard time trying to figure it out by myself.
Thank you for your patience and for your time reading it.
Firstly, we need some simple factory which will supply all necessary dependencies by their type. So let's create types for states:
public enum StateType
{
Start,
Middle,
Final
}
And simple factory:
public class StateFactory
{
private Dictionary<StateType, IOperationState> _stateByType;
// you can inject these dependencies through DI like that:
// public StateFactory(StartingState startingState,
// MiddleState middleState, FinalState finalState,
// PersistenceService persistenceService)
public StateFactory()
{
_stateByType = new Dictionary<StateType, IOperationState>()
{
{ StateType.Start, new StartingState(this) },
{ StateType.Middle, new MiddleState(this) },
{ StateType.Final, new FinalState(new PersistenceService()) }
};
}
public IOperationState GetByType(StateType stateType) =>
_stateByType[stateType];
}
Then we should register all our dependencies:
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IPersistenceService, PersistenceService>();
services.AddTransient<StartingState>();
services.AddTransient<MiddleState>();
services.AddTransient<FinalState>();
services.AddTransient<MainService>();
services.AddTransient<OperatingClass>();
services.AddTransient<PersistenceService>();
services.AddTransient<StateFactory>();
}
Our states would look like this:
public class StartingState : IOperationState
{
private StateFactory _factory;
public StartingState(StateFactory stateFactory)
{
_factory = stateFactory;
}
public int ExecuteOperation(OperatingClass operatingClass)
{
// Do something...
// operatingClass.OperationState = new MiddleState();
operatingClass.OperationState = _factory.GetByType(StateType.Middle);
return operatingClass.Execute();
}
}
And MiddleState would look like this:
public class MiddleState : IOperationState
{
private StateFactory _factory;
public MiddleState(StateFactory stateFactory)
{
_factory = stateFactory;
}
public int ExecuteOperation(OperatingClass operatingClass)
{
//Do something with the value... let's supose the result is 123,
// but it does not matter rn;
operatingClass.value = 123;
//Here is the problem: FinalState needs the PersistenceService, who
//receives a injected class to acess the database;
operatingClass.OperationState = _factory.GetByType(StateType.Final);
//I want to execute it and return the sucess or failure of the persistence.
return operatingClass.Execute();
}
}
And Final state should look like this:
public class FinalState : IOperationState
{
private readonly IPersistenceService _persistenceService;
public FinalState(IPersistenceService persistenceService)
{
_persistenceService = persistenceService;
}
public int ExecuteOperation(OperatingClass operatingClass)
{
return _persistenceService
.PersistData(operatingClass.id, operatingClass.value)
? 200
: 503;
}
}
And other classes sush as OperatingClass would use StateFactory too:
public class OperatingClass
{
public IOperationState OperationState { get; set; }
public int id { get; set; }
public double value { get; set; }
public OperatingClass(int id, StateFactory stateFactory) //constructor
{
this.id = id;
value = 0;
// OperationState = new StartingState();
OperationState = stateFactory.GetByType(StateType.Start);
}
public int Execute()
{
return OperationState.ExecuteOperation(this);
}
}
And it is necessary to create concrete example of PersistenceService:
public interface IPersistenceService
{
bool PersistData(int id, double value);
}
public class PersistenceService : IPersistenceService
{
public bool PersistData(int id, double value)
{
throw new NotImplementedException();
}
}

Lazy decorator - The lazily-initialized type does not have a public, parameterless constructor

I cant make working the code below.. Do I need other class that impolement my IComponent with paratmeterless consturctor?
public class Program
{
public static void Main()
{
var lazy = new Lazy<IComponent>();
IComponent comp = lazy.Value;
var client = new ComponentClient(comp);
client.Run();
}
}
public interface IComponent
{
void Something();
}
public class LazyComponent : IComponent
{
public Lazy<IComponent> _LazyComponent { get; set ;}
public LazyComponent(Lazy<IComponent> lazyComponent)
{
_LazyComponent = lazyComponent;
}
public void Something()
{
_LazyComponent.Value.Something();
}
}
public class ComponentClient
{
public IComponent _Component { get; set; }
public ComponentClient(IComponent component)
{
_Component = component;
}
public void Run()
{
_Component.Something();
}
}
You need to tell the Lazy how to construct the component, by giving it a factory method.
https://learn.microsoft.com/en-us/dotnet/api/system.lazy-1?view=netframework-4.8
public class Program
{
public static void Main()
{
var lazy = new Lazy<IComponent>(() => new RealComponent());
var lazyComponent = new LazyComponent(lazy);
var client = new ComponentClient(lazyComponent);
client.Run();
}
}

StructureMap: conditionally use concrete type based on concrete instance property value

I'm struggling to make StructureMap use one of concrete types sharing a common interface. This is further complicated by the fact that all candidate objects are descendants of an intermediate abstract class.
public interface ICustomer
{
string Id { get; }
}
public abstract class CommonCustomer : ICustomer {
public abstract string Id { get; }
}
// Fallback type if none matched
public class BaseCustomer : CommonCustomer
{
public override string Id { get; } = "Base";
}
// Concrete type 1
public class AlphaCustomer : CommonCustomer
{
public override string Id { get; } = "Alpha";
}
// Concrete type 2
public class BravoCustomer : CommonCustomer
{
public override string Id { get; } = "Bravo";
}
What I tried so far:
Scan(x =>
{
x.TheCallingAssembly();
x.AddAllTypesOf<ICustomer>();
});
var key = "Alpha";
For<ICustomer>().Use("",
context => context.GetAllInstances<ICustomer>()
.FirstOrDefault(x => x.Id == key)).Singleton();
For<ICustomer>().UseIfNone<BaseCustomer>().Singleton();
How can I select a concrete type based on it's string property? And how do I scan through types which do not directly implement ICustomer?
Sounds like you want to create a factory for instantiating ICustomer.
public interface ICustomerFactory
{
ICustomer Create(string key);
}
public class CustomerFactory : ICustomerFactory
{
private readonly IContainer _container;
public CustomerFactory(IContainer container)
{
_container = container;
}
public ICustomer Create(string key) => _container.TryGetInstance<ICustomer>(key);
}
And during configuration of your container naming them:
var container = new Container(c =>
{
c.For<ICustomerFactory>().Use<CustomerFactory>();
c.Scan(x =>
{
x.TheCallingAssembly();
x.AddAllTypesOf(typeof(ICustomer))
.NameBy(t => ((ICustomer)Activator.CreateInstance(t, new object[0], new object[0])).Id);
});
});
Usage:
ICustomerFactory factory;
var customer1 = factory.Create("Alpha");
var customer2 = factory.Create("Bravo");
var customer3 = factory.Create("Base");
var customer4 = factory.Create("NotExisting"); // returns null.

Inject different implementations that have same interface in multiple constructor parameters

My goal is to create an object that contains different implementations of an interface and at runtime select the implementation to use. I'm using the Dependency injection in ASP.NET Core.
Code:
public interface IStateRepository : IDbReadRepository<IState> { }
public interface IDbReadRepository<T> : IBaseRepository
{
IReadOnlyList<T> GetAll();
}
public interface IBaseRepository
{
IUserContext UserContext { get; set; }
}
namespace MvcOpinionatedTemplate.Repositories.Dapper
{
public class StateRepository : BaseDbRepository, IStateRepository
{
public StateRepository(IUserContext userContext, IDbConnection dbConnection) : base(userContext, dbConnection) { }
public IReadOnlyList<IState> GetAll()
{
return _dbConnection.Query<State>("SELECT * FROM State").ToList();
}
}
}
namespace Template.Repositories.Local
{
public class StateRepository : BaseRepository, IStateRepository
{
public StateRepository(IUserContext userContext) : base(userContext) { }
public IReadOnlyList<IState> GetAll()
{
var filePath = Path.Combine(AppContext.BaseDirectory, #"Local\json\states.json");
return JsonConvert.DeserializeObject<List<State>>(File.ReadAllText(filePath));
}
}
namespace MvcOpinionatedTemplate.Repositories.Collections
{
public class StateRepositories
{
public IStateRepository Local { get; }
public IStateRepository SqlServer { get; }
public StateRepositories(IStateRepository local, IStateRepository sqlServer)
{
Local = local;
SqlServer = sqlServer;
}
}
}
What I'd like to do is set in the Startup.ConfigureServices():
services.AddTransient<StateRepositories, XXXXX>
I tried this:
services.AddTransient<StateRepositories>(s => new StateRepositories(new Repositories.Local.StateRepository(--UserContext--), new Repositories.Dapper.StateRepository(-UserContext--)));
The problem is how to have DI populate UserContext. I have it defined Startup.ConfigureServices():
services.AddScoped<IUserContext, UserContext>();
How do have DI populate UserContext for the StateRepositories implementations? Or is there a better approach to achieve my goal?
You can register your IStateRepository separately and then inject IEnumerable<IStateRepository> which injects all implementations of IStateRepository.
public interface IStateRepository
{
}
public class LocalRepository : IStateRepository
{
}
public class DapperRepository : IStateRepository
{
}
services.AddTransient<IStateRepository, LocalRepository>()
.AddTransient<IStateRepository, DapperRepository>()
.AddTransient<StateRepositories>();
public class StateRepositories
{
public IStateRepository Local { get; }
public IStateRepository SqlServer { get; }
public StateRepositories(IEnumerable<IStateRepository> repositories)
{
Local = repositories.OfType<LocalRepository>().FirstOrDefault();
SqlServer = repositories.OfType<DapperRepository>().FirstOrDefault();
}
}

Change business layer class to be loosely coupled

I have below code I am working on. I am working on data access adon.net layer. I have problem with my business layer class called UserBAL. The problem is that I am creating instance of dal and dbmanager inside constructor of UserBAL. How can I change this to be loosely coupled for UserBAL? Hope you get my point.
public interface IEntity
{
int Id { get; set; }
int DoSomething(string one, int two);
}
public class User : IEntity
{
public int Id { get; set; }
public int DoSomething(string one, int two)
{
throw new NotImplementedException();
}
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class UserBal //busines logic
{
private readonly IRepositoryDal<User> _userRepositoryDal;
public UserBal()
{
_userRepositoryDal = new UserRepositoryDal(new DbManager("sqlserver?"));
}
public IEnumerable<User> SearchByName(string name)
{
return _userRepositoryDal.SearchByName(name);
}
}
interface IRepositoryDal<T> where T : IEntity
{
IEnumerable<T> SearchByName(string username);
T SearchById(string id);
void Update(T entity);
void Remove(T entity);
void Add(T entity);
}
public class UserRepositoryDal: IRepositoryDal<User>
{
private readonly IDbManager _dbManager;
public UserRepositoryDal(IDbManager dbManager)
{
//read from either singleton or configuration file !!
_dbManager = dbManager;
}
public IEnumerable<User> SearchByName(string username)
{
var parameters = new List<IDbDataParameter>
{
_dbManager.CreateParameter("#FirstName", 50, username, DbType.String),
};
var userDataTable = _dbManager.GetDataTable("storedpr2",
CommandType.StoredProcedure, parameters.ToArray());
foreach (DataRow dr in userDataTable.Rows)
{
var user = new User
{
Id = int.Parse(dr["Id"].ToString()),
Firstname = dr["Firstname"].ToString(),
Lastname = dr["LastName"].ToString(),
Email = dr["Email"].ToString()
};
yield return user;
}
}
public User SearchById(string id)
{
var parameters = new List<IDbDataParameter>
{
_dbManager.CreateParameter("#Id", 50, id, DbType.Int32),
};
var userDataTable = _dbManager.GetDataTable("storedpr2",
CommandType.StoredProcedure, parameters.ToArray());
return new User
{
Id = int.Parse(userDataTable.Rows[0]["Id"].ToString()),
Firstname = userDataTable.Rows[0]["Firstname"].ToString(),
Lastname = userDataTable.Rows[0]["LastName"].ToString(),
Email = userDataTable.Rows[0]["Email"].ToString()
};
}
public void Update(User entity)
{
throw new System.NotImplementedException();
}
public void Remove(User entity)
{
throw new System.NotImplementedException();
}
public void Add(User entity)
{
throw new System.NotImplementedException();
}
}
public partial class FrmLogin : Form
{
private readonly UserBal _userBal;
public FrmLogin()
{
InitializeComponent();
_userBal = new UserBal();
}
}
You should use dependency injection, and for required dependencies, you can use constructor injection, e.g:
public class UserBal
{
private readonly IRepositoryDal<User> _userRepositoryDal;
public UserBal(IRepositoryDal<User> userRepositoryDal)
{
_userRepositoryDal = userRepositoryDal
?? throw new ArgumentNullException(nameof(userRepositoryDal));
}
...
}
Dependency injection is the way to go. Here's a simplified example of your situation.
Given your classes could be like this:
public interface IEntity { }
public interface IRepositoryDal<T> where T : IEntity { }
public interface IDbManager { }
public class User : IEntity { }
public class UserBal //busines logic
{
[Injectivity.Attributes.Inject]
private IRepositoryDal<User> _userRepositoryDal;
}
public class UserRepositoryDal: IRepositoryDal<User>
{
[Injectivity.Attributes.Inject]
private IDbManager _dbManager;
}
public class DbManager : IDbManager
{
[Injectivity.Attributes.Construct()]
public DbManager([Injectivity.Attributes.Key("dbKey", typeof(string))] string x)
{
Console.WriteLine($"DbManager created with parameter \"{x}\"");
}
}
...then this code:
var context = Injectivity.Context.CreateRoot();
context.SetConfig<string>("dbKey", "sqlserver?");
context.SetFactory<IDbManager, DbManager>();
context.SetFactory<IRepositoryDal<User>, UserRepositoryDal>();
context.SetFactory<UserBal, UserBal>();
var user = context.Resolve<UserBal>();
...will produce this output:
DbManager created with parameter "sqlserver?"
Now, I've used a DI container that I wrote a number of years back. It's very flexible.
So the call to context.Resolve<UserBal>(); runs down the chain of seeing what needs to be injected to create all of the instances of all of the objects. Ultimately the creation of UserBal requires the DbManager to be created and hence the output.
You would normally not explicitly register each factory. Normally you would put attributes on all of the classes you want to register and then use context.Register(Assembly.LoadFrom("My.DLL")); or create an XML config file and call context.LoadConfig(XDocument.Load("config.xml"));.
You can even do things like this:
context.SetDecorator<IRepositoryDal<User>, UserRepositoryDalDecorator>();
This will cause all calls to context.Resolve<IRepositoryDal<User>>() or [Inject] attributes to automatically wrap the real instances in this decorator. Ideal to intercepting method calls for debugging purposes.

Categories