I cant seem to get the proper service to configure.
The idea is to register all types that have the marker interface IDao and have there base classes be the service.
base class
public abstract class DirectorDaoContract : RepositoryBase<Director>
{
public abstract Director Get(int id);
}
implementing class
public class DirectorDao : DirectorDaoContract,IDao
{
public override Director Get(int directorId)
{
.....
}
}
The Idea being that a
Resolve<DirectorDaoContract>()
would return DirectorDao
my current attempt
container.Register(AllTypes.FromAssemblyNamed(dataAssembly)
.BasedOn<IDao>)
.WithService.Base()
.Configure(c => c.LifeStyle.PerThread));
No matter what I have tried I am unable to get the service correct.
container.Register(AllTypes.FromAssemblyNamed(dataAssembly)
.BasedOn<IDao>()
.WithService.FromInterface()
.Configure(c => c.LifeStyle.PerThread));
try this
container.Register(AllTypes.FromAssemblyNamed(dataAssembly(
.BasedOn(typeof(DirectorDaoContract))
.WithService.Select((t,b) => t.GetInterfaces().Where(d => d == typeof(IDao)).AsEnumerable<Type>());
Related
I have gotten a task that contains creating a .Net 4.8 application that contains a "HttpSelfHostServer".
I'm stuck in the quest of assigning "IServiceCollection services" to config.DependencyResolver (of type System.Web.Http.Dependencies.IDependencyResolver)
I would really like not to use autofac or other frameworks, but all guids I can find are pointing toward these frameworks. Isn't Microsoft providing a way through?
I just had to solve the same issue. This is how i did it:
First I created a new facade class to map the IServiceCollection from the host builder to the interface HttpSelfHostConfiguration supports:
using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies;
using Microsoft.Extensions.DependencyInjection;
namespace IntegrationReceiver.WebApi
{
public class HttpSelfHostDependencyResolver : IDependencyResolver
{
private readonly IServiceProvider sp;
private readonly IServiceScope scope;
public HttpSelfHostDependencyResolver(IServiceProvider sp)
{
this.sp = sp;
this.scope = null;
}
public HttpSelfHostDependencyResolver(IServiceScope scope)
{
this.sp = scope.ServiceProvider;
this.scope = scope;
}
public IDependencyScope BeginScope() => new HttpSelfHostDependencyResolver(sp.CreateScope());
public void Dispose() => scope?.Dispose();
public object GetService(Type serviceType) => sp.GetService(serviceType);
public IEnumerable<object> GetServices(Type serviceType) => sp.GetServices(serviceType);
}
}
This required me to get the latest NuGet package Microsoft.Extensions.DependencyInjection.Abstractions according to an answer here: How do I see all services that a .NET IServiceProvider can provide?
I then registered my HttpSelfHostServer in the service provider with this code:
services.AddSingleton(sp => new HttpSelfHostDependencyResolver(sp));
services.AddSingleton(sp =>
{
//Starting the HttpSelfHostServer with user-level permissions requires to first run a command like
// netsh http add urlacl url=http://+:8080/ user=[DOMAINNAME]\[USERNAME]
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional });
config.DependencyResolver = sp.GetRequiredService<HttpSelfHostDependencyResolver>();
return new HttpSelfHostServer(config);
});
And finally, to find my ApiController, I had to register that too in the service provider. I did that simply with:
services.AddScoped<HealthCheckController>();
For brewity, I'm just including my api controller below to illustrate how it now gets its dependencies:
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
namespace IntegrationReceiver.WebApi
{
public class HealthCheckController : ApiController
{
private readonly ServiceBusRunner serviceBusRunner;
public HealthCheckController(ServiceBusRunner serviceBusRunner)
{
this.serviceBusRunner = serviceBusRunner;
}
[HttpGet]
public async Task<HttpResponseMessage> Get()
{
var response = new
{
serviceBusRunner.RunningTasks,
serviceBusRunner.MaxRunningTasks
};
return await Json(response)
.ExecuteAsync(System.Threading.CancellationToken.None);
}
}
}
This is a pretty dumb-down implementation but works for me until I can upgrade this code to net5.
I hope it helps you too!
I'm currently registering a bunch of stuff at one point in my initialisation sequence
Container.Register(AllTypes.FromAssemblyContaining<MyAssembly>()
.BasedOn(typeof(IRepository<>))
.WithService.Self().Configure(c => c.LifeStyle.Transient));
I'm using WithService.Self so that it doesn't automatically pick up AllInterfaces, so that the interface that I will want to add later on as a type forward has not already been added.
I'd like then (later on) to be able to add a type forward to one of the already registered components, and Intellisense on the ForwardedTypes property suggested using .Forward(typof()) e.g.
Container.Register(Component.For<IOtherInterface>()
.Forward(typeof(IOtherInterface))
.ImplementedBy<AlreadyRegisteredType>().LifeStyle.Transient);
Is this possible?
EDIT:
I've been trying to get the stuff that Krzysztof has suggested working so I've generated a test project (below). I've tried various combinations to get the ConfigureFor to forward IMyInterface to MySecondType, but just can't get it to work, when done as a second step after initial registration of my component types. I'm probably being dim, but I'm just not getting how the ConfigureFor command works, and the documentation is a little sketchy (non-existant) on the subject.
namespace TestProject1
{
public class MyType : IMyInterface
{
public virtual string MyProperty { get; set; }
}
public class MySecondType : IMyInterface
{
public virtual string MySecondProperty { get; set; }
}
public interface IMyInterface
{
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
//New Container
var container = new WindsorContainer();
//Register Types
container.Register(
AllTypes.FromAssemblyContaining<MyType>().BasedOn<IMyInterface>().WithService.Self().Configure(
c => c.LifeStyle.Transient));
//Other stuff happens here...
//Now Register our interface as a forward
container.Register(AllTypes.FromAssemblyContaining<IMyInterface>()
.BasedOn<IMyInterface>()
.WithService.Base()
.ConfigureFor<IMyInterface>(r => r.Forward<MySecondType>()).Configure(c => c.LifeStyle.Transient));
var typeA = new MySecondType();
var typeB = container.Resolve<IMyInterface>();
Assert.IsInstanceOfType(typeB.GetType(), typeA.GetType());
}
}
}
What you described in the comment is registering another component for the AlreadyRegisteredType.
If you want to add a forward to the same component use
ConfigureFor<AlreadyRegisteredType>(c => c.Forward<IOtherInterface>())
full example:
Container.Register(AllTypes.FromThisAssembly()
.BasedOn<IEmptyService>()
.WithService.Base()
.ConfigureFor<EmptyServiceComposite>(r => r.Forward<EmptyServiceComposite>()));
Once you registered some type
Container.Register(Component.For<SomeType>());
you can forward some interface to it in this way:
Container.Register(Component.For<ISomeInterface>()
.UsingFactoryMethod<ISomeInterface>(kernel => kernel.Resolve<SomeType>()));
It is my understanding I can configure AutoMapper in the following way and during mapping it should format all source model dates to the rules defined in the IValueFormatter and set the result to the mapped model.
ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>();
ForSourceType<DateTime?>().AddFormatter<StandardDateFormatter>();
I get no effect for my mapped class with this. It only works when I do the following:
Mapper.CreateMap<Member, MemberForm>().ForMember(x => x.DateOfBirth, y => y.AddFormatter<StandardDateFormatter>());
I am mapping DateTime? Member.DateOfBirth to string MemberForm.DateOfBirth. The formatter basically creates a short date string from the date.
Is there something I am missing when setting the default formatter for a given type?
Thanks
public class StandardDateFormatter : IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
if (context.SourceValue == null)
return null;
if (!(context.SourceValue is DateTime))
return context.SourceValue.ToNullSafeString();
return ((DateTime)context.SourceValue).ToShortDateString();
}
}
I had the same problem and found a fix. Try changing:
ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>();
To
Mapper.ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>();
FYI - AddFormatter method is obsolete in 3.0 version. You can use ConvertUsing instead:
Mapper.CreateMap<DateTime, string>()
.ConvertUsing<DateTimeCustomConverter>();
public class DateTimeCustomConverter : ITypeConverter<DateTime, string>
{
public string Convert(ResolutionContext context)
{
if (context.SourceValue == null)
return null;
if (!(context.SourceValue is DateTime))
return context.SourceValue.ToNullSafeString();
return ((DateTime)context.SourceValue).ToShortDateString();
}
}
I am using AutoMapper v1.
There is an abstract class there that does most of the grunt work called ValueFormatter.
My Code:
public class DateStringFormatter : ValueFormatter<DateTime>
{
protected override string FormatValueCore(DateTime value)
{
return value.ToString("dd MMM yyyy");
}
}
Then in my Profile class:
public sealed class ViewModelMapperProfile : Profile
{
...
protected override void Configure()
{
ForSourceType<DateTime>().AddFormatter<DateStringFormatter>();
CreateMap<dto, viewModel>()
.ForMember(dto => dto.DateSomething, opt => opt.MapFrom(src => src.DateFormatted));
}
I have an entity like so:
public class Land
{
public virtual IDictionary<string, int> Damages { get; set; }
// and other properties
}
Every time I try to use automapping with the following code:
var sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory)
.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Land>))
.BuildSessionFactory();
I get the following error:
{"The type or method has 2 generic parameter(s), but 1 generic argument(s) were
provided. A generic argument must be provided for each generic parameter."}
Can someone tell me what I'm doing wrong? Also, this is just a simple example. I have much more dictionaries than just this one.
It is impossible with NHibernate.
Found some traces that this isn't possible. Some traces, that it's recently implemented.
Still investigating. :)
This looks quite promising (didn't test yet).
So, in your case it should look like=>
public class LandMap : ClassMap<Land>
{
public LandMap()
{
(...)
HasMany(x => x.Damages)
.WithTableName("Damages")
.KeyColumnNames.Add("LandId")
.Cascade.All()
.AsMap<string>(
index => index.WithColumn("DamageType").WithType<string>(),
element => element.WithColumn("Amount").WithType<int>()
);
}
}
Keep in mind - it should. I didn't test it.
A possible workaround that should in theory work with automapping:
public class DamagesDictionary : Dictionary<string, int>
{
}
Land.cs
public class Land
{
public virtual DamagesDictionary Damages { get; set; }
// and other properties
}
or a more generic approach...
public class StringKeyedDictionary<T> : Dictionary<string, T>
{
}
Land.cs
public class Land
{
public virtual StringKeyedDictionary<int> Damages { get; set; }
// and other properties
}
I have been working on injecting AutoMapper into controllers. I like the implementation of Code Camp Server. It creates a wrapper around AutoMapper's IMappingEngine. The dependency injection is done using StructureMap. But I need to use Castle Windsor for my project. So, how do we implement the following dependency injection and set-up using Windsor? I am not looking for line-by-line equivalent implementation in Castle Windsor. If you want to do that, please feel free. Instead, what is Windsor equivalent of StructureMap's Registry and Profile? I need Profile to define CreateMap<> like the following.
Thanks.
Meeting controller:
public MeetingController(IMeetingMapper meetingMapper, ...)
Meeting Mapper:
public class MeetingMapper : IMeetingMapper
{
private readonly IMappingEngine _mappingEngine;
public MeetingMapper(IMappingEngine mappingEngine)
{
_mappingEngine = mappingEngine;
}
public MeetingInput Map(Meeting model)
{
return _mappingEngine.Map<Meeting, MeetingInput>(model);
}
......
}
Auto Mapper Registry:
public class AutoMapperRegistry : Registry
{
public AutoMapperRegistry()
{
ForRequestedType<IMappingEngine>().TheDefault.Is.ConstructedBy(() => Mapper.Engine);
}
}
Meeting Mapper Profile:
public class MeetingMapperProfile : Profile
{
public static Func<Type, object> CreateDependencyCallback = (type) => Activator.CreateInstance(type);
public T CreateDependency<T>()
{
return (T)CreateDependencyCallback(typeof(T));
}
protected override void Configure()
{
Mapper.CreateMap<MeetingInput, Meeting>().ConstructUsing(
input => CreateDependency<IMeetingRepository>().GetById(input.Id) ?? new Meeting())
.ForMember(x => x.UserGroup, o => o.MapFrom(x => x.UserGroupId))
.ForMember(x => x.Address, o => o.Ignore())
.ForMember(x => x.City, o => o.Ignore())
.ForMember(x => x.Region, o => o.Ignore())
.ForMember(x => x.PostalCode, o => o.Ignore())
.ForMember(x => x.ChangeAuditInfo, o => o.Ignore());
}
}
you mean how do you register it in Windsor?
you may have to register FactorySupportFacility fist... I have no way of checking at this moment.
container.AddFacility<FactorySupportFacility>();
and then
container.Register(Component.For<IMappingEngine>().UsingFactoryMethod(()=>
Mapper.Engine));
I'm not familiar with Castle Windsor but here is the StructureMap syntax. You would need to set up your registry a little different. Instead of setting the IMappingEngine to Mapper.Engine, you will have to do configure a few more interfaces. It's a little more work but this will allow you to set the profile as part of registration.
public AutoMapperRegistry()
{
//type mapping
For<ConfigurationStore>()
.Singleton()
.Use(ctx =>
{
ITypeMapFactory factory = ctx.GetInstance<ITypeMapFactory>();
ConfigurationStore store
= new ConfigurationStore(factory, MapperRegistry.AllMappers());
IConfiguration cfg = store;
//Here's where you load your profile
cfg.AddProfile<MeetingMapperProfile>();
store.AssertConfigurationIsValid();
return store;
});
For<IConfigurationProvider>().Use(ctx => ctx.GetInstance<ConfigurationStore>());
For<IConfiguration>().Use(ctx => ctx.GetInstance<ConfigurationStore>());
For<IMappingEngine>().Use<MappingEngine>();
For<ITypeMapFactory>().Use<TypeMapFactory>();
}
I realize this is a bit old, but I use Castle Windsor, and it's been fairly easy to get AutoMapper profiles loaded using an installer:
using System.Linq;
using System.Reflection;
using AutoMapper;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
namespace YourNamespace
{
public class AutoMapperInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
Mapper.Initialize(m => m.ConstructServicesUsing(container.Resolve));
container.Register(Types.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn<IValueResolver>());
container.Register(Types.FromThisAssembly().BasedOn<Profile>().WithServiceBase());
var profiles = container.ResolveAll<Profile>();
profiles.ToList().ForEach(p => Mapper.AddProfile(p));
container.Register(Component.For<IMappingEngine>().Instance(Mapper.Engine));
}
}
}
This installer would pick up the MeetingMapperProfile shown in the question, or any other class based on AutoMapper's Profile.