I've just implemented the Translator pattern discussed here and here like so...
ITranslator interface...
public interface ITranslator
{
bool CanTranslate(Type targetType, Type sourceType);
bool CanTranslate<TTarget, TSource>();
object Translate(Type targetType, object source);
TTarget Translate<TTarget>(object source);
}
Translator.cs...
public abstract class Translator<TBusinessEntity, TServiceEntity> : ITranslator where TBusinessEntity : class
where TServiceEntity : class
{
public bool CanTranslate(Type targetType, Type sourceType)
{
return (targetType == typeof(TBusinessEntity) && sourceType == typeof(TServiceEntity)) ||
(targetType == typeof(TServiceEntity) && sourceType == typeof(TBusinessEntity));
}
public bool CanTranslate<TTarget, TSource>()
{
return CanTranslate(typeof (TTarget), typeof (TSource));
}
public TTarget Translate<TTarget>(object source)
{
return (TTarget)Translate(typeof(TTarget), source);
}
public object Translate(Type targetType, object source)
{
if (targetType == typeof(TBusinessEntity))
return ServiceToBusiness((TServiceEntity)source);
if (targetType == typeof(TServiceEntity))
return BusinessToService((TBusinessEntity)source);
throw new System.ArgumentException("Invalid type passed to Translator", "targetType");
}
protected abstract TServiceEntity BusinessToService(TBusinessEntity value);
protected abstract TBusinessEntity ServiceToBusiness(TServiceEntity value);
protected abstract List<TServiceEntity> BusinessToService(List<TBusinessEntity> valueList);
protected abstract List<TBusinessEntity> ServiceToBusiness(List<TServiceEntity> valueList);
}
Here is my StudentFeeTranslator class that implements the Translator abstract methods...
public class StudentFeeTranslator : Translator<StudentFee, StudentFeeType>
{
#region Overrides of Translator<StudentFee,StudentFeeType>
protected override StudentFeeType BusinessToService(StudentFee value)
{
return new
StudentFeeType
{
StudentFeeId = value.StudentFeeRefId,
FeeId = value.FeeRefId,
StudentId = value.StudentRefId,
SchoolId = value.SchoolRefId,
FeeDate = value.AssessmentDate,
FeeAmount = value.AssessmentAmount,
Balance = value.UnpaidBalance,
FeeTypeId = value.FeeType,
Description = value.FeeDescription
};
}
protected override StudentFee ServiceToBusiness(StudentFeeType value)
{
return new
StudentFee
{
StudentFeeRefId = value.StudentFeeId,
FeeRefId = value.FeeId,
StudentRefId = value.StudentId,
SchoolRefId = value.SchoolId,
AssessmentDate = value.FeeDate,
AssessmentAmount = value.FeeAmount,
UnpaidBalance = value.Balance,
FeeType = (Byte)value.FeeTypeId,
FeeDescription = value.Description
};
}
protected override List<StudentFeeType> BusinessToService(List<StudentFee> valueList)
{
return valueList.Select(BusinessToService).ToList();
}
protected override List<StudentFee> ServiceToBusiness(List<StudentFeeType> valueList)
{
return valueList.Select(ServiceToBusiness).ToList();
}
#endregion
}
Next is my StudentFeeService class minus the irrelevant methods. Notice the Translator property tagged for injection...
public partial class StudentFeeService : IStudentFeeService
{
#region Public Members
[Dependency]
public ITranslator Translator { get; set; }
#endregion
#region Private Methods
private List<StudentFeeType> ConvertStudentFeeListToListOfStudentFeeTypes(List<StudentFee> studentFees)
{
return Translator.Translate<List<StudentFeeType>>(studentFees);
}
#endregion
}
Finally, here is the code snippet of my attempt to register the Translator class with my Unity container...
container.RegisterType(typeof (ITranslator), typeof (Translator<,>));
This attempt failed. My question is how can I register a generic abstract class with a Unity container? FYI I'm using MSUnity 2.0.
You are trying to map a non-generic interface to an open generic type. How should Unity (or any other container) guess if your service needs a StudenFeeTranslator or a RentalFeeTranslator? Both implement ITranslator and that is all the container can see.
You can register all of your concrete implementations of ITranslator giving them individual names. This is something all containers support. And then make Unity inject that specific dependency into the Translator property of your service. Something like
container.RegisterType(typeof(ITranslator), typeof(StudentFeeTranslator), "StudentFee");
container.RegisterType(typeof(ITranslator), typeof(RentalFeeTranslator), "RentalFee");
container.RegisterType(typeof(IStudentFeeService), typeof(StudentFeeService),
new InjectionProperty("Translator", new ResolvedParameter<ITranslator>("StudentFee")));
That is a lot of repetetive code though.
Unity does not come with registration conventions out-of-the-box. But the TecX project contains an enhanced configuration engine that would allow you to do something like this:
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.Scan(
scanner =>
{
scanner.AddAllImplementationsOf(typeof(ITranslator);
scanner.AssembliesFromApplicationBaseDirectory();
});
container.AddExtension(builder);
This registers all implementations of ITranslator with the name of the implementing class (e.g. the name for StudentFeeTranslator would be StudentFeeTranslator) in one go.
If you make your interface generic it would be easier to inject into the property. Matching ITranslator<X, Y> to an implementation thereof is not really hard to do.
Related
I'm trying do upgrade Unity from version 3.0.1304.1 to the latest, 5.11.1. Unsurprisingly, there are some issues (3.0.1304.1 is quite old). The code below accomplished two things:
Use a ContainerControlledLifetimeManager for all classes deriving from some base class X, without specifying that for every class derived from X.
Use the default constructor when resolving for all classes deriving from base class Y, without specifying that for every class derived from Y.
Unity documentation is scarce nowadays, and lacks examples. I've read the upgrade notes and regular documentation but I can't make this work with the new version.
So far I have:
Replaced IConstructorSelectorPolicy with ISelect<ConstructorInfo>
Replaced IBuilderContext with BuilderContext (or ref BuilderContext in the PreBuildUp function)
But what to do with the LifetimeManagerFactory? Should I derive IOCConstructorSelectorPolicy from ISelect<ConstructorInfo> and should IOCConstructorSelectorPolicy.SelectConstructor then be replaced by Select (as per the ISelect interface) and how to implement this Select function?
I know this is a very long shot, and I'll post this on the unity github as well (as a request for documentation of sorts), but hopefully there is someone who can give me some pointers.
/// <summary>
/// This class merely exists for readability: avoid having to write the class it derives from.
/// </summary>
public class IOCConfigLifetimeExtension : DefaultLifetimeManagerExtension<ContainerControlledLifetimeManager, X>
{}
/// <summary>
/// An IOC strategy to use a specific LifetimeManager (<typeparamref name="TLifetimeManager"/>) for subclasses of <typeparamref name="TBaseClass"/>.
/// </summary>
public class DefaultLifetimeManagerExtension<TLifetimeManager, TBaseClass> : UnityContainerExtension where TLifetimeManager : LifetimeManager, new()
{
protected override void Initialize()
{
var theFactory = new LifetimeManagerFactory(Context, typeof(TLifetimeManager));
Context.Strategies.Add(new DefaultLifetimeManagerStrategy(theFactory, TypePredicate), UnityBuildStage.TypeMapping);
}
private bool TypePredicate(Type type)
{
return typeof (TBaseClass).IsAssignableFrom(type);
}
}
public class DefaultLifetimeManagerStrategy : BuilderStrategy
{
public DefaultLifetimeManagerStrategy(LifetimeManagerFactory factory, Predicate<Type> typePredicate)
{
mFactory = factory;
mTypePredicate = typePredicate;
}
public override void PreBuildUp(IBuilderContext context)
{
if (context.Existing == null) {
var theLifetime = context.Policies.GetNoDefault<ILifetimePolicy>(context.BuildKey, false);
if (theLifetime == null && mTypePredicate(context.BuildKey.Type)) {
theLifetime = mFactory.CreateLifetimePolicy();
context.PersistentPolicies.Set(theLifetime, context.BuildKey);
}
}
}
private readonly LifetimeManagerFactory mFactory;
private readonly Predicate<Type> mTypePredicate;
}
/// <summary>
/// A Unity extension that prioritizes the default constructor for classes derived of Y when available, otherwise the default resolve method is used.
/// </summary>
public class IOCExtension : UnityContainerExtension
{
protected override void Initialize()
{
var theDefaultConstructorSelectorPolicy = Context.Policies.Get<IConstructorSelectorPolicy>(null);
Context.Policies.SetDefault<IConstructorSelectorPolicy>(new IOCConstructorSelectorPolicy(theDefaultConstructorSelectorPolicy));
}
public class IOCConstructorSelectorPolicy : IConstructorSelectorPolicy
{
public IOCConstructorSelectorPolicy(IConstructorSelectorPolicy defaultConstructorSelectorPolicy)
{
mDefaultConstructorSelectorPolicy = defaultConstructorSelectorPolicy;
}
public SelectedConstructor SelectConstructor(IBuilderContext context, IPolicyList resolverPolicyDestination)
{
Type theType = context.BuildKey.Type;
if (typeof(DataNode).IsAssignableFrom(theType)) {
ConstructorInfo theDefaultConstructorInfo = theType.GetConstructor(Type.EmptyTypes);
if (theDefaultConstructorInfo != null && theDefaultConstructorInfo.IsPublic) {
return new SelectedConstructor(theDefaultConstructorInfo);
}
}
return mDefaultConstructorSelectorPolicy.SelectConstructor(context, resolverPolicyDestination);
}
private readonly IConstructorSelectorPolicy mDefaultConstructorSelectorPolicy;
}
}
I have created a asp.net web api project and implemented the below HTTP GET method in AccountController and the related service method & repository method in AccountService & AccountRepository respectively.
// WEB API
public class AccountController : ApiController
{
private readonly IAccountService _accountService;
public AccountController(IAccountService accountService)
{
_accountService = accountService;
}
[HttpGet, ActionName("UserProfile")]
public JsonResult<decimal> GetUserSalary(int userID)
{
var account = _accountService.GetUserSalary(userID);
if (account != null)
{
return Json(account.Salary);
}
return Json(0);
}
}
Service / Business Layer
public interface IAccountService
{
decimal GetUserSalary(int userId);
}
public class AccountService : IAccountService
{
readonly IAccountRepository _accountRepository = new AccountRepository();
public decimal GetUserSalary(int userId)
{
return _accountRepository.GetUserSalary(userId);
}
}
Repository / Data Access Layer
public interface IAccountRepository
{
decimal GetUserSalary(int userId);
}
public class AccountRepository : IAccountRepository
{
public decimal GetUserSalary(int userId)
{
using (var db = new AccountEntities())
{
var account = (from b in db.UserAccounts where b.UserID == userId select b).FirstOrDefault();
if (account != null)
{
return account.Salary;
}
}
return 0;
}
}
UnityConfig
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<IAccountService, AccountService>();
container.RegisterType<IAccountRepository, AccountRepository>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
But when I invoke the API method GetUserSalary() I get an error saying
An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.
Check that you did not forget to register Unity IoC container itself:
if you use ASP.NET Framework it could be - Global.asax or Startap.cs (Owin) via UnityConfig.RegisterComponents() method.
if you use ASP.NET Core then in the Startup.cs file (I was unable to find official guides for its configuting)
Your current constructor has parameters (or args if you prefer).
see:
public AccountController(IAccountService accountService)
{
_accountService = accountService;
}
All you need to do is add a "Parameter-less Constructor" into the controller as well.
public AccountController()
{
}
Parameter-less constructors are usually above the ones that have params, though as far as I am aware this is only due to standards not any actual effect(s) it may cause.
There is also an already existing issue/question similar to this I will link below that may provide further details.
Make sure that the controller has a parameterless public constructor error
I am newbie to MVC3 application development, currently, we need following Application technologies as requirement
MVC3 framework
IOC framework – Autofac to manage object creation dynamically
Moq – Unit testing
Entity Framework
Repository and Unit Of Work Pattern of Model class
I have gone through many article to explore an basic idea about the above points but still I am little bit confused on the “Repository and Unit Of Work Pattern “. Basically what I understand Unit Of Work is a pattern which will be followed along with Repository Pattern in order to share the single DB Context among all Repository object, So here is my design :
IUnitOfWork.cs
public interface IUnitOfWork : IDisposable
{
IPermitRepository Permit_Repository{ get; }
IRebateRepository Rebate_Repository { get; }
IBuildingTypeRepository BuildingType_Repository { get; }
IEEProjectRepository EEProject_Repository { get; }
IRebateLookupRepository RebateLookup_Repository { get; }
IEEProjectTypeRepository EEProjectType_Repository { get; }
void Save();
}
UnitOfWork.cs
public class UnitOfWork : IUnitOfWork
{
#region Private Members
private readonly CEEPMSEntities context = new CEEPMSEntities();
private IPermitRepository permit_Repository;
private IRebateRepository rebate_Repository;
private IBuildingTypeRepository buildingType_Repository;
private IEEProjectRepository eeProject_Repository;
private IRebateLookupRepository rebateLookup_Repository;
private IEEProjectTypeRepository eeProjectType_Repository;
#endregion
#region IUnitOfWork Implemenation
public IPermitRepository Permit_Repository
{
get
{
if (this.permit_Repository == null)
{
this.permit_Repository = new PermitRepository(context);
}
return permit_Repository;
}
}
public IRebateRepository Rebate_Repository
{
get
{
if (this.rebate_Repository == null)
{
this.rebate_Repository = new RebateRepository(context);
}
return rebate_Repository;
}
}
}
PermitRepository .cs
public class PermitRepository : IPermitRepository
{
#region Private Members
private CEEPMSEntities objectContext = null;
private IObjectSet<Permit> objectSet = null;
#endregion
#region Constructors
public PermitRepository()
{
}
public PermitRepository(CEEPMSEntities _objectContext)
{
this.objectContext = _objectContext;
this.objectSet = objectContext.CreateObjectSet<Permit>();
}
#endregion
public IEnumerable<RebateViewModel> GetRebatesByPermitId(int _permitId)
{
// need to implment
}
}
PermitController .cs
public class PermitController : Controller
{
#region Private Members
IUnitOfWork CEEPMSContext = null;
#endregion
#region Constructors
public PermitController(IUnitOfWork _CEEPMSContext)
{
if (_CEEPMSContext == null)
{
throw new ArgumentNullException("Object can not be null");
}
CEEPMSContext = _CEEPMSContext;
}
#endregion
}
So here I am wondering how to generate a new Repository for example “TestRepository.cs” using same pattern where I can create more then one Repository object like
RebateRepository rebateRepo = new RebateRepository ()
AddressRepository addressRepo = new AddressRepository()
because , what ever Repository object I want to create I need an object of UnitOfWork first as implmented in the PermitController class. So if I would follow the same in each individual Repository class that would again break the priciple of Unit Of Work and create multiple instance of object context.
So any idea or suggestion will be highly appreciated.
Thank you
Your IUnitOfWork interface has too many responsibilities. Each time you add a new repository, you would need to change your IUnitOfWork interface and all of its implementations.
Instead, how about something like this?
public interface IUnitOfWork
{
int SaveChanges();
}
You can then implement this interface in your Entity Framework ObjectContext or DbContext:
public MyCustomContext : DbContext, IUnitOfWork
{
// ... this class already implements the SaveChanges method
}
You can then constructor inject the unit of work into each of your repositories. AutoFac can make certain the same instance is shared among multiple repositories used within the same HttpContext:
public class PermitRepository : IPermitRepository
{
#region Private Members
private readonly IObjectSet<Permit> _objectSet;
private readonly IUnitOfWork _unitOfWork;
#endregion
#region Constructors
public PermitRepository(IUnitOfWork unitOfWork, IObjectSet<Permit> objectSet)
{
_unitOfWork = unitOfWork;
_objectSet = objectSet;
}
#endregion
public IEnumerable<RebateViewModel> GetRebatesByPermitId(int _permitId)
{
// need to implment
}
}
Then, when you constructor inject the repository into the controller, the IUnitOfWork will automatically be constructor injected into the repository.
public class PermitController : Controller
{
#region Private Members
private readonly IPermitRepository _permitRepos;
#endregion
#region Constructors
public PermitController(IPermitRepository permitRepos)
{
if (permitRepos== null)
{
throw new ArgumentNullException("permitRepos");
}
_permitRepos = permitRepos;
}
#endregion
}
Note that when querying data out of your repository, you're really not doing any work, so the IUnitOfWork interface does not apply here. It applies when inserting, updating, and deleting entities, not when selecting them.
in my implementation, I have an interface as: ICachingManager. I've got now one implementation. I also created a manager class as:
public class CachingManager
{
#region Members
private ICachingManager service;
#endregion
#region Constructors
public CachingManager(ICachingManager service)
{
this.service = service;
}
#endregion
#region Public Methods
public void EnCache<T>(string key, T value)
{
this.service.EnCache<T>(key, value);
}
public T DeCache<T>(string key)
{
return this.service.DeCache<T>(key);
}
#endregion
}
In case I had one implementation, then I can easily register the CachingManager class with Unity, automatically Unity resolves and injects the ICachingManager.
In case I had more than one implementation using named types, then how can I can make use of Unity? Do I need to make use of an Abstract Factory to decide on which named type to initialize?
Is it a good idea to make use of such a composite class or use directly implementations of the interface with Abstract Factory?
You don't have to create an abstract factory. You can inject a given named implementation:
public class MyClient
{
[Dependency("NamedManager")]
public ICachingManager CachingManager { get; set; }
// or in the constructor
public MyClient([Dependency("NamedManager")] ICachingManager cachingManager) {
// ...
}
}
or you can configure the container to do the same thing:
public class MyClient
{
public MyClient(ICachingManager cachingManager) {
// ...
}
}
...
void ContainerBuilder() {
...
Container.RegisterType<MyClient>(
new InjectionConstructor(
new ResolvedParameter<ICachingManager>("NamedManager")));
...
}
My Grails application has a large number of enums that look like this:
public enum Rating {
BEST("be"), GOOD("go"), AVERAGE("av"), BAD("ba"), WORST("wo")
final String id
private RateType(String id) {
this.id = id
}
static public RateType getEnumFromId(String value) {
values().find {it.id == value }
}
}
If I have a command object such as this:
class MyCommand {
Rating rating
}
I would like to (for example) automatically convert a request parameter with value "wo" to Rating.WORST.
The procedure for defining custom converters is described here (in the context of converting Strings to Dates). Although this procedure works fine, I don't want to have to create a class implementing PropertyEditorSupport for each of my enums. Is there a better alternative?
I found a solution I'm pretty happy with.
Step 1: Create an implementation of PropertyEditorSupport to convert text to/from the relevant Enum
public class EnumEditor extends PropertyEditorSupport {
private Class<? extends Enum<?>> clazz
public EnumEditor(Class<? extends Enum<?>> clazz) {
this.clazz = clazz
}
public String getAsText() {
return value?.id
}
public void setAsText(String text) {
value = clazz.getEnumFromId(text)
}
}
Step 2: Define a class that registers EnumEditor as a converter for the various enum classes. To change the list of enum classes that are bindable by id, just modify BINDABLE_ENUMS
public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
private static final String REQUIRED_METHOD_NAME = 'getEnumFromId'
// Add any enums that you want to bind to by ID into this list
private static final BINDABLE_ENUMS = [Rating, SomeOtherEnum, SomeOtherEnum2]
public void registerCustomEditors(PropertyEditorRegistry registry) {
BINDABLE_ENUMS.each {enumClass ->
registerEnum(registry, enumClass)
}
}
/**
* Register an enum to be bound by ID from a request parameter
* #param registry Registry of types eligible for data binding
* #param enumClass Class of the enum
*/
private registerEnum(PropertyEditorRegistry registry, Class<? extends Enum<?>> enumClass) {
boolean hasRequiredMethod = enumClass.metaClass.methods.any {MetaMethod method ->
method.isStatic() && method.name == REQUIRED_METHOD_NAME && method.parameterTypes.size() == 1
}
if (!hasRequiredMethod) {
throw new MissingMethodException(REQUIRED_METHOD_NAME, enumClass, [String].toArray())
}
registry.registerCustomEditor(enumClass, new EnumEditor(enumClass))
}
}
Step 3: Make Spring aware of the registry above by defining the following Spring bean in grails-app/conf/spring/resources.grooovy
customPropertyEditorRegistrar(CustomPropertyEditorRegistrar)
So the default Databinding binds on the Enum name and not a separately defined property of the Enum. You can either create your own PropertyEditor as you have mentioned or do a work-around similar to this:
class MyCommand {
String ratingId
Rating getRating() {
return Rating.getEnumFromId(this.ratingId)
}
static constraints = {
ratingId(validator:{val, obj -> Rating.getEnumFromId(val) != null })
}
}