I have some data I need to store somewhere after a user logs in, what I need are:
1. A list of customers
2. US States list
for now that's all. Now Customer's list can continue to grow constantly and even after it has been loaded, obviously US States don't.
Currently what I do in my login.aspx page after the user logs in and everything is validated then I go to my SQL database and load all customers and US states into a Session variable. Right now this works although it does take a little bit of time but since there's only about 3 users at any time testing the site it doesn't affect. But as I understand this could be a problem when the company of about 50+ users start using it or even customers aswell.. So what would be the best way to work this and why? Thanks in advance.
ps. if this is not a valid question, please let me know and I'll take it down, I just didn't know where to ask this and get some useful feedback.
Andres,
Is it possible to (instead of using session) to cache the data. This can be done in multiple ways through various caching techniques (IE System.Web.Caching, MemoryCahce).
As your users grow \ shrink you can modify the DB and the cached instance at the same time. When the user requests the list of users (states) the cached list is evaluated. If the cached list isnt set then you re-build the cache list.
You could do something like. (Basic example)
public class UserCache : IEnumerable<User>
{
/// <summary>
/// const cache string
/// </summary>
const string userCacheString = "_userCacheList";
/// <summary>
/// current list of users
/// </summary>
public static UserCache Current
{
get
{
if (HttpContext.Current == null)
throw new Exception("NO CONTEXT");
var userList = HttpContext.Current.Cache[userCacheString] as UserCache;
if (userList == null)
{
userList = new UserCache();
HttpContext.Current.Cache[userCacheString] = new UserCache();
}
return userList;
}
}
/// <summary>
/// default constructor
/// </summary>
public UserCache()
{
}
/// <summary>
/// the list of users
/// </summary>
List<User> users;
/// <summary>
/// adds a user
/// </summary>
/// <param name="user"></param>
public void Add(User user)
{
if (this.Contains(user))
return;
this.users.Add(user);
}
/// <summary>
/// removes a user
/// </summary>
/// <param name="user"></param>
public void Remove(User user)
{
if (this.Contains(user))
return;
this.users.Remove(user);
}
/// <summary>
/// clears a user
/// </summary>
public void Clear()
{
this.users = null;
}
/// <summary>
/// fills the users from the database
/// </summary>
void fillUsers()
{
this.users = new List<User>();
//TODO: Get from DB
}
/// <summary>
/// gets the enumerator
/// </summary>
/// <returns></returns>
public IEnumerator<User> GetEnumerator()
{
if (this.users == null)
fillUsers();
foreach (var user in users)
yield return user;
}
/// <summary>
/// gets the enumerator
/// </summary>
/// <returns></returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
public class User
{
public string UserName { get; set; }
public Guid UserID { get; set; }
public string Email { get; set; }
}
From here you user management (where you add \ remove users) can call the UserCache to modify its collection accordinly. Such as (psudo code)
public class UserManager
{
public void Register(string userName, string email)
{
//TODO: Register in DB.
UserCache.Current.Add(new User
{
UserID = Guid.NewGuid(),
Email = email,
UserName = userName
});
}
}
From here you can always call UserCache.Current to get the current user list.
Just a thought.
EDIT: Response to Simon Halsey comment.
In the example I did inherit from the IEnumerable<> interface and not the List<> interface. This was a personal preference and to support "how" the class was defined. Now before I explain, this is not the only way but just a conceptual way of achieving the result.
In the example the method Clear() clears the inner list by setting the inner list user to null. In the IEnumerable<> implentation GetEnumerator() method the first check is if the inner list is null. If the inner list is null then the fillUsers() method is called to retrieve all users from the database.
If this example inherited from List<> then the Clear() method of List<> would be called and clears the list (removing all items) however the list is not null. Therefore enumerating the list after the Clear() method has been called will result in no users. Now this could be re-written using a List<> implentation as follows. Where the only thing you will have to do is override the Clear() method and the constructor to load the users. Such as.
public class UserListCache : List<User>
{
/// <summary>
/// const cache string
/// </summary>
const string userCacheString = "_userCacheList";
/// <summary>
/// current list of users
/// </summary>
public static UserListCache Current
{
get
{
if (HttpContext.Current == null)
throw new Exception("NO CONTEXT");
var userList = HttpContext.Current.Cache[userCacheString] as UserListCache;
if (userList == null)
{
userList = new UserListCache();
HttpContext.Current.Cache[userCacheString] = new UserListCache();
}
return userList;
}
}
/// <summary>
/// default constructor
/// </summary>
public UserListCache()
{
this.fillUsers();
}
/// <summary>
/// clear the list
/// </summary>
public new void Clear()
{
base.Clear();
this.fillUsers();
}
/// <summary>
/// fills the users from the database
/// </summary>
void fillUsers()
{
//TODO: Get from DB
}
}
Now neither method is better than the other (and the solution may not be adequate).
Related
Ive just started upgrading an old .net framework platform to .net Core 5 using Razor Pages
The first issue i ran into is on updating records.
We have a frontend and a backend form to edit users. On frontend only few fields are visible and on backend we have more fields
The model could look like this
public class User
{
public int ID {get;set;}
public string Name {get;set;}
public int UserType {get;set;}
public DateTime TStamp {get;set;}
}
On frontend the user can update the name, and i don't want to expose the value of UserType(or TStamp) using a hidden field.
But this means that the Usertype and TStamp always are reset
I have read the the best way is to send the model to the server and then update(and validate) the record serverside like :
Model recordToUpdate = GetRecordFromDB(id)
recordToUpdate.Name = postedRecord.Name;
UpdateRecord(recordToUpdate);
return recordToUpdate
Is there anyway else to accomplish update only few fields ?
08-02-2021 11:57
I have found this script which iterates through a model and a viewmodel and then transfer data.
https://www.codeproject.com/Tips/5163606/Generic-MVVM-Data-Exchange-between-Model-and-ViewM
public enum MVVMDirection { FROM, TO };
/// <summary>
/// ViewModel base class
/// </summary>
public class VMBase
{
/// <summary>
/// Move the data from the model to the viewmodel, using reflection.
/// Property names in both objects MUST be the same (both name and type)
/// </summary>
/// <typeparam name="TModel">The model's type</typeparam>
/// <param name="model">The model object the data will be moved from</param>
public void UpdateFromModel<TModel>(TModel model)
{
this.Update<TModel>(model, MVVMDirection.FROM);
}
/// <summary>
/// Move the data from the viewmodel to the model, using reflection.
/// Property names in both objects MUST be the same (both name and type)
/// </summary>
/// <typeparam name="TModel">The model's type</typeparam>
/// <param name="model">The model object the data will be moved from</param>
public void UpdateToModel<TModel>(TModel model)
{
this.Update<TModel>(model, MVVMDirection.TO);
}
/// <summary>
/// Update to or from the model based on the specified direction. Property names in both
/// objects MUST be the same (both name and type), but properties used just for the view
/// model aren't affected/used.
/// </summary>
/// <typeparam name="TModel">The model's type</typeparam>
/// <param name="model">The model object the data will be moved to/from</param>
/// <param name="direction">The direction in which the update will be performed</param>
public void Update<TModel>(TModel model, MVVMDirection direction)
{
PropertyInfo[] mProperties = model.GetType().GetProperties();
PropertyInfo[] vmProperties = this.GetType().GetProperties();
foreach (PropertyInfo mProperty in mProperties)
{
PropertyInfo vmProperty = this.GetType().GetProperty(mProperty.Name);
if (vmProperty != null)
{
if (vmProperty.PropertyType.Equals(mProperty.PropertyType))
{
if (direction == MVVMDirection.FROM)
{
vmProperty.SetValue(this, mProperty.GetValue(model));
}
else
{
vmProperty.SetValue(model, mProperty.GetValue(this));
}
}
}
}
}
If you are using EF Core, you can tell it which properties have to be updated in this way:
var user = new User{ ID = id, Name = postedRecord.Name};
_dbContext.Employee.Attach(user);
_dbContext.Entry(user).Property(x => x.Name).IsModified = true;
_dbContext.SaveChanges();
Code is further down in the post.
Question: I would like Swashbuckle to generate the following two "GET" requests:
1. mydomain.com/api/books?apikey=12345567891011121314151617181920
2. mydomain.com/api/books/1234?apikey=12345567891011121314151617181920
Swashbuckle does fine on #1 and it works great from the Swagger UI. But for #2, it ends up generating (and calling):
mydomain.com/api/books/{Id}?id=1234&apikey==12345567891011121314151617181920
Needless to say, that GET fails. Swashbuckle is picking up the literal "Id" from the route attribute as well as the BookDetail object. In fact, it shows both IDs in the UI, but I was able to solve that by de-duping by hooking up a custom IOperationFilter, but that obviously didn't help with correcting the actual GET request path.
I already looked at Duplicate parameter output in Swagger 2 but that answer does not work for me. I am looking for having Swashbuckle use the "Id" that's part of the BookDetail object so that Swagger UI shows the description (from the XML comment) while supporting route based ID rather than query string based: mydomain.com/api/books/1234.... What am I doing wrong? (I am on Swashbuckle 5.2.2).
Code:
/// <summary>
/// Books controller
/// </summary>
[RoutePrefix("api")]
public class BooksController : ApiController
{
/// <summary>
/// Returns books matching the search query
/// </summary>
/// <param name="searchRequest"></param>
/// <returns></returns>
[Route("books")]
public IHttpActionResult Get(BookSearch searchRequest)
{
//Do stuff with request
return Ok();
}
/// <summary>
/// Retruns a single book for the given ID
/// </summary>
/// <param name="detailRequest"></param>
/// <returns></returns>
[Route("books/{id:int}")]
public IHttpActionResult Get(BookDetail detailRequest)
{
//Do stuff with request
return Ok();
}
/// <summary>
/// Book search
/// </summary>
public class BookSearch
{
/// <summary>
/// API key
/// </summary>
[Required]
public string ApiKey { get; set; }
/// <summary>
/// Search terms
/// </summary>
public string Query { get; set; }
}
/// <summary>
/// Book detail
/// </summary>
public class BookDetail
{
/// <summary>
/// API key
/// </summary>
[Required]
public string ApiKey { get; set; }
/// <summary>
/// Book of the ID you want to retrieve
/// </summary>
[Required]
public int Id { get; set; }
/// <summary>
/// Boolean indicating whether to include photos or not
/// </summary>
public bool IncludePhotos { get; set; }
}
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I have a settings class that looks like the following:
/// <summary>
/// Class for pipeline settings
/// </summary>
public class PipelineSettings
{
/// <summary>
/// List of jobs to process
/// </summary>
[JsonProperty("jobs")]
public List<PipelineJob> Jobs;
// todo: make private and create FetchCredentials(id)
/// <summary>
/// List of credentials information cataloged by id
/// </summary>
[JsonProperty("credentials")]
public List<PipelineCredentials> Credentials;
}
And a credentials class that looks like the following:
/// <summary>
/// Class to hold credentials for pipeline jobs
/// </summary>
public class PipelineCredentials
{
/// <summary>
/// The id for the current credential to be used when referring to it within
/// other areas of json settings files
/// </summary>
[JsonProperty("id")]
public int Id;
/// <summary>
/// Username or login string for the current system
/// </summary>
[JsonProperty("username")]
public string Username;
// refine: AES auto encrypt?
/// <summary>
/// The password for the active authentication. If not encrypted then it
/// will be automatically converted into an AES encrypted string
/// </summary>
[JsonProperty("password")]
public string Password;
[JsonProperty("path")]
public string UNCPath;
}
I've built the following to try to add a new credential to my list:
var settings = new PipelineSettings();
// Build credentials for storage
var credentials = new PipelineCredentials();
credentials.Id = 1;
credentials.Username = "testUsername";
credentials.Password = "test_password";
credentials.UNCPath = null;
// Add credentials to the current settings class
settings.Credentials.Add(credentials);
var json = new JsonSerializeHelper();
Console.Write(json.Serialize(settings));
Console.ReadKey();
And when I do, I'm receiving a null reference exception on the following line:
settings.Credentials.Add(credentials);
I don't know what I don't know - how should I be adding new items into a list if they're prebuilt?
It looks like you need to instantiate the Credentials list.
settings.Credentials = new List<PipelineCredentials>
somewhere in the code. Or, to keep things tidy, you could do this in the constructor:
/// <summary>
/// Class for pipeline settings
/// </summary>
public class PipelineSettings
{
public PipelineSettings()
{
this.Credentials = new List<PipelineCredentials>();
}
/// <summary>
/// List of jobs to process
/// </summary>
[JsonProperty("jobs")]
public List<PipelineJob> Jobs;
// todo: make private and create FetchCredentials(id)
/// <summary>
/// List of credentials information cataloged by id
/// </summary>
[JsonProperty("credentials")]
public List<PipelineCredentials> Credentials;
}
That way, whenever a new PipelineSettings class is instantiated, it will automatically create a PipelineCredentials list.
Your Credentials list is not initialized, you could do that in the constructor:
public class PipelineSettings
{
/// <summary>
/// List of jobs to process
/// </summary>
[JsonProperty("jobs")]
public List<PipelineJob> Jobs;
// todo: make private and create FetchCredentials(id)
/// <summary>
/// List of credentials information cataloged by id
/// </summary>
[JsonProperty("credentials")]
public List<PipelineCredentials> Credentials;
public PipelineSettings()
{
Credentials = new List<PipelineCredentials>();
}
}
Very new to using Matisse so I don't know if I'm just flat out doing this wrong or something. I have a table containing usernames and passwords for the system, along with the following method...
public List<Users> FindUsers()
{
List<Users> users = new List<Users>();
//Users obj;
executeCmd("SELECT * FROM Users");
while (Reader.Read())
{
Users obj = new Users(db, 0x10ff);
users.Add(obj);
}
reader.Close();
return users;
}
Which I had hoped would simply pull all records from the table, drop them in a list and return that list. The problem however lies with this line of code here.
Users obj = new Users(db, 0x10ff);
I can only seem to pull one record, entering '0x10ff' as a parameter(the records OID) will return that record, which works with any record I try. Leaving it blank(i.e. '(db)' ) throws a Matisse exception(InvalidOperation) and entering anything else other than a specific records OID returns an ObjectNotFound exception. Am I just doing something totally stupid here? What am I missing?
Thanks
EDIT:
db refers to the database connection, shown below, DBAcess being a class with connectivity & query functionality.
private MtDatabase db;
public DBAccess()
{
db = new MtDatabase(Properties.Settings.Default.Host, Properties.Settings.Default.Database);
db.Open();
}
And as for the constructers, I left them as the default ones when I imported the classes from Matisse
// Generated constructor, do not modify
/// <summary>
/// The factory constructor
/// </summary>
/// <param name="db">a database</param>
/// <param name="mtOid">an existing object ID in the db</param>
public Users(MtDatabase db, int mtOid) :
base(db, mtOid) {
}
// Generated constructor, do not modify
/// <summary>
/// Cascaded constructor, used by subclasses to create a new object in the database
/// </summary>
/// <param name="clsObj">the class descriptor of the class to instantiate</param>
protected Users(MtClass clsObj) :
base(clsObj) {
}
#endregion
// GEN_END: Matisse Generated Code - Do not modify
// Generated constructor
/// <summary>
/// Default constructor provided as an example. NOTE: You may modify or delete this constructor
/// </summary>
/// <param name="db">a database</param>
public Users(MtDatabase db) :
base(GetClass(db)) {
}
Have now sorted this out, turns out I was just doing it wrong.
Instead of trying to retrieve the values with the reader, if you instead return the object and then use that to create a new User containing the values of said object, that works. Not sure if that really explains it all that well so here is the updated, working code.
public List<Users> FindUsers()
{
List<Users> users = new List<Users>();
executeCmd("SELECT REF(Users) FROM Users c");
while (Reader.Read())
{
MtObject obj = Reader.GetObject(0);
users.Add(new Users(db, obj.MtOid));
}
Reader.Close();
return users;
}
Does any one have a working example of getting google contact via AuthSub in c#
I have tried this url , but i was not able to complete it.
Here is a piece of code from one of my projects:
public class GoogleContactsProvider : IContactProvider
{
#region IContactProvider Members
/// <summary>
/// Gets the contacts list form the contact provider.
/// </summary>
/// <returns></returns>
public EntityCollection<IContactItem> GetContactsList()
{
EntityCollection<IContactItem> collection = new EntityCollection<IContactItem>();
// Setup the contacts request (autopage true returns all contacts)
RequestSettings requestSettings = new RequestSettings("AgileMe", UserName, Password);
requestSettings.AutoPaging = true;
ContactsRequest contactsRequest = new ContactsRequest(requestSettings);
// Get the feed
Feed<Contact> feed = contactsRequest.GetContacts();
// create our collection by looping through the feed
foreach (Contact contact in feed.Entries)
{
GoogleContactItem newContact = new GoogleContactItem();
newContact.Name = contact.PrimaryEmail.Address;
newContact.Summary = contact.Summary;
collection.Add(newContact);
}
return collection;
}
/// <summary>
/// Gets or sets the name of the user for the contact provider.
/// </summary>
/// <value>The name of the user.</value>
public string UserName { get; set; }
/// <summary>
/// Gets or sets the password for the contact provider.
/// </summary>
/// <value>The password.</value>
public string Password { get; set; }
#endregion
}
EntityCollection is a wrapper around a simple List and GoogleContactItem is a wrapper around the retrieved information.
I found this example so simple. link
And it works great.