DRY principle with controller methods - c#

I have this logic in several methods in my MVC5 project. It works but I am repeating myself pretty consistently.
private PersonnelManagementEntities db = new PersonnelManagementEntities();
private ActiveDirectoryTools adt = new ActiveDirectoryTools();
private ManagerService ms = new ManagerService();
private UserService us = new UserService();
private CompanyService cs = new CompanyService();
public ActionResult CompanySummary(int id = 0)
{
//Repeating logic begins here
int setID = 0;
adt.GetUserInfo(User.Identity.Name);
//Fetch current users company
User currentUser = us.getUser(adt.adUserName);
//Determine if user is a global manager or not. If global display all companies
ViewBag.Company = cs.FetchCompanies(currentUser);
//You can only see the companies you're assigned to, in the AllRequests window. Unless manually overwritten in the URL
if (currentUser.GlobalUser == true && id > 0)
{
setID = id;
}
else
{
setID = (int)currentUser.CompanyID;
}
//End of repeating logic
var resultSet = db.UserTimeSummaryUpdated(setID);
return View(resultSet.ToList());
}
What do you guys think would be the best way of reducing the amount of times I repeat this?
You can see here in another method where I reuse this code:
public ActionResult AllRequests(int id = 0)
{
int setID = 0;
adt.GetUserInfo(User.Identity.Name);
User currentUser = us.getUser(adt.adUserName);
ViewBag.Company = cs.FetchCompanies(currentUser);
//You can only see the companies you're assigned to, in the AllRequests window. Unless manually overwritten in the URL
if (id > 0)
{
setID = id;
}
else
{
setID = (int)currentUser.CompanyID;
}
ViewBag.EmployeeList = db.Users
.Where(x => x.disabled == false)
.Where(x => x.CompanyID == setID)
.OrderBy(x => x.FullName)
.ToList();
IQueryable timeRequests = db.TimeRequests
.Include(t => t.ApproveDenyReason)
.Include(t => t.DayType)
.Include(t => t.User)
.Include(t => t.User1)
.Include(t => t.User2)
.OrderByDescending(t => t.sDateTime)
.Where(t => t.User.CompanyID == setID);
return View(timeRequests);
}
I was thinking about creating an ActionFilter and doing it that way but it seems kind of a hack instead of the correct way of doing things.
I also entertained the idea of when a user logs in I create a user object and persist it through a session.
Any help is appreciated

One option is to write a CustomController that inherits Controller. I did this to add Member Session Data and a Messaging Output System that can write to my LayoutView. For the example below I assumed FetchCompanies returns a list...
public class CustomController : Controller
{
private ActiveDirectoryTools _adt = new ActiveDirectoryTools();
private UserService _us = new UserService();
private CompanyService _cs = new CompanyService();
public List<Company> UserCompanies;
public ApplicationController()
: base()
{
_adt.GetUserInfo(User.Identity.Name);
User currentUser = _us.getUser(adt.adUserName);
UserCompanies = _cs.FetchCompanies(currentUser);
}
}
When you create your Controller inherit from this CustomController. Then in your ActionResult simply set using UserCompanies.
public AccountController:CustomController
{
public ActionResult Index()
{
ViewBag.Company = UserCompanies;
return View();
}
}

Related

ASP.NET Core web api serialize an object's List type property

After the server returned the object, the List type field went missing.
Each EmployeeViewModel has a list of EmployeeContactViewModel objects. The intended logic is, after querying the employee object from database, populate the view model in a method, including the list, and return to client.
The view model:
public class EmployeeViewModel
{
public List<EmployeeContactViewModel> EmployeeContacts;
public EmployeeViewModel()
{
EmployeeContacts = new List<EmployeeContactViewModel>();
}
public string EmployeeId { get; set; }
public string EmployeeName { get; set; }
// more fields
}
The method to populate view models:
public EmployeeViewModel GetViewModelFromEmpObject()
{
var vm = new EmployeeViewModel();
var contact1 = this.CONTACTs.Where(e => e.ContactId == 1).FirstOrDefault();
if (contact1 != null)
{
var contactVm1 = new EmployeeContactViewModel();
CopyContactFields(contact1, contactVm1); // method to populate view-model
vm.EmployeeContacts.Add(contactVm1);
}
else
vm.EmployeeContacts.Add(new EmployeeContactViewModel());
var contact2 = this.CONTACTs.Where(e => e.ContactId == 2).FirstOrDefault();
if (contact2 != null)
{
var contactVm2 = new EmployeeContactViewModel();
CopyContactFields(contact1, contactVm2); // method to populate view-model
vm.EmployeeContacts.Add(contactVm2);
}
else
vm.EmployeeContacts.Add(new EmployeeContactViewModel());
// more lines below
}
The api controller method is below. By setting a breakpoint at the return line, I could see the EmployeeContactViewModel list was correctly created.
[HttpGet("{empid:string}")]
public async Task<EmployeeViewModel> GetAsync(string empid)
{
Employee emp = await _context.Employees
.Include(c => c.CONTACTs)
.Where(c => c.EmployeeId == empid)
.FirstOrDefaultAsync();
var viewModel = emp.GetViewModelFromEmpObject();
return viewModel;
}
However on the client side, although all the other fields (EmployeeId, EmployeeName, etc.) were present in the response, there was nothing for EmployeeContacts list, when inspecting the server response in the browser network tab.
I tried to search before asking but using "Serialize" as a keyword would just get topics on how to do serialization. Any advice is appreciated.
Do you mean that EmployeeContacts is not included in the response result?
You can install Microsoft.AspNetCore.Mvc.NewtonsoftJson, and add this line in Program.cs:
builder.Services.AddMvc().AddNewtonsoftJson(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

Best way to find values not in two lists c#

I have two lists which I need to compare (carOptions and custOptions).
Both of these lists are in my Customer class like below:
public class CustomerDTO
{
public int CustomerId { get; set; }
//other props removed for brevity
public List<OptionDTO> SelectedCarOptions { get; set; }
public List<OptionDTO> SelectedCustomerOptions { get; set; }
}
var existingData = _myRepository.GetDataByCustomer(customerId, year);
var existingCarOptions = existingData.Select(f => f.SelectedCarOptions);
var existingCustomerOptions = existingData.Select(f => f.SelectedCustomerOptions);
existingData is an IEnumerable of CustomerDTO and then existingCarOptions and existingCustomerOptions is an IEnumerable<List<OptionDTO>>
In the method, I have a list of IEnumerable<OptionDTO> options that gets passed in. I then break this down into car or customer based on the Enum as below:
var newCarOptions = options.Where(o => o.OptionTypeID == OptionType.CarOptions);
var newCustomerOptions = options.Where(o => o.OptionTypeID == OptionType.CustomerOptions).ToList();
What I need to do is find which options are in one collection but no in the other.
I tried as below but getting an Error on the Except (I maybe need to create my own static method in that class) but I am not sure this is the best approach really?
if (existingCarOptions.Count() != newCarOptions.Count())
{
//var test = newCarOptions.Except(existingCarOptions);
}
if (existingCustomerOptions.Count() != newCustomerOptions.Count())
{
//var test2 = newCustomerOptions.Except(existingCustomerOptions);
}
Is it also quite a bit of code in the method - I could split it out into sperate methods if required but perhaps there is a simpler way I could achieve this?
I'm assuming OptionDTO has a property called Id, which uniquely identifies an option (you have to change the code accordingly if this is not the case), you may use HashSets to quickly find unmatched OptionsDTOs, while keeping the overall time cost O(n) (where n is the max number of combined options).
Create the existing options sets:
var existingCarOptions = existingData.SelectMany(d => d.SelectedCarOptions).Select(o => o.Id);
var existingCustomerOptions = existingData.SelectMany(d => d.SelectedCustomerOptions).Select(o => o.Id);
var existingCarOptionsIds = new HashSet<int>(existingCarOptions);
var existingCustomerOptionsIds = new HashSet<int>(existingCustomerOptions );
Then you extract options missing in existing sets with:
var unmatchedCarOptions = newCarOptions.Where(o => !existingCarOptionsIds.Contains(o.Id));
var unmatchedCustomerOptions = newCustomerOptions.Where(o => !existingCustomerOptionsIds.Contains(o.Id));
If you want to compare two classes you can use IEqualityComparer
public class OptionComparer : IEqualityComparer<OptionDTO>
{
public bool Equals(OptionDTO x, OptionDTO y)
{
if (object.ReferenceEquals(x, y))
{
return true;
}
if (object.ReferenceEquals(x, null) ||
object.ReferenceEquals(y, null))
{
return false;
}
return x.OptionTypeID == y.OptionTypeID ;
}
public int GetHashCode(OptionDTO obj)
{
if (obj == null)
{
return 0;
}
return obj.OptionTypeID.GetHashCode();
}
With using this you can ıdentify that What is the concept of equality for these classes.
Now we can find different values..
public List<OptionDTO>CalculateDiffBetweenLists(List<OptionDTO> left, List<OptionDTO> right){
List<OptionDTO> optionDiff;
optionDiff = left.Except(right, new OptionComparer ()).ToList();
return optionDiff ;
}

How to configure AutoFixture to use an enum value as seed when creating many of a certain type?

I have the following types:
public enum Status
{
Online,
Offline
}
public class User
{
private readonly Status _status;
public User(Status status) { _status = status; }
public Status Status {get {return _status; }}
public string Name {get;set;}
}
Now, when executing fixture.CreateMany<User> I want AutoFixture to return two Users, one per status. All other properties - like Name - should be filled with anonymous data.
Question:
How to configure AutoFixture to do this?
I tried the following this:
Register collection that news up the User object:
fixture.Register(
() => Enum.GetValues(typeof(Status)).Cast<Status>().Select(s =>
new User(s)));
The problem with this approach is that AutoFixture doesn't fill the other properties like Name
Customize User to use a factory and register a collection that uses fixture.Create:
f.Customize<User>(c => c.FromFactory((Status s) => new User(s)));
f.Register(() =>
Enum.GetValues(typeof(Status))
.Cast<Status>()
.Select(s => (User)f.Create(new SeededRequest(typeof(User), s),
new SpecimenContext(f))));
That didn't work either. The seed isn't being used.
You could do this:
var users = new Fixture().Create<Generator<User>>();
var onlineUser = users.Where(u => u.Status == Status.Online).First();
var offlineUser = users.Where(u => u.Status == Status.Offline).First();
If you're using AutoFixture.Xunit, the declarative equivalent is:
[Theory, AutoData]
public void CreateOneOfEachDeclaratively(Generator<User> users)
{
var onlineUser = users.Where(u => u.Status == Status.Online).First();
var offlineUser = users.Where(u => u.Status == Status.Offline).First();
// Use onlineUser and offlineUser here...
}
You may declare and use a customization, e.g. StatusGenerator:
var fixture = new Fixture();
fixture.RepeatCount = 2;
fixture.Customizations.Add(new StatusGenerator());
var result = fixture.CreateMany<User>();
A hypothetical implementation of the StatusGenerator could be the following:
internal class StatusGenerator : ISpecimenBuilder
{
private readonly Status[] values;
private int i;
internal StatusGenerator()
{
this.values =
Enum.GetValues(typeof(Status)).Cast<Status>().ToArray();
}
public object Create(object request, ISpecimenContext context)
{
var pi = request as ParameterInfo;
if (pi == null || !pi.ParameterType.IsEnum)
return new NoSpecimen(request);
return this.values[i == this.values.Length - 1 ? i = 0 : ++i];
}
}
Based on Mark's answer, this is what I am using now:
fixture.Customize<User>(c => c.Without(x => x.Status));
fixture.Customize<IEnumerable<User>>(
c =>
c.FromFactory(
() => Enum.GetValues(typeof(Status)).Cast<Status>()
.Select(s => users.First(u => u.Status == s))));
fixture.Create<IEnumerable<User>>(); // returns two Users
I know it is already answered and the Generator was a very interesting finding.
I think there is a much simpler approach for this problem.
var numberOfEnumValues = Enum.GetValues(typeof(Status)).Length;
var users = fixture.CreateMany<User>(numberOfEnumValues);
In case the constructor is more complicated, with multiple Status values, or the model has property setters of Status type. Then you generally have a problem, and the generator might blow as well.
Say that:
public class SuperUser : User
{
public SuperUser(Status status, Status shownStatus): base(status)
{
}
}
Then this will never be evaluated:
var users = fixture.Create<Generator<SuperUser>>();
var offlineUser = users.Where(u => u.Status == Status.Offline).First();
Current way of doing this with AutoFixture 4.17.0
fixture
.Build<User>
.With(u => u.Status, Status.Offline)
.CreateMany(5)
.ToList();

MVC4 - Make a value global to whole project

I am using database with multiple language support. Now the problem is that I need to enter my language into query in order to get information and it is fine, but what would be optimal way to store that information.
On client side it will be stored in cookies, of course. Now only way I can think of is making global variable on class and then use it in my functions. Is that only way?
Example code
private string lang = Infrastructure.UserSettings.Language(); // I don't have this implemented yet
[HttpGet]
public dynamic List()
{
string lang = "English"; // That's why I set it here manually for testing
var items = _db.Items.OrderByDescending(x => x.ID).Select(x => new
{
ID = x.ID,
Price = x.Price,
Name = x.ItemTranslations.Where(y => y.Language.Name == lang).Select(y => y.Name).SingleOrDefault(),
Category = new {
ID = x.Category.ID,
Name = x.Category.CategoryTranslations.Where(y => y.Language.Name == lang).Select(y => y.Name).SingleOrDefault()
}
});
return items;
}
My question: Is this good way of doing this or there is some more optimal way?
You could make a base controller with a read-only variable, like so:
public class BaseController : Controller
{
public string UserLanguage
{
get
{
var cLanguage = HttpContext.Request.Cookies["lang"];
if (cLanguage != null)
return cLanguage.Value;
else
return "English";
}
}
}
Then inherit your base controller, like so:
public class HomeController : BaseController
Then access your variable like so:
var items = _db.Items.OrderByDescending(x => x.ID).Select(x => new
{
ID = x.ID,
Price = x.Price,
Name = x.ItemTranslations.Where(y => y.Language.Name == UserLanguage).Select(y => y.Name).SingleOrDefault(),
Category = new {
ID = x.Category.ID,
Name = x.Category.CategoryTranslations.Where(y => y.Language.Name == lang).Select(y => y.Name).SingleOrDefault()
}
});
You would just need to set the cookie at a particular time.
A cookie is sent to the server on every page request. If the setting is available in the cookie, just read the cookie when you need to do a query. There is no performance overhead for reading a cookie that is already present.

How to verify multiple method calls with Moq

So the scenario is this: a user does some action (like earn a badge or unlock something) and an email notification gets sent out. One to the user (with a message like "You've unlocked XYZ...") and then a different message to each of their friends like ("You're friend has unlocked XYZ...").
public interface INotify
{
void Notify(User user, User friend);
}
public class NotificationService
{
private IEnumerable<INotify> _notifiers;
public NotificationService(IEnumerable<INotify> notifiers)
{
_notifiers = notifiers;
}
public SendNotifications()
{
User user = GetUser();
IEnumerable<User> friends = GetFriends();
foreach(var notifier in _notifiers)
{
//Send notification to user
notifier.Notify(user, null);
//send notification to users friends
foreach(var friend in friends)
notifier.Notify(user, friend);
}
}
}
I'm trying to use moq to test that each notifier is called 2x. Once passing null as the second parameter and the second time passing in a value to both parameters.
[Test]
public void MakeSureEveryoneIsNotified()
{
var notifierMock = new Mock<INotifier>();
var svc = new NotificationService(new List<INotifier>{ notifierMock.Object });
svc.SendNotifications();
notifierMock.Verify(x => x.Notify(It.Is<User>(user => user.UserId == 1), null), Times.Once());
notifierMock.Verify(x => x.Notify(It.Is<User>(user => user.UserId == 1), It.Is<User>(user => user.UserId == 2)), Times.Once());
}
The problem is that the second verify call throws an ArgumentNullException for the second parameter. Is there away to say "Check the first call has these parameters, and then the second call has other parameters". I know I can get it around it simply by calling:
notifierMock.Verify(x => x.Notify(It.IsAny<User>(), It.IsAny<User>()), Times.Exactly(2));
But I was wanting to be a little more specific. Anyway to do this?
You can achieve this by recording what happens on each call to Notify. Then you can compare the recording to what's expected:
[TestMethod]
public void TestMoqInvocations()
{
var notifierMock = new Mock<INotifier>();
var svc = new NotificationService(new List<INotifier>{ notifierMock.Object });
svc.SendNotifications();
var invocations = new List<NotifyParams>();
notifierMock
.Setup(f => f.Notify(It.IsAny<User>(), It.IsAny<User>()))
.Callback<string, string>((user, friend) => invocations.Add(new NotifyParams{user = user, friend = friend}));
Assert.AreEqual(1, invocations[0].user.UserId);
Assert.IsNull(invocations[0].friend);
Assert.AreEqual(1, invocations[1].user.UserId);
Assert.AreEqual(2, invocations[1].user.UserId);
}
public struct NotifyParams {
public User user {get;set;}
public User friend { get; set; }
}
You can create a method for get User list. Then get user by It.Is method.
private bool GetUser(User user, List<User> users)
{
if (user != null)
users.Add(user);
return true;
}
[Test]
public void MakeSureEveryoneIsNotified()
{
var notifierMock = new Mock<INotifier>();
var svc = new NotificationService(new List<INotifier>{ notifierMock.Object });
svc.SendNotifications();
var users = new List<User>();
var friends = new List<User>();
// verify how many times call the method
notifierMock
.Verify(x => x.Notify(
It.Is<User>(u => GetUser(u, users)),
It.Is<User>(f => GetFriend(f, friends))
), Times.Exactly(2));
Assert.AreEquals(2, users.Count);
Assert.AreEquals(1, users[0].UserId);
Assert.AreEquals(1, users[1].UserId);
Assert.AreEquals(1, friends.Count);
Assert.AreEquals(2, friends[0].UserId);
}

Categories

Resources