MVC 3: AutoMapper and Project/Solution Structure - asp.net-mvc-3

I've just started to use AutoMapper in my MVC 3 project and I'm wondering how people here structure their projects when using it. I've created a MapManager which simply has a SetupMaps method that I call in global.asax to create the initial map configurations. I also need to use a ValueResolver for one of my mappings. For me, this particular ValueResolver will be needed in a couple of different places and will simply return a value from Article.GenerateSlug.
So my questions are:
How do you manage the initial creation of all of your maps (Mapper.CreateMap)?
Where do you put the classes for your ValueResolvers in your project? Do you create subfolders under your Model folder or something else entirely?
Thanks for any help.

i won't speak to question 2 as its really personal preference, but for 1 i generally use one or more AutoMapper.Profile to hold all my Mapper.CreateMap for a specific purpose (domaintoviewmodel, etc).
public class ViewModelToDomainAutomapperProfile : Profile
{
public override string ProfileName
{
get
{
return "ViewModelToDomain";
}
}
protected override void Configure()
{
CreateMap<TripRegistrationViewModel, TripRegistration>()
.ForMember(x=>x.PingAttempts, y => y.Ignore())
.ForMember(x=>x.PingResponses, y => y.Ignore());
}
}
then i create a bootstrapper (IInitializer) that configures the Mapper, adding all of my profiles.
public class AutoMapperInitializer : IInitializer
{
public void Execute()
{
Mapper.Initialize(x =>
{
x.AddProfile<DomainToViewModelAutomapperProfile>();
x.AddProfile<ViewModelToDomainAutomapperProfile>();
});
}
}
then in my global.asax i get all instances of IInitializer and loop through them running Execute().
foreach (var initializer in ObjectFactory.GetAllInstances<IInitializer>())
{
initializer.Execute();
}
that's my general strategy.
by request, here is the reflection implementation of the final step.
var iInitializer = typeof(IInitializer);
List<IInitializer> initializers = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => iInitializer.IsAssignableFrom(p) && p.IsClass)
.Select(x => (IInitializer) Activator.CreateInstance(x)).ToList();
foreach (var initializer in initializers)
{
initializer.Execute();
}

Related

Property Injection for NRules within ABP

I am using ABP Boilerplate 6.0 and have integrated NRules with our platform.
I am able to get the rule below to work, but the issue is that I cannot use the injected ‘_questionResponseRepository’ within the rule condition, because only after the rule criteria are met, does the resolution of dependencies take place.
I would like to use ‘_questionResponseRepository’ to get from the database a list of keywords and use those words in the Rule match condition
The Calling code
public WasteManagementManager(
IRepository<WasteBirthCertificateBlock, long> wasteBirthCertificateBlockRepository,
IRepository<WasteBirthCertificateChain, long> wasteBirthCertificateChainRepository,
ISession nRuleSession,
ISessionFactory nRuleSessionFactory,
ILogger log
)
{
_wasteBirthCertificateBlockRepository = wasteBirthCertificateBlockRepository;
_wasteBirthCertificateChainRepository = wasteBirthCertificateChainRepository;
_nRuleSession = nRuleSession;
_nRuleSessionFactory = nRuleSessionFactory;
_log = log;
}
public void Trigger()
{==>
When I am in debug, _questionResponseRepository is NOT NUll. I'm trying inject it as a fact but that is not property injection .. I'm just trying one way or the other to get it working
_nRuleSession.Insert(_questionResponseRepository);
_nRuleSession.Fire();
}
The Rule code
namespace SagerSystems.AI.WasteManagements.NRules
{
[Name("Track Explosive Substances Rule")]
public class TrackExplosiveSubstancesRule : Rule
{
private string[] _listOfExplosiveKeyWords = new string[] { "H3O", "N2" };
public IRepository<QuestionResponse, long> _questionResponseRepository { get; set; }
public TrackExplosiveSubstancesRule()
{
**This does NOT work** (the value remains null)
Dependency()
.Resolve(() => _questionResponseRepository);
}
public override void Define()
{
*This does work* but only after the rule fires)
Dependency()
.Resolve(() => _questionResponseRepository);
When()
.Match(() => questionResponseDto, c => CheckKeyWord(c));
Then()
.Do(ctx => Console.WriteLine(“Test Condition Works”))
}
private bool CheckKeyWord(QuestionResponseDto questionResponseDto)
{
==> How do I user ‘questionResponseRepository’
var isKeyWord=
_listOfExplosiveKeyWords.Any(c => questionResponseDto.QuestionText.Contains(c));
return isKeyWord;
}
}
}
There are a few ways to use external information (in this case keywords from the DB) in the rule's match condition in NRules.
Inject the corresponding repository/service into the rule.
There are two ways to inject dependencies into rules. During instantiation of rule classes, or at rule's run time via Dependency.Resolve DSL. Since Dependency.Relsove, as you pointed out, can only be used on the right-hand side of the rule (actions), it's not suitable for this use case. But you can still inject the dependency into the rule during the rule's instantiation.
What you need to do here is to register the rule type itself with the container, implement an IRuleActivator to resolve rules via that container, and set the RuleRepository.RuleActivator when loading the rules. If both the repository and the rule are registered with the same container, the rule will get injected with the dependency (you can use either property or constructor injection, depending on how you registered the types). Then you can just use the dependency in the expressions.
I don't have all your code, so hypothetically, it would look something like below. Here I'm assuming there is a repository of a Keyword entity that can be used to fetch keywords from the DB. I'm also using constructor injection, but the same would work for property injection.
public class RuleActivator : IRuleActivator
{
private readonly IIocResolver _iocResolver;
public RuleActivator(IIocResolver iocResolver)
{
_iocResolver = iocResolver;
}
public IEnumerable<Rule> Activate(Type type)
{
yield return (Rule)_iocResolver.Resolve(type);
}
}
public class RulesEngineModule : AbpModule
{
public override void Initialize()
{
//Find rule classes
var scanner = new RuleTypeScanner();
scanner.AssemblyOf<TrackExplosiveSubstancesRule>();
var ruleTypes = scanner.GetRuleTypes();
//Register rule classes with the container
foreach (var ruleType in ruleTypes)
{
IocManager.Register(ruleType);
}
//Load rules into the repository; use a rule activator to resolve rules via the container
var repository = new RuleRepository {Activator = new RuleActivator(IocManager)};
repository.Load(x => x.From(s => s.Type(ruleTypes)));
//Compile rules into the factory
var factory = repository.Compile();
//Register session factory instance
IocManager.IocContainer.Register(
Component.For<ISessionFactory>().Instance(factory));
//Register session as a delegate that creates a new instance from a factory
IocManager.IocContainer.Register(
Component.For<ISession>().UsingFactoryMethod(k => k.Resolve<ISessionFactory>().CreateSession()).LifestyleTransient());
}
}
[Name("Track Explosive Substances Rule")]
public class TrackExplosiveSubstancesRule : Rule
{
private readonly IRepository<Keyword, long> _keywordRepository;
public TrackExplosiveSubstancesRule(IRepository<Keyword, long> keywordRepository)
{
_keywordRepository = keywordRepository;
}
public override void Define()
{
QuestionResponseDto questionResponseDto = default;
When()
.Match(() => questionResponseDto, c => ContainsKeyword(c));
Then()
.Do(ctx => Console.WriteLine("Test Condition Works"));
}
private bool ContainsKeyword(QuestionResponseDto questionResponseDto)
{
var keywords = _keywordRepository.GetAll().ToList();
var hasKeyWord = keywords.Any(keyword => questionResponseDto.QuestionText.Contains(keyword.Value));
return hasKeyWord;
}
}
Then somewhere in your Trigger method or somewhere in a corresponding controller:
var dto = new QuestionResponseDto(...);
_session.Insert(dto);
_session.Fire();
Retrieve keywords from the repository outside of the rules engine, and insert them into the session as facts. This is actually preferred, because you would have more control over the interactions with the external data. Also, you can put keywords into a data structure with efficient lookup performance (e.g. a trie).
In this case you don't need a rule activator or to register the rules with the container, like with the option #1, since all inputs come to the rule as facts, so there are actually no external dependencies.
For example:
public class KeywordSet
{
private readonly Keyword[] _keywords;
public KeywordSet(IEnumerable<Keyword> keywords)
{
_keywords = keywords.ToArray();
}
public bool ContainsAny(string value)
{
return _keywords.Any(keyword => value.Contains(keyword.Value));
}
}
[Name("Track Explosive Substances Rule")]
public class TrackExplosiveSubstancesRule : Rule
{
public override void Define()
{
KeywordSet keywordSet = default;
QuestionResponseDto questionResponseDto = default;
When()
.Match(() => keywordSet)
.Match(() => questionResponseDto, c => keywordSet.ContainsAny(c.QuestionText));
Then()
.Do(ctx => Console.WriteLine("Test Condition Works"));
}
}
Then somewhere in your Trigger method or somewhere in a corresponding controller:
var keywords = _keywordRepository.GetAll().ToList();
var keywordSet = new KeywordSet(keywords);
_session.Insert(keywordSet);
var dto = new QuestionResponseDto(...);
_session.Insert(dto);
_session.Fire();
You can also insert the repository itself as a fact, and then match it in the rule, but I would not recommend doing this, so I would stick to either the option #1 or #2.

Automatically switching views for AMP in ASP.NET MVC

I want to create and AMP version of my website in ASP.NET MVC using .NET Core 2.0. Previously I had done some work with DisplayModeProvider instances in tha past on .Net framework, but that does not seem to be an option in .NET Core.
What I want to be able to do is alter the view names to be index.amp.cshtml rather than index.cshtml when my URL starts iwth /amp. What's the best way to achieve this in .NET Core?
You can do something like this using IViewLocationExpander. As it happens, I was playing with this a few days ago so I have some code samples to hand. If you create something like this:
public class AmpViewLocationExpander : IViewLocationExpander
{
public void PopulateValues(ViewLocationExpanderContext context)
{
var contains = context.ActionContext.HttpContext.Request.Query.ContainsKey("amp");
context.Values.Add("AmpKey", contains.ToString());
var containsStem = context.ActionContext.HttpContext.Request.Path.StartsWithSegments("/amp");
context.Values.Add("AmpStem", containsStem.ToString());
}
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
if (!(context.ActionContext.ActionDescriptor is ControllerActionDescriptor descriptor)) { return viewLocations; }
if (context.ActionContext.HttpContext.Request.Query.ContainsKey("amp")
|| context.ActionContext.HttpContext.Request.Path.StartsWithSegments("/amp")
)
{
return viewLocations.Select(x => x.Replace("{0}", "{0}.amp"));
}
return viewLocations;
}
}
iViewLocationExpander can be found in Microsoft.AspNetCore.Mvc.Razor
Then in your Configure Services method in Startup.cs, add the following:
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new AmpViewLocationExtender());
});
What this will do is update the view locations per request to insert .amp before .cshtml any time the URL either starts with /amp or there is a query string key of amp. If your AMP views don't exist, it might blow-up a little, I've not fully tested it, but it should get you started.
You can define this Middleware :
public class AmpMiddleware
{
private RequestDelegate _next;
public AmpMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext context)
{
const string ampTag = "/amp";
var path = context.Request.Path;
if (path.HasValue)
{
var ampPos = path.Value.IndexOf(ampTag);
if (ampPos >= 0)
{
context.Request.Path = new PathString(path.Value.Remove(ampPos, ampTag.Length));
context.Items.Add("amp", "true");
}
}
return _next(context);
}
}
public static class BuilderExtensions
{
public static IApplicationBuilder UseAmpMiddleware(this IApplicationBuilder app)
{
return app.UseMiddleware<AmpMiddleware>();
}
}
And call it in Startup:
app.UseAmpMiddleware();
Then can check in page and simple set another layout or limit some code, in his way no need to create separate page for amp version:
#if (HttpContext.Items.ContainsKey("amp"))
{
<b>Request AMP Version</b>
}

How can I ensure a consistent url "interface" between different controllers in an Asp.net MVC project?

Interfaces in C# are great to ensure my controllers' action methods take the same number, data type and order of parameters. Unfortunately, that doesn't help keep the urls generated by the routing mechanism consistent. How can I ensure the names of parameters are the same?
For example:
How do I ensure that
sportswear/products
and
carsandtrucks/products
both take the parameter productId?
I'd like to try to avoid a lot of routes in global.asax.cs, as I feel that they're not intuitive, but I'm open to ideas.
I'd suggest that the best option would be a unit test that finds all implementations of your controller interfaces and ensures that the implementation parameter names match those on the interface.
So something like
public interface IController
{
ActionResult GetProducts(string productId);
}
[TestFixture]
public class IControllerTest
{
[Test]
public void EnsureImplementationsUseCorrectParameterNames()
{
// Assuming all required assemblies have been loaded
var implementations = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes().Where(type => typeof(IController).IsAssignableFrom(type)));
var interfaceMethods = typeof(IController).GetMethods().ToList();
foreach (var implementation in implementations)
{
var methodPairs = interfaceMethods.Join(implementation.GetMethods(), mi => mi.ToString(), mi => mi.ToString(), (inner, outer) => new { InterfaceMethod = inner, ImplementationMethod = outer });
foreach (var methodPair in methodPairs)
{
using (var interfaceParameters = methodPair.InterfaceMethod.GetParameters().Cast<ParameterInfo>().GetEnumerator())
using (var implementationParameters = methodPair.ImplementationMethod.GetParameters().Cast<ParameterInfo>().GetEnumerator())
{
while (interfaceParameters.MoveNext() && implementationParameters.MoveNext())
{
Assert.AreEqual(interfaceParameters.Current.Name, implementationParameters.Current.Name);
}
}
}
}
}
}
Hope this helps.

Moq testing LINQ Where queries

I'm using EF 4.1 to build a domain model. I have a Task class with a Validate(string userCode) method and in it I want to ensure the user code maps to a valid user in the database, so:
public static bool Validate(string userCode)
{
IDbSet<User> users = db.Set<User>();
var results = from u in users
where u.UserCode.Equals(userCode)
select u;
return results.FirstOrDefault() != null;
}
I can use Moq to mock IDbSet no problem. But ran into trouble with the Where call:
User user = new User { UserCode = "abc" };
IList<User> list = new List<User> { user };
var users = new Mock<IDbSet<User>>();
users.Setup(x => x.Where(It.IsAny<Expression<Func<User, bool>>>())).Returns(list.AsQueryable);
Initialization method JLTi.iRIS3.Tests.TaskTest.SetUp threw exception.
System.NotSupportedException: System.NotSupportedException: Expression
references a method that does not belong to the mocked object:
x => x.Where<User>(It.IsAny<Expression`1>()).
Other than creating a level of indirection (eg, using a ServiceLocator to get an object that runs the LINQ and then mock that method) I can't think of how else to test this, but I want to make sure there is no way before I introduce another layer. And I can see this kind of LINQ queries will be needed quite often so the service objects can quickly spiral out of control.
Could some kind soul help? Thanks!
There is an article on MSDN highlighting how to mock using moq:
The gist of it is to represent linq to entities operations with linq to objects.
var mockSet = new Mock<DbSet<Blog>>();
mockSet.As<IQueryable<Blog>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Blog>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Blog>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Blog>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
As Ladislav points out there are disadvantages to this as Linq To Objects is simply different to Linq to Entities so it may result in false positives. But it now being an MSDN article it does point that it is at least possible and perhaps recommended in some cases?
One thing that may of changed since the original answers to this post is that the Entity Framework team have opened up areas of Entity Framework in EF 6.0 to make it easier to mock it's inners.
Although I have not tried this, because IDBSet implements IEnumerable you might have to mock the enumerator method so the linq statements will pick up your list of users. You don't actually want to mock linq but by the looks of your code you want to test whether you are finding the right user based on the UserCode which I think is a valid unit test.
var user = new User { UserCode = "abc" };
var list = new List<User> { user };
var users = new Mock<IDbSet<User>>();
users.Setup(x => x.GetEnumerator()).Returns(list.GetEnumerator());
You might get a conflict with the non-generic version of the GetEnumerator but it this might help you on the right track.
Then you have to then place the mocked object on the data context which depends on other code that we don't see.
As I know Moq is able to set up only virtual methods of mocked object itself but you are trying to set up extensions (static) method - no way! These methods are absolutely outside of your mock scope.
Moreover that code is hard to test and requires too much initialization to be able to test it. Use this instead:
internal virtual IQueryable<User> GetUserSet()
{
return db.Set<User>();
}
public bool Validate(string userCode)
{
IQueryable<User> users = GetUserSet();
var results = from u in users
where u.UserCode.Equals(userCode)
select u;
return results.FirstOrDefault() != null;
}
You will just need to set up GetUserSet to return your list. Such testing has some major issues:
You are not testing the real implementation - in case of EF mocking sets is stupid approach because once you do it you change linq-to-entities to linq-to-objects. Those two are totally different and linq-to-entities is only small subset of linq-to-objects = your unit tests can pass with linq-to-objects but your code will fail at runtime.
Once you use this approach you cannot use Include because include is dependent on DbQuery / DbSet. Again you need integration test to use it.
This doesn't test that your lazy loading works
The better approach is removing your linq queries from Validate method - just call them as another virtual method of the object. Unit test your Validate method with mocked query methods and use integration tests to test queries themselves.
I found it easier just to write the stub:
internal class FakeDbSet<T> : IDbSet<T>where T : class
{
readonly HashSet<T> _data;
readonly IQueryable _query;
public FakeDbSet()
{
_data = new HashSet<T>();
_query = _data.AsQueryable();
}
public virtual T Find(params object[] keyValues)
{
throw new NotImplementedException("Derive from FakeDbSet<T> and override Find");
}
public T Add(T item)
{
_data.Add(item);
return item;
}
public T Remove(T item)
{
_data.Remove(item);
return item;
}
public T Attach(T item)
{
_data.Add(item);
return item;
}
public void Detach(T item)
{
_data.Remove(item);
}
Type IQueryable.ElementType
{
get { return _query.ElementType; }
}
Expression IQueryable.Expression
{
get { return _query.Expression; }
}
IQueryProvider IQueryable.Provider
{
get { return _query.Provider; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return _data.GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return _data.GetEnumerator();
}
public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T
{
return Activator.CreateInstance<TDerivedEntity>();
}
public T Create()
{
return Activator.CreateInstance<T>();
}
public ObservableCollection<T> Local
{
get
{
return new ObservableCollection<T>(_data);
}
}

Ninject generic bindings

I am building an application using Ninject and ASP.NET MVC 3.
Is it possible with Ninject to supply a generic binding within a module like this:
Bind(typeof(IRepository<>)).To(typeof(SomeConcreteRepository<>));
EDIT:
And then for a specific type , create a class that inherits from SomeConcreteRepository:
Bind(typeof(IRepository<Person>)).To(typeof(PersonConcreteRepository));
This throws an exception that multiple bindings are available. However, is there another approach to this? Are there other DI frameworks for .NET which support this behavior?
You don't need the second line. Simply register the open generic types:
kernel.Bind(typeof(IRepository<>)).To(typeof(SomeConcreteRepository<>));
and later fetch a specific repository like this:
var repo = kernel.Get<IRepository<Person>>();
or you can also use a provider.
A bit of a nasty fix but for the scenario at hand it works:
public class MyKernel: StandardKernel
{
public MyKernel(params INinjectModule[] modules) : base(modules) { }
public MyKernel(INinjectSettings settings, params INinjectModule[] modules) : base(settings, modules) { }
public override IEnumerable<IBinding> GetBindings(Type service)
{
var bindings = base.GetBindings(service);
if (bindings.Count() > 1)
{
bindings = bindings.Where(c => !c.Service.IsGenericTypeDefinition);
}
return bindings;
}
}
public class ExtendedNinjectKernal : StandardKernel
{
public ExtendedNinjectKernal(params INinjectModule[] modules) : base(modules) { }
public ExtendedNinjectKernal(INinjectSettings settings, params INinjectModule[] modules) : base(settings, modules) { }
public override IEnumerable<IBinding> GetBindings(Type service)
{
var bindings = base.GetBindings(service);
//If there are multiple bindings, select the one where the service does not have generic parameters
if (bindings.Count() > 1 && bindings.Any(a => !a.Service.IsGenericTypeDefinition))
bindings = bindings.Where(c => !c.Service.IsGenericTypeDefinition);
return bindings;
}
}

Resources