I have an MVC 5 site with adminlte template. It's work fine. Now I need pass to some data to _layout page. I need pass to _layout page same info as number of alert, number of email, alert list ecc. I read same document about BaseController where do this operation, i.e. read this data and put in a model, or create an abstract model and put this info into. But is not possibile create this model one time (i.e. on user login controller) and share it in all request without re-create it every controller call? Pratically, as a global singleton variabile.
Thanks.
Looks like a good usecase to use a ChildAction which can be called from the layout view.
So start by creating a view model to represent the data
public class AlertVm
{
public int EmailCount { set; get; }
public int NotificationCount { set; get; }
}
Now create an action method which creates an object of this, set the values and pass to a partial view
[ChildActionOnly]
public ActionResult Alerts()
{
var vm = new AlertVm {EmailCount = 4, NotificationCount = 2};
return PartialView(vm);
}
Now your Alerts.cshtml view, which is strongly typed to our view model, you can render whatever you want.
<div>
<p>#Model.EmailCount emails</p>
<p>#Model.NotificationCount notifications</p>
</div>
And this action method can be invoked from the _Layout.cshtml view.
<div>#Html.Action("Alerts", "Home")</div>
With this approach, you do not need worry about the creating a view model for every single action. (Ex : Your about page which does not need a view model usually)
Yeah you can create a base view model & make all the model inherit it
public class MyModel
{
public MyBaseClass BaseClass { get; set; }
}
public abstract class MyBaseClass
{
public virtual string MyName
{
get
{
return "MyBaseClass";
}
}
}
public class MyDerievedClass : MyBaseClass
{
public int MyProperty { get; set; }
public override string MyName
{
get
{
return "MyDerievedClass";
}
}
}
only problem is ..the default CreateModel process doesnt register this deafult view model so global.asax is where you tweek it ..
Here is a good explanation
Related
What is the best practice for placing business logic in message based design?
Im using servicestack for building my api.
The wiki shows the example of placing the RequiredRole Attribute on the message instead of the service handling it.
In a sense this [RequiredRole]/[Authenticate] is business logic/security attached to the message.
Concrete example
Say for example i would add DeleteAddress message:
public class DeleteAddress : IReturn<bool>
{
public int AddressId { get; set; }
}
But for this to be properly secure i want to check either Admin Role, permission to ManageAllAddresses or that the AddressId is linked to this user (maybe in session, maybe through a db call).
How would i best go about this?
Proposition
Is the following code the good practice and if so how would i implement it?
[RequiredRole("Admin")]
[RequiredPermission("ManageAllAddresses ")]
[RequiredAddressLinkedToAccount]
public class DeleteAddress : IReturn<bool>
{
public int AddressId { get; set; }
}
ServiceStack's recommendation is to keep your ServiceModel free of dependencies so we'd recommend to annotate your Service implementation classes instead which you can annotate either on the Service class to apply to all Operations or on the individual methods to apply just to that operation, e.g:
[RequiredRole("Admin")]
public class AddressServices : Service
{
[RequiredPermission("ManageAllAddresses ")]
[RequiredAddressLinkedToAccount]
public object Any(DeleteAddress request)
{
}
}
Please note ServiceStack requires your Services to return reference types, which is typically a Response DTO but can also be a string, e.g:
public class DeleteAddress : IReturn<string>
{
public int AddressId { get; set; }
}
To finish of this question. I could make a request filter and add it on the service.
Either inherit from AuthenticateAttribute or Directly from RequestFilterAttribute.
public class RequiredAddressLinkedToAccount : AuthenticateAttribute
{
public RequiredRoleAttribute(ApplyTo applyTo)
{
this.ApplyTo = applyTo;
this.Priority = (int)RequestFilterPriority.RequiredRole;
}
public override void Execute(IRequest req, IResponse res, object requestDto)
{
var dto = requestDto as ILinkedToAccount;
var session = req.GetSession();
if(dto.AccountId == session.Id)
return; //we dont want anything to be blocked if the account Id is there.
//Implement like RequireRoleAttribute
if (DoHtmlRedirectIfConfigured(req, res))
return;
res.StatusCode = (int)HttpStatusCode.Forbidden;
res.StatusDescription = "Address does not belong to you";
res.EndRequest();
}
}
What I am after is having a partial view that hosts a dropdown list of all available languages in the system. This partial view will be used in many edit templates and will be loaded from a separate controller.
Following the articles and information on the net I have the following implementation:
ViewModel
public class LanguagesViewModel
{
public int SelectedID { get; set; }
public virtual SelectList Languages { get; set; }
public LanguagesViewModel(int selectedID, IEnumerable<Language> languages)
{
SelectedID = selectedID;
Languages = new SelectList(languages, "LanguageId", "Name");
}
}
In the Shared folder I have a file: _LanguageDropDownList.cshtml with
#model XNETProductQuote.Web.Models.LanguagesViewModel
#Html.DropDownListFor(model => model.SelectedID, Model.Languages)
I have a LanguageController that has the following implementation
public ActionResult GetAllLanguages()
{
var languages = service.GetAll();
return PartialView("_LanguageDropDownList", new LanguagesViewModel(1, languages));
}
So the above is meant to load the drop down list in that partial view so I can use it in other templates.
From a template that is loaded from a different controller (ApplicationSetting) I call the partial view using:
#Html.Action("GetAllLanguages", "LanguageController")
This doesn't work. It throws an exception:
The controller for path '/ApplicationSetting/Edit/1' was not found or does not implement IController.
What is the correct implementation for such scenario?
Thanks
In Asp.Net MVC when we make a new controller then 'Controller' postfix is automatically attached to the Controller Name for ex:- in your case if you give 'Language' name to the controller then controller's complete name will be like 'LanguageController',so where ever you want to give controller name you have to use only 'Language' not 'LanguageController' and one of the overloads of #Html.Action() is ControllerName which is only 'Language' and not 'LanguageController' ,So in your problem just change #Html.Action("GetAllLanguages", "LanguageController") with #Html.Action("GetAllLanguages", "Language") and your problem will be solved.
BACKGROUND: I have a Person domain object. It is an aggregate root. I have included a portion of the class below.
I am exposing methods to perform the objects behaviors. For instance, to add a BankAccount I have the AddBankAccount() method. I have not included all the methods of the class but suffice to say that any public property must be updated using a method.
I am going to create an IPerson repository to handle the CRUD operations.
public interface IPersonRepository
{
void Save(Person p);
//...other methods
}
QUESTION: How do I tell the repository which fields need to be updated when we are updating an existing person? For example, If I add a bank account to an existing person how do I communicate this information to the repository when repository.Save() is called?
In the repository it is easy to determine when a new person is created, but when an existing person exists and you update fields on that person, i'm not sure how to communicate this to the repository.
I don't want to pollute my Person object with information about which fields are updated.
I could have separate methods on the repository like .UpdateEmail(), AddBankAccount() but that feels like overkill. I would like a simple .Save() method on the repository and it determines what needs to update in some manner.
How have others handled this situation?
I have searched the web and stackoverflow but haven't found anything. I must not be searching correctly because this seems like something simple when it comes to persistence within the DDD paradigm. I could also be way off on my understanding of DDD :-)
public class Person : DomainObject
{
public Person(int Id, string FirstName, string LastName,
string Name, string Email)
{
this.Id = Id;
this.CreditCards = new List<CreditCard>();
this.BankAccounts = new List<BankAccount>();
this.PhoneNumbers = new List<PhoneNumber>();
this.Sponsorships = new List<Sponsorship>();
}
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string Name{ get; private set; }
public string Email { get; private set; }
public string LoginName { get; private set; }
public ICollection<CreditCard> CreditCards { get; private set; }
public ICollection<BankAccount> BankAccounts { get; private set; }
public ICollection<PhoneNumber> PhoneNumbers { get; private set; }
public void AddBankAccount(BankAccount accountToAdd, IBankAccountValidator bankAccountValidator)
{
bankAccountValidator.Validate(accountToAdd);
this.BankAccounts.Add(accountToAdd);
}
public void AddCreditCard(CreditCard creditCardToAdd, ICreditCardValidator ccValidator)
{
ccValidator.Validate(creditCardToAdd);
this.CreditCards.Add(creditCardToAdd);
}
public void UpdateEmail(string NewEmail)
{
this.Email = NewEmail;
}
There is an example of Repository interface from S#arp Architecture project. It is similar to PoEAA Data Mapper because it used to CRUD operations also.
public interface IRepositoryWithTypedId<T, IdT>
{
T Get(IdT id);
IList<T> GetAll();
IList<T> FindAll(IDictionary<string, object> propertyValuePairs);
T FindOne(IDictionary<string, object> propertyValuePairs);
T SaveOrUpdate(T entity);
void Delete(T entity);
IDbContext DbContext { get; }
}
As you can see, there is no update method for specific properties of an entity. The whole entity is provided as an argument into the method SaveOrUpdate.
When properties of your domain entity are being updated you should tell your Unit of Work that entity is 'dirty' and should be saved into storage (e.g. database)
You should not pollute your Person object with information about updated fields but it is needed to track information if entity is updated.
There might be methods of the class DomainObject which tell 'Unit of Work' if entity is 'new', 'dirty' or 'deleted'. And then your UoW itself might invoke proper repository methods - 'SaveOrUpdate' or 'Delete'.
Despite the fact that modern ORM Frameworks like NHibernate or EntityFramework have their own implementations of 'Unit of Work', people tend to write their own wrappers/ abstractions for them.
What I'm doing to solve this problem, is adding an interface to my domain objects:
interface IDirtyTracker {
bool IsDirty {get;}
void MarkClean();
void MarkDirty();
}
The base DomainObject class could implement IDirtyTracker, and then repositories etc. could use IsDirty to check if it's dirty or clean.
In each setter that makes a change:
void SetValue() {
this._value = newValue;
this.MarkDirty();
}
This does not give you fine grain checking, but it's a simple way to avoid some unnecessary updates at the repository level.
To make this a little easier, a GetPropertiesToIncludeInDirtyCheck method could be added, which would retrieve a list of properties which need to be checked.
interface IDirtyTracker {
IENumerable<Object> GetPropertiesToIncludeInDirtyCheck();
}
I have a question in regarding aggregate roots, should they have the responsibility for deleting child objects or should that be up to the repository? What if I wanna query one file by its Id, should I then create a specific method for this in my repository?
Code snippet of my aggregate root:
public class Folder {
#region Properties
public Guid Id { get;set; }
public Name { get;set; }
public virtual ICollection<File> Files { get;set; }
#endregion
#region Methods
public File AddFile(string type, string title, bool share = false)
{
///
}
#endregion
}
File class:
public class File
{
#region Properties
public virtual Folder Folder { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public bool Shared { get; set; }
#endregion
#region Constructor
public File(Folder folder, string type, string title, bool share = false)
{
///
}
#endregion
}
Thanks
Aggregate root are responsible for domain invariants (see http://dddcommunity.org/library/vernon_2011/).
So the answer is yes, the aggregate root should be the only object that has access to the objects that it aggregates. This means that no other object should obtain a reference to a File and that File should not expose any method that change its own state.
All method that change the state of the child object should be exposed by the aggregate root itself, since it must ensure the aggregated invariants.
As to persisting the deletion, I usually model domain events as .NET events: such events are then subscribed by the Repository before returning the entity. Thus, in the event handler the persistence logic occurs (see http://epic.tesio.it/doc/manual/observable_entities.html for details)
This depends heavily on your context. If a file has its own lifecycle independent of the folder then you could make a File an entity/AR. This would, however, mean you need to break the instance aggregation relationship in the Folder so that it only has the reference to the File. Something like this:
public class Folder
{
public Guid Id { get;set; }
public string Name { get;set; }
public List<ContainedFile> Files { get;set; }
}
public class File
{
public Guid Id { get;set; }
public string Title { get;set; }
}
public class ContainedFile // or FolderFIle or whatever makes sense in your domain
{
public Guid FileId { get;set; }
}
Try to keep references to other AR instances out of an AR. Also, that bi-directional relationship (File.Folder) is not necessary. That is probably an indication that you are using your domain model for navigation :) --- try not to do that.
AggregateRoots should be responsible for their child objects. In the case of your example, imagine that the Folder exposes a Size property, which is determined from the sum of the size of Files.
long Size{get{return Files.Sum(f => f.Size);}
So when you're actually deleting the file, the folder would need to know about it.
You might not have the Size property now - but part of the purpose of following DDD is so that when you need to implement it it's easy and clean to do.
I have an aggregate named Campaigns every with a root entity named campaign, this root entity has a list of attempts (entity)
public class Attempts: IEntity<Attempts>
{
private int id;
public AttempNumber AttemptNumber {get;}
//other fields
}
public class Campaign: IEntity<Campaign> //root
{
private int id;
public IList<Attempt> {get;}
//other fields
}
Im using a method to add a campaign attempt
public virtual void AssignAttempts(Attempts att)
{
Validate.NotNull(att, "attemps are required for assignment");
this.attempts.add(att);
}
Problem comes when i try to edit a specific item in attempts list. I get Attempt by AttempNumber and pass it to editAttempt method but i dont know how to set the attempt without deleting whole list and recreate it again
public virtual void EditAttempts(Attempts att)
{
Validate.NotNull(att, "attemps are required for assignment");
}
Any help will be appreciated!
Thanks,
Pedro de la Cruz
First, I think there may be a slight problem with your domain model. It seems to me like 'Campaign' should be an aggregate root entity having a collection of 'Attempt' value objects (or entities). There is no 'Campaigns' aggregate unless you have a parent concept to a campaign which would contain a collection of campaigns. Also, there is no 'Attempts' entity. Instead a collection of 'Attempt' entities or values on the 'Campaign' entity. 'Attempt' may be an entity if it has identity outside of a 'Campaign', otherwise it is a value object. The code could be something like this:
class Campaign {
public string Id { get; set; }
public ICollection<Attempt> Attempts { get; private set; }
public Attempt GetAttempt(string id) {
return this.Attempts.FirstOrDefault(x => x.Number == id);
}
}
class Attempt {
public string Number { get; set; }
public string Attribute1 { get; set; }
}
If you retrieve an Attempt from the Campaign entity and then change some of the properties, you should not have to insert it back into the campaign entity, it is already there. This is how the code would look if you were using NHibernate (similar for other ORMs):
var campaign = this.Session.Get<Campaign>("some-id");
var attempt = campaign.GetAttempt("some-attempt-id");
attempt.Attribute1 = "some new value";
this.Session.Flush(); // will commit changes made to Attempt
You don't need an Edit method. Your code can modify the Attempts in-place, like so:
Attempt toModify = MyRepository.GetAttemptById(id);
toModify.Counter++;
toModify.Location = "Paris";
MyRepository.SaveChanges(); // to actually persist to the DB
Of course how you name the SaveChanges() is up to you, this is the way Entity Framework names its general Save method.