I am trying to use the IOC pattern and could not find the perfect way of implementing the state management using the same. Would be great if someone can help me with the same. Thank you.
What I would do is first create an Interface containing all the properties you would need to store in the Session (i.e.: the context):
using ProjectName.Core.Domain;
namespace ProjectName.Core.Interfaces
{
public interface IProjectNameSessionContext
{
string StringProperty1 { get; set; }
bool BoolProperty1 { get; set; }
ProjectName.Core.Domain.Entity1 DomainEntity1 { get; set; }
}
}
Then create a class that implements this interface, mark it as serializable:
using ProjectName.Core.Interfaces;
namespace ProjectName.Front.SessionData
{
[Serializable]
public class ProjectNameSessionContext : IProjectNameSessionContext
{
public string StringProperty1 { get; set; }
public bool BoolProperty1 { get; set; }
public ProjectName.Core.Domain.Entity1 DomainEntity1 { get; set; }
}
}
And finally tell your IOC to bind the interface with the class at runtime an instantiate an object in HttpSession context.
It looks like this with StrucureMap:
For<Core.Interfaces.IProjectNameSessionContext>().LifecycleIs(new HttpSessionLifecycle()).Use<ProjectNameSessionContext>();
Hope that helps!
Related
I struggle to understand why i can't cast an interface that came in the message to a concrete implementation of it. The worst thing is that it worked before. but i'm unable to backtrack to the change that broke it. Maybe someone could give me a clue what's wrong.
We have a service XXX that shares contracts with other services, XXX.Contracts:
public interface IStuffUpdated : IEvent
{
InvalidationReason Reason { get; }
IStuffDto AffectedStuff { get; }
}
IStuffDto is something like this:
public interface IStuffDto
{
Guid Id { get; set; }
string Name { get; set; }
ICollection<ISubStuffDto> SubStuff { get; set; }
}
We have also another project in this solution, XXX.Shared:
public class StuffDto : IStuffDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<ISubStuffDto> PlentySubStuff { get; set; } = new List<ISubStuffDto>();
}
Now we have another service that is an event consumer to the above, this service has imported XXX.Shared project:
public class StuffUpdatedConsumer : IConsumer<IStuffUpdated>
{
public async Task Consume(ConsumeContext<IStuffUpdated> context)
{
var castedStuff = context.Message.AffectedStuff as StuffDto;
}
}
Up until recently the casting above worked just fine. Now i'm forced to use a mapper to get the same result which i'd like to avoid. What could be the breaking change for this?
I have around 50 master tables that requires simple and straight forward CRUD operations, my tables are already available in the sql database.
My question is how to make it generic so that I dont need to create manually each individual page for master tables. I saw some ABP CRUDEntityAscyn classes in Boilerplate framework, but I am wondering how to bring it at Presentation layer (.cshtml).
If you need to create an application service that will have Create, Update, Delete, Get, GetAll methods for a specific entity, you can inherit from CrudAppService (or AsyncCrudAppService if you want to create async methods) class to create it easier. CrudAppService base class is generic which gets related Entity and DTO types as generic arguments and is extensible which allows you to override functionality when you need to customize it.
public class Task : Entity, IHasCreationTime
{
public string Title { get; set; }
public string Description { get; set; }
public DateTime CreationTime { get; set; }
public TaskState State { get; set; }
public Person AssignedPerson { get; set; }
public Guid? AssignedPersonId { get; set; }
public Task()
{
CreationTime = Clock.Now;
State = TaskState.Open;
}
}
[AutoMap(typeof(Task))]
public class TaskDto : EntityDto, IHasCreationTime
{
public string Title { get; set; }
public string Description { get; set; }
public DateTime CreationTime { get; set; }
public TaskState State { get; set; }
public Guid? AssignedPersonId { get; set; }
public string AssignedPersonName { get; set; }
}
public class TaskAppService : AsyncCrudAppService<Task, TaskDto>
{
public TaskAppService(IRepository<Task> repository)
: base(repository)
{
}
}
public interface ITaskAppService : IAsyncCrudAppService<TaskDto>
{
}
public class TaskAppService : AsyncCrudAppService<Task, TaskDto>, ITaskAppService
{
public TaskAppService(IRepository<Task> repository)
: base(repository)
{
}
}
calling webapi from client code:
var _editionService = abp.services.app.edition
_editionService.deleteEdition({
id: edition.id
}).done(function () {
getEditions();
abp.notify.success(app.localize('SuccessfullyDeleted'));
});
read for more > https://aspnetboilerplate.com/Pages/Documents/Application-Services#crudappservice-and-asynccrudappservice-classes
I'm learning about the Bind attribute and I have a doubt.
I can use the Bind attribute to include/exclude the data that will be posted, so.
Would it not be better to use a specific ViewModel instead of the Bind attribute?
Think about what happened if your entity changes overtime, then you might force to change all your different viewModels which you have created instead of using Include or Exclude. it will get hard to maintain your code.
Suppose you have this :
public class PersonalViewModel
{
private int PersonalID { get; set; }
public string PersonalName { get; set; }
public string PersonalFamily { get; set; }
public byte? GenderID { get; set; }
public string PersonalPhone { get; set;}
}
Consider these :
public string ShowPersonalToAll(
[Bind(Exclude = "PersonalPhone")]PersonalViewModel newPersonal)
{...}
OR
public class PersonalViewModel
{
private int PersonalID { get; set; }
public string PersonalName { get; set; }
public string PersonalFamily { get; set; }
public byte? GenderID { get; set; }
}
Now What if saving personal's mobile become important! and if you have created different customized ViewModel for several action (depends on application's business)?
Then you have to change the main ViewModel and all the other Customize ViewModel, While by using Exclude there is no need to change ViewModels, no need to change actions and the main ViewModel just changes.
If I have the following objects:
public class Application
{
public int ApplicationId { get; set; }
public string Name { get; set; }
public virtual ICollection<TestAccount> TestAccounts { get; set; }
}
public class TestAccount
{
public int TestAccountId { get; set; }
public int ApplicationId { get; set; }
public string Name { get; set; }
public virtual Application Application { get; set; }
}
EF Mapping looks like this:
modelBuilder.Entity<Application>()
.HasMany(a => a.TestAccounts)
.WithRequired(t => t.Application)
.WillCascadeOnDelete(false);
In one part of my code I want to retrieve data for Application and have
it return TestAccount data.
In another part of my code I want to retrieve data for Application and
have it NOT return TestAccount data.
Is there a way I can make this happen with LINQ or some other way?
This question has already been answered here: Disable lazy loading by default in Entity Framework 4.
Basically, in the constructor of your DbContext, just add this:
this.Configuration.LazyLoadingEnabled = false;
I hope this helps.
EDIT
Also, if you want to know how to load it manually later, it should be a simple matter of using Include() like this:
var query = context.Application.Include(x => x.TestAccounts).ToList()
I have an abstract class
public abstract class Member
{
public string ID { get; set; }
public string Username { get; set; }
public int MemberType { get; set; }
public abstract string MemberName { get; set; }
public int Status { get; set; }
}
public class Person : Member
{
public string LastName { get; set; }
public string FirstName{ get; set; }
}
public class Business : Member
{
public string BusinessName { get; set; }
public string TaxNo { get; set; }
}
The class was mapped using fluent API,
Now, is there a way to update the "Status" property from the view(having Member model) without using or going to a subclass (Person/Business)?
I just tried it, but it says "Cannot create an abstract class.", when submitting the page.
Or there is a correct way to do this?
Thanks
Not in EF. You have to instantiate an object to work with EF, and you can't instantiate an abstract class.
You could make the class not be abstract. Or you could use a stored proc to update the field, or some direct sql.
It sounds like your problem is that your action method has an abstract type as a parameter, which the default model binder can't do anything with. If you are dead set on using the same view for two different classes, you may need to implement your own model binder to inspect in the incoming request and decide which type, Person or Business, to instantiate.
Check out this link for more information on creating a custom model binder:
http://odetocode.com/blogs/scott/archive/2009/05/05/iterating-on-an-asp-net-mvc-model-binder.aspx
This seems like a similar problem to the one I've answered previously here:
ASP.NET MVC 3: DefaultModelBinder with inheritance/polymorphism